[转]Android时间获取与使用
编写Android网络程序时难免会遇到手机时间不准确的问题,本文总结了一些常用的时间获取与校正方法:
转载请注明:http://blog.csdn.net/xzy2046
1.获取本机当前时间:
Time local = new Time();
local.set(System.currentTimeMillis());
mLocalTime.setText(local.format2445());
2.获取本机时间对应的上海时间:
Time shanghai = new Time("Asia/Shanghai");
shanghai.set(System.currentTimeMillis());
mShangHaiTime.setText(shanghai.format2445());
各位可能想了解类似Asia/Shanghai从哪边可以查到,本文最后将附上查找表。
3.获取本机时间对应的东京时间
Time tokyo = new Time("Asia/Tokyo");
tokyo.set(System.currentTimeMillis());
mTokyoTime.setText(tokyo.format2445());
4.获取网络时间:
URL url = null;
try {
url = new URL("http://www.baidu.com");
} catch (MalformedURLException e) {
e.printStackTrace();
}
URLConnection uc = null;
try {
uc = url.openConnection();
uc.connect();
} catch (IOException e) {
e.printStackTrace();
}
long ld = uc.getDate();
Date date = new Date(ld);
Log.i("xzy", "Net Time is : " + date.getHours() + "时" + date.getMinutes() + "分" + date.getSeconds() + "秒");
注意网络相关方法需要做异步处理,不能直接在UI线程中获取。本文最后将提供一个Demo,在AsyncTaskLoader中进行网络请求。
5.通过Gps获取当前时间:
LocationManager locMan = (LocationManager) this.getSystemService(LOCATION_SERVICE);
locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
long time = location.getTime();
Date date = new Date(time);
mGpsTime.setText(date.toString());
} @Override
public void onStatusChanged(String provider, int status, Bundle extras) { } @Override
public void onProviderEnabled(String provider) { } @Override
public void onProviderDisabled(String provider) { }
});
6.计算手机当前时区和北京时区的差值:
Date date = new Date();
long timezoneDiff = TimeZone.getDefault().getOffset(date.getTime()) - TimeZone.getTimeZone("GMT+8").getOffset(date.getTime());
通过这个差值和之前我们获取网络时间,可以计算出本机时间和网络时间的差值。对时间进行转换,解决一系列的时间错误问题。
7.附国家缩写和时区名称对照表:
//主要城市,国家对应表
String[] world = new String[]{
"ad", "Europe/Andorra",
"ae", "Asia/Dubai",
"af", "Asia/Kabul",
"ag", "America/Antigua",
"ai", "America/Anguilla",
"al", "Europe/Tirane",
"am", "Asia/Yerevan",
"an", "America/Curacao",
"ao", "Africa/Luanda",
"aq", "Antarctica/McMurdo",
"aq", "Antarctica/DumontDUrville",
"aq", "Antarctica/Casey",
"aq", "Antarctica/Davis",
"aq", "Antarctica/Mawson",
"aq", "Antarctica/Syowa",
"aq", "Antarctica/Rothera",
"aq", "Antarctica/Palmer",
"ar", "America/Argentina/Buenos_Aires",
"as", "Pacific/Pago_Pago",
"at", "Europe/Vienna",
"au", "Australia/Sydney",
"au", "Australia/Adelaide",
"au", "Australia/Perth",
"au", "Australia/Eucla",
"aw", "America/Aruba",
"ax", "Europe/Mariehamn",
"az", "Asia/Baku",
"ba", "Europe/Sarajevo",
"bb", "America/Barbados",
"bd", "Asia/Dhaka",
"be", "Europe/Brussels",
"bf", "Africa/Ouagadougou",
"bg", "Europe/Sofia",
"bh", "Asia/Bahrain",
"bi", "Africa/Bujumbura",
"bj", "Africa/Porto-Novo",
"bm", "Atlantic/Bermuda",
"bn", "Asia/Brunei",
"bo", "America/La_Paz",
"br", "America/Noronha",
"br", "America/Sao_Paulo",
"br", "America/Manaus",
"bs", "America/Nassau",
"bt", "Asia/Thimphu",
"bw", "Africa/Gaborone",
"by", "Europe/Minsk",
"bz", "America/Belize",
"ca", "America/St_Johns",
"ca", "America/Halifax",
"ca", "America/Toronto",
"ca", "America/Winnipeg",
"ca", "America/Edmonton",
"ca", "America/Vancouver",
"cc", "Indian/Cocos",
"cd", "Africa/Lubumbashi",
"cd", "Africa/Kinshasa",
"cf", "Africa/Bangui",
"cg", "Africa/Brazzaville",
"ch", "Europe/Zurich",
"ci", "Africa/Abidjan",
"ck", "Pacific/Rarotonga",
"cl", "America/Santiago",
"cl", "Pacific/Easter",
"cm", "Africa/Douala",
"cn", "Asia/Shanghai",
"co", "America/Bogota",
"cr", "America/Costa_Rica",
"cu", "America/Havana",
"cv", "Atlantic/Cape_Verde",
"cx", "Indian/Christmas",
"cy", "Asia/Nicosia",
"cz", "Europe/Prague",
"de", "Europe/Berlin",
"dj", "Africa/Djibouti",
"dk", "Europe/Copenhagen",
"dm", "America/Dominica",
"do", "America/Santo_Domingo",
"dz", "Africa/Algiers",
"ec", "America/Guayaquil",
"ec", "Pacific/Galapagos",
"ee", "Europe/Tallinn",
"eg", "Africa/Cairo",
"eh", "Africa/El_Aaiun",
"er", "Africa/Asmara",
"es", "Europe/Madrid",
"es", "Atlantic/Canary",
"et", "Africa/Addis_Ababa",
"fi", "Europe/Helsinki",
"fj", "Pacific/Fiji",
"fk", "Atlantic/Stanley",
"fm", "Pacific/Ponape",
"fm", "Pacific/Truk",
"fo", "Atlantic/Faroe",
"fr", "Europe/Paris",
"ga", "Africa/Libreville",
"gb", "Europe/London",
"gd", "America/Grenada",
"ge", "Asia/Tbilisi",
"gf", "America/Cayenne",
"gg", "Europe/Guernsey",
"gh", "Africa/Accra",
"gi", "Europe/Gibraltar",
"gl", "America/Danmarkshavn",
"gl", "America/Scoresbysund",
"gl", "America/Godthab",
"gl", "America/Thule",
"gm", "Africa/Banjul",
"gn", "Africa/Conakry",
"gp", "America/Guadeloupe",
"gq", "Africa/Malabo",
"gr", "Europe/Athens",
"gs", "Atlantic/South_Georgia",
"gt", "America/Guatemala",
"gu", "Pacific/Guam",
"gw", "Africa/Bissau",
"gy", "America/Guyana",
"hk", "Asia/Hong_Kong",
"hn", "America/Tegucigalpa",
"hr", "Europe/Zagreb",
"ht", "America/Port-au-Prince",
"hu", "Europe/Budapest",
"id", "Asia/Jayapura",
"id", "Asia/Makassar",
"id", "Asia/Jakarta",
"ie", "Europe/Dublin",
"il", "Asia/Jerusalem",
"im", "Europe/Isle_of_Man",
"in", "Asia/Calcutta",
"io", "Indian/Chagos",
"iq", "Asia/Baghdad",
"ir", "Asia/Tehran",
"is", "Atlantic/Reykjavik",
"it", "Europe/Rome",
"je", "Europe/Jersey",
"jm", "America/Jamaica",
"jo", "Asia/Amman",
"jp", "Asia/Tokyo",
"ke", "Africa/Nairobi",
"kg", "Asia/Bishkek",
"kh", "Asia/Phnom_Penh",
"ki", "Pacific/Kiritimati",
"ki", "Pacific/Enderbury",
"ki", "Pacific/Tarawa",
"km", "Indian/Comoro",
"kn", "America/St_Kitts",
"kp", "Asia/Pyongyang",
"kr", "Asia/Seoul",
"kw", "Asia/Kuwait",
"ky", "America/Cayman",
"kz", "Asia/Almaty",
"kz", "Asia/Aqtau",
"la", "Asia/Vientiane",
"lb", "Asia/Beirut",
"lc", "America/St_Lucia",
"li", "Europe/Vaduz",
"lk", "Asia/Colombo",
"lr", "Africa/Monrovia",
"ls", "Africa/Maseru",
"lt", "Europe/Vilnius",
"lu", "Europe/Luxembourg",
"lv", "Europe/Riga",
"ly", "Africa/Tripoli",
"ma", "Africa/Casablanca",
"mc", "Europe/Monaco",
"md", "Europe/Chisinau",
"me", "Europe/Podgorica",
"mg", "Indian/Antananarivo",
"mh", "Pacific/Majuro",
"mk", "Europe/Skopje",
"ml", "Africa/Bamako",
"mm", "Asia/Rangoon",
"mn", "Asia/Choibalsan",
"mn", "Asia/Hovd",
"mo", "Asia/Macau",
"mp", "Pacific/Saipan",
"mq", "America/Martinique",
"mr", "Africa/Nouakchott",
"ms", "America/Montserrat",
"mt", "Europe/Malta",
"mu", "Indian/Mauritius",
"mv", "Indian/Maldives",
"mw", "Africa/Blantyre",
"mx", "America/Mexico_City",
"mx", "America/Chihuahua",
"mx", "America/Tijuana",
"my", "Asia/Kuala_Lumpur",
"mz", "Africa/Maputo",
"na", "Africa/Windhoek",
"nc", "Pacific/Noumea",
"ne", "Africa/Niamey",
"nf", "Pacific/Norfolk",
"ng", "Africa/Lagos",
"ni", "America/Managua",
"nl", "Europe/Amsterdam",
"no", "Europe/Oslo",
"np", "Asia/Katmandu",
"nr", "Pacific/Nauru",
"nu", "Pacific/Niue",
"nz", "Pacific/Auckland",
"nz", "Pacific/Chatham",
"om", "Asia/Muscat",
"pa", "America/Panama",
"pe", "America/Lima",
"pf", "Pacific/Gambier",
"pf", "Pacific/Marquesas",
"pf", "Pacific/Tahiti",
"pg", "Pacific/Port_Moresby",
"ph", "Asia/Manila",
"pk", "Asia/Karachi",
"pl", "Europe/Warsaw",
"pm", "America/Miquelon",
"pn", "Pacific/Pitcairn",
"pr", "America/Puerto_Rico",
"ps", "Asia/Gaza",
"pt", "Europe/Lisbon",
"pt", "Atlantic/Azores",
"pw", "Pacific/Palau",
"py", "America/Asuncion",
"qa", "Asia/Qatar",
"re", "Indian/Reunion",
"ro", "Europe/Bucharest",
"rs", "Europe/Belgrade",
"ru", "Asia/Kamchatka",
"ru", "Asia/Magadan",
"ru", "Asia/Vladivostok",
"ru", "Asia/Yakutsk",
"ru", "Asia/Irkutsk",
"ru", "Asia/Krasnoyarsk",
"ru", "Asia/Novosibirsk",
"ru", "Asia/Yekaterinburg",
"ru", "Europe/Samara",
"ru", "Europe/Moscow",
"ru", "Europe/Kaliningrad",
"rw", "Africa/Kigali",
"sa", "Asia/Riyadh",
"sb", "Pacific/Guadalcanal",
"sc", "Indian/Mahe",
"sd", "Africa/Khartoum",
"se", "Europe/Stockholm",
"sg", "Asia/Singapore",
"sh", "Atlantic/St_Helena",
"si", "Europe/Ljubljana",
"sj", "Arctic/Longyearbyen",
"sk", "Europe/Bratislava",
"sl", "Africa/Freetown",
"sm", "Europe/San_Marino",
"sn", "Africa/Dakar",
"so", "Africa/Mogadishu",
"sr", "America/Paramaribo",
"st", "Africa/Sao_Tome",
"sv", "America/El_Salvador",
"sy", "Asia/Damascus",
"sz", "Africa/Mbabane",
"tc", "America/Grand_Turk",
"td", "Africa/Ndjamena",
"tf", "Indian/Kerguelen",
"tg", "Africa/Lome",
"th", "Asia/Bangkok",
"tj", "Asia/Dushanbe",
"tk", "Pacific/Fakaofo",
"tl", "Asia/Dili",
"tm", "Asia/Ashgabat",
"tn", "Africa/Tunis",
"to", "Pacific/Tongatapu",
"tr", "Europe/Istanbul",
"tt", "America/Port_of_Spain",
"tv", "Pacific/Funafuti",
"tw", "Asia/Taipei",
"tz", "Africa/Dar_es_Salaam",
"ua", "Europe/Kiev",
"ug", "Africa/Kampala",
"um", "Pacific/Wake",
"um", "Pacific/Johnston",
"um", "Pacific/Midway",
"us", "America/New_York",
"us", "America/Chicago",
"us", "America/Denver",
"us", "America/Los_Angeles",
"us", "America/Anchorage",
"us", "Pacific/Honolulu",
"uy", "America/Montevideo",
"uz", "Asia/Tashkent",
"va", "Europe/Vatican",
"vc", "America/St_Vincent",
"ve", "America/Caracas",
"vg", "America/Tortola",
"vi", "America/St_Thomas",
"vn", "Asia/Saigon",
"vu", "Pacific/Efate",
"wf", "Pacific/Wallis",
"ws", "Pacific/Apia",
"ye", "Asia/Aden",
"yt", "Indian/Mayotte",
"za", "Africa/Johannesburg",
"zm", "Africa/Lusaka",
"zw", "Africa/Harare",
};
测试程序下载地址:
http://download.csdn.net/detail/xzy2046/8161511
[转]Android时间获取与使用的更多相关文章
- android 时间获取以及时间格式化
Android中获取系统时间有多种方法,可分为Java中Calendar类获取,java.util.date类实现,还有android中Time实现 现总结如下: 方法一: void getTime1 ...
- android中获取时间
android中获取时间 1)通过calendar类获取 Calendar calendar = Calendar.getInstance();int moth = calendar.get(Cale ...
- Android实现获取本机中所有图片
本示例演示如何在Android中使用加载器(Loader)来实现获取本机中的所有图片,并进行查看图片的效果. 在这个示例中,我使用android-support-v4.jar中的加载器(Loader) ...
- Android中获取网页表单中的数据实现思路及代码
在Android中获取网页里表单中的数据具体实现代码如下,感兴趣的各位可以参考过下哈,希望对大家有所帮助 MainActivity如下: 复制代码 代码如下: package cn.testjavas ...
- Android中获取网页表单中的数据
MainActivity如下: package cn.testjavascript; import java.util.StringTokenizer; import android.os.Bundl ...
- Android中获取正在运行的服务-------ActivityManager.RunningServiceInfo的使用
关于PackageManager和ActivityManager的使用 ,自己也写了一些DEMO 了,基本上写的线路参考了Settings模块下的 应用程序,大家如果真正的有所兴趣,建议大家看看源码, ...
- Android开发 获取视频中的信息(例如预览图或视频时长) MediaMetadataRetriever媒体元数据检索器
前言 在Android里获取视频的信息主要依靠MediaMetadataRetriever实现 获取最佳视频预览图 所谓的最佳就是MediaMetadataRetriever自己计算的 /** * 获 ...
- URL转Drawable之 Android中获取网络图片的三种方法
转载自: http://doinone.iteye.com/blog/1074283 Android中获取网络图片是一件耗时的操作,如果直接获取有可能会出现应用程序无响应(ANR:Applicatio ...
- android 中获取视频文件的缩略图(非原创)
在android中获取视频文件的缩略图有三种方法: 1.从媒体库中查询 2. android 2.2以后使用ThumbnailUtils类获取 3.调用jni文件,实现MediaMetadataRet ...
随机推荐
- 【笔记】MySQL的基础学习
[笔记]MySQL的基础学习 老男孩 MySQL 一 安装与配置 1 下载安装 官网:http://dev.mysql.com/downloads/mysql/ 下载相应版本的压缩包 解压压缩包至任 ...
- sharepoint_study_6
描述:SharePoint 2013配置开发环境,需要安装VS2012插件 解决: 参见地址-http://www.java123.net/detail/view-330510.html
- 毕业设计 python opencv实现车牌识别 颜色判断
主要代码参考https://blog.csdn.net/wzh191920/article/details/79589506 GitHub:https://github.com/yinghualuow ...
- GitLab 遇到的坑
Job for postfix.service failed because the control process exited with error [zbb@storage1 ~]$ sudo ...
- 移动性能测试 | 持续集成中的 Android 稳定性测试
前言 谈到Android稳定测试,大多数会联想到使用monkey工具来做测试.google官方提供了monkey工具,可以很快速点击被应用,之前我有一篇帖子提到了monkey工具的使用,详见: htt ...
- error creating bean with name 'defaultvalidator' defined in class path resource
场景: 1.直接用eclipse 运行没问题(本地用的tomcat是7.0.70): 2.打包发布到服务器运行也没问题(服务器tomcat是8.5.30): 3.将打包发布的放到本地tomcat(7. ...
- Silverlight FullScreen 全屏
<UserControl x:Class="FullScreen.MainPage" xmlns="http://schemas.microsoft.com/win ...
- 多线程编程_读写锁ReadWriteLock
Lock比传统线程模型中的synchronized方式更加面向对象,与生活中的锁类似,锁本身也应该是一个对象.两个线程执行的代码片段要实现同步互斥的效果,它们必须用同一个Lock对象. 读写锁:分为读 ...
- 3d Max 2012安装失败怎样卸载3dsmax?错误提示某些产品无法安装
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- docker~service将容器日志输出到fluentd
我们把微小的服务发布到docker里,然后实现docker service启动它,保证了服务的高可用性,然后把服务产生的日志输出到fluentd,由它进行过滤和转发,存储到elasticsearch里 ...