Android time acquisition and use, android Time Acquisition

Source: Internet
Author: User
Tags net time

Android time acquisition and use, android Time Acquisition
When compiling an Android network program, you will inevitably encounter inaccurate mobile phone time. This article summarizes some common time acquisition and Correction Methods: for reprinting, please note: http://blog.csdn.net/xzy20461.get the original time:


 Time local = new Time();       
 local.set(System.currentTimeMillis());     
  mLocalTime.setText(local.format2445());


2. Get the Shanghai time corresponding to the local time:


 Time shanghai = new Time("Asia/Shanghai");   
     shanghai.set(System.currentTimeMillis());    
    mShangHaiTime.setText(shanghai.format2445());
You may want to know which side of 
<span style = "background-color: rgb (240,240,240);"> Asia/Shanghai can be found. The search table will be attached at the end of this article. </Span>



3. Get the Tokyo time corresponding to the local time



        Time tokyo = new Time("Asia/Tokyo");    
    tokyo.set(System.currentTimeMillis());     
   mTokyoTime.setText(tokyo.format2445());


4. Obtain the Network Time:



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 () + "Hour" + date. getMinutes () + "Minute" + date. getSeconds () + "seconds ");

Note that network-related methods need to be processed asynchronously and cannot be obtained directly in the UI thread. At the end of this article, we will provide a Demo to perform network requests in AsyncTaskLoader.


5. Get the current time through 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. Calculate the difference between the current time zone of the mobile phone and the Beijing time zone:



Date date = new Date(); long timezoneDiff = TimeZone.getDefault().getOffset(date.getTime()) - TimeZone.getTimeZone("GMT+8").getOffset(date.getTime());

Through this difference and we get the network time, we can calculate the difference between the local time and the network time. Converts time to solve a series of time errors.

7. Compare the country abbreviation with the time zone Name:
// Main city, country table String [] world = new String [] {
"ad", "Europe/Andorra", "AE", "Asia/Dubai ", "af", "Asia/Kabul", "ag", "America/Antigua",
 "ai", "America/Anguilla", "al", "Europe/Tianyi ", "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", "pacpacific/Pago_Pago", "at", "Europe/Vienna", 
"au", "Australia/sysydney ", "au", "Australia/adelide", "au", "Australia/Perth", 
"au", "Australia/eulinoleic", "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 ", "Atlanta/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/nicsia", "cz", "Europe/Prague", 
"de ", "Europe/Berlin", "dj", "Africa/djiboti", "dk", "Europe/Copenhagen", "dm", "America/Dominica", "do ", "America/Santo_Domingo", 
"dz", "Africa/Algiers", "ec", "America/Guayaquil", "ec", "pacpacific/Galapagos", "ee ", "Europe/Tallinn", "eg", "Africa/Cairo", "eh", "Africa/El_Aaiun", 
"er", "Africa/Asmara", "es ", "Europe/Madrid", "es", "Atlanta/Canary", "et", "Africa/Addis_Ababa", "fi", "Europe/Helsinki", "fj ", "pacpacific/Fiji", 
"fk", "Atlanta/Stanley", "fm", "pacpacific/Ponape", "fm", "pacpacific/Truk", "fo ", "Atlanta/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", "Atlanta/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", "Atlanta/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", "pacpacific/Kiritimati", 
"ki", "pacpacific/Enderbury", "ki", "pacpacific/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", "pacpacific/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 ico_city", "mx", "America/chibaihua ", "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/katmanu ", "nr", "pacpacific/Nauru", "nu", "pacpacific/Niue",
 "nz", "pacpacific/Auckland", "nz", "pacpacific/Chatham ", "om", "Asia/Muscat", "pa", "America/Panama", "pe", "America/Lima", "pf", "Pacific/Gambier ", 
"pf", "pacpacific/Marquesas", "pf", "pacpacific/Tahiti", "pg", "pacpacific/Port_Moresby", "ph", "Asia/Manila ", "pk", "Asia/Karachi", "pl", "Europe/Warsaw", 
"pm", "America/Miquelon", "pn", "pacpacific/Pitcairn ", "pr", "America/Puerto_Rico", "ps", "Asia/Gaza", "pt", "Europe/Lisbon", "pt", "Atlanta/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/krnoyarsk ",
 "ru", "Asia/Novosibirsk", "ru", "Asia/Yekaterinburg", "ru", "Europe/Samara", "ru", "Europe/Moscow ", "ru", "Europe/Kaliningrad", "rw", "Africa/Kigali",
 "sa", "Asia/Riyadh", "sb", "pacpacific/Guadalcanal ", "SC", "Indian/Mahe", "sd", "Africa/Khartoum", "se", "Europe/stockhoma", "sg", "Asia/Singapore ",
 "sh", "Atlanta/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", "pacpacific/Fakaofo ", "tl", "Asia/Dili", "tm", "Asia/Ashgabat", "tn", "Africa/Tunis", "to", "pacpacific/Tongatapu ", "tr", "Europe/Istanbul", "tt", "America/Port_of_Spain", 
"TV", "pacpacific/funafti", "tw", "Asia/Taipei ", "tz", "Africa/Dar_es_Salaam", "ua", "Europe/Kiev", "ug", "Africa/Kampala", "um", "pacpacific/Wake ", "um", "pacpacific/Johnston", 
"um", "pacpacific/Midway", "us", "America/New_York", "us", "America/Chicago ", "us", "America/Denver", "us", "America/Los_Angeles", "us", "America/Anchorage", 
"us", "pacpacific/Honolulu ", "uy", "America/Monte video", "uz", "Asia/Tashkent", "va", "Europe/Vatican", "vc", "America/St_Vincent ", "ve", "America/Caracas", "vg", "America/Tortola", 
"vi", "America/St_Thomas", "vn", "Asia/Saigon ", "vu", "pacpacific/Efate", "wf", "pacpacific/Wallis",
 "ws", "pacpacific/Apia", "ye", "Asia/Aden ", "yt", "Indian/Mayotte", "za", "Africa/Johannesburg", "zm", "Africa/Lusaka", "zw ", "Africa/Harare ",};


Test procedure:
Http://download.csdn.net/detail/xzy2046/8161511


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.