In the Java language, you can obtain a local time or a time instance of a specified timezone through the Java.util.Calendar class, as follows:
Get local time:
Java.util.Calendar cal = Java.util.Calendar.getInstance ();
Time to get the specified time zone:
Java.util.TimeZone zone = Java.util.TimeZone.getTimeZone ("gmt-8:00= java.util.Calendar.getInstance (zone);
Or:
Java.util.Calendar cal = Java.util.Calendar.getInstance (Java.util.Locale.CHINA);
You can even get the GMT 0 local time, but how do you get the standard (versus Java) time?
From my previous article about time and area, you can get the following methods:
1. Get local time:
Java.util.Calendar cal = Java.util.Calendar.getInstance ();
2. Get time Offset:
int zoneoffset = Cal.get (Java.util.Calendar.ZONE_OFFSET);
3, to obtain daylight saving time difference:
int dstoffset = Cal.get (Java.util.Calendar.DST_OFFSET);
4. Deduct these differences from local time, that is, you can get UTC time:
Cal.add (Java.util.Calendar.MILLISECOND,-(Zoneoffset + dstoffset));
After that, the time you get by calling the Cal.get (int x) or Cal.gettimeinmillis () method is the UTC standard Time.
The detailed code is as follows:
PackageCom.feima.afei.util; PackageCom.feima.afei.util;ImportJava.text.DateFormat;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;ImportJava.util.TimeZone;/** * * @authorJaven **/ Public Final classGetutctimeutil {Private StaticDateFormat format =NewSimpleDateFormat ("Yyyy-mm-dd hh:mm") ; /*** Get UTC time, type string, format "Yyyy-mm-dd hh:mm" <br/> * If get failed, return null *@return */ Public StaticString getutctimestr () {StringBuffer Utctimebuffer=NewStringBuffer (); //1. Get local time:Calendar cal =calendar.getinstance (); //2. Get time offset: intZoneoffset =Cal.get (Java.util.Calendar.ZONE_OFFSET); //3, to obtain daylight saving time difference: intDstoffset =Cal.get (Java.util.Calendar.DST_OFFSET); //4. Deduct these differences from local time, that is, you can get UTC time:Cal.add (Java.util.Calendar.MILLISECOND,-(Zoneoffset +dstoffset)); intYear =Cal.get (calendar.year); intmonth = Cal.get (calendar.month) +1; intDay =Cal.get (Calendar.day_of_month); inthour =Cal.get (Calendar.hour_of_day); intminute =Cal.get (Calendar.minute); Utctimebuffer.append (year). Append ("-"). Append (Month). Append ("-"). append (day); Utctimebuffer.append (""). Append (Hour). Append (":"). append (minute); Try{format.parse (utctimebuffer.tostring ()); returnutctimebuffer.tostring (); }Catch(ParseException e) {e.printstacktrace (); } return NULL ; } /*** Convert UTC time to East eight zone time *@paramUtctime *@return */ Public Staticstring Getlocaltimefromutc (String utctime) {java.util.Date utcdate=NULL ; String Localtimestr=NULL ; Try{utcdate=Format.parse (utctime); Format.settimezone (Timezone.gettimezone ("GMT-8")) ; Localtimestr=Format.format (utcdate); } Catch(ParseException e) {e.printstacktrace (); } returnLocaltimestr; } Public Static voidMain (string[] args) {String utctimestr=Getutctimestr (); System.out.println (UTCTIMESTR); System.out.println (GETLOCALTIMEFROMUTC (UTCTIMESTR)); }}
Java gets UTC time