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());
各位可能想了解类似<span style="background-color: rgb(240, 240, 240);">Asia/Shanghai从哪边可以查到,本文最后将附上查找表。</span>

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

时间: 2024-12-28 11:38:01

Android时间获取与使用的相关文章

[转]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/

Android常用之系统时间获取

1.Calendar Calendar c = Calendar.getInstance(); 取得系统日期:year = c.get(Calendar.YEAR) month = c.grt(Calendar.MONTH) day = c.get(Calendar.DAY_OF_MONTH) 取得系统时间:hour = c.get(Calendar.HOUR_OF_DAY); minute = c.get(Calendar.MINUTE) 2.new Date SimpleDateFormat

21.Android 时间工具DateUtil

21.Android 时间工具DateUtil Android 时间工具DateUtil String 转换 Date long 转换 String long 去除 时分秒 计算时间差 24小时制转12小时制 Date 转换 String DateUtil全部源码 String 转换 Date /** * String 转换 Date * * @param str * @param format * @return */ public static Date string2Date(String

Android 时间更新机制之网络更新时间

转载请注明出处:http://blog.csdn.net/droyon/article/details/45701257 综述:Android网络时间更新,大体分两类.1.moderm相关更新,2.网络更新.本次主要介绍网路更新时间,主要涉及到NetworkTimeUpdateService,该类运行在SystemServer(ActivityManagerService)进程中.它有点特殊,从名字来看,其实Service,其实它和WifiService.ConnectivityManagerS

Android 时间工具DateUtil

Android 时间工具DateUtil   String 转换 Date long 转换 String long 去除 时分秒 计算时间差 24小时制转12小时制 Date 转换 String DateUtil全部源码 String 转换 Date   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 /** * String 转换 Date * * @param str * @param format * @return */ public static Date st

android 时间对话框 TimePickerDialog详细介绍

 个人在做提醒功能的时候用到了TimePickerDialog对话框,查阅了很多技术资料,但是感觉很多东西都说的不是很详细,并且很多地方,都有不完善的地方,比如有弹出对话框得到的不是系统当前的时间,而是一个其他时间的现象,让人很是苦闷.所以在经过几度研究之后,决定把个人对此空间的部分理解拿出来与大家讨论,如有不对之处,敬请谅解,会加以改进. 闲话不多说,直接进入主题: 首先,我们要在程序中声明一个日历的对象,并对其实例化获取日历实例,后面会用到. private Calendar c = C

Android实现获取本机中所有图片

本示例演示如何在Android中使用加载器(Loader)来实现获取本机中的所有图片,并进行查看图片的效果. 在这个示例中,我使用android-support-v4.jar中的加载器(Loader)来实现获取本机中所有图片,关于这个包在以前的文章中也提到,是一个非常有用的包,关于这个包的详细信息请大家查看官方文档:http://developer.android.com/sdk/compatibility-library.html. 关于加载器(Loader)是在Android3.0中才开始引

时间获取程序客户端 TCP 协议相关性

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 最初代码: 这是一个简单的时间获取客户程序.客户与服务器建立一个TCP连接后,服务器以直观 可读格式简单地送回当前时间和日期 这个例子是协议相关,使用 inet_pton TCP/IPv4, IPv6 协议相关 IPv4 --> IPv6 (把代码中出现的左边的字符串换为右边的,就变成了IPv6版本的) sockaddr_in --> sockaddr_in6 AF_INET -->

时间获取程序服务器 TCP 协议相关性

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 最初代码: 这是一个简单的时间获取服务器程序.它和时间获取程序客户端一道工作. TCP/IPv4, IPv6 协议相关 IPv4 --> IPv6 (把代码中出现的左边的字符串换为右边的,就变成了IPv6版本的) sockaddr_in --> sockaddr_in6 AF_INET --> AF_INET6 sin_family --> sin6_family sin_p