Java gets UTC time

Source: Internet
Author: User

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

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.