Java local time vs. UTC time Conversion

Source: Internet
Author: User

UTC time: Universal co-ordination (Universal-time coordinated), like Greenwich Mean Time (Gmt,greenwich Mean time), is the same as in London, England.

ImportJava.text.SimpleDateFormat;ImportJava.util.Calendar;Importjava.util.Date;ImportJava.util.TimeZone; Public classGetutctime {//get local time:    PrivateCalendar cal =calendar.getinstance (); //get time Offset:    Private intZoneoffset =Cal.get (Java.util.Calendar.ZONE_OFFSET); //Get Daylight Saving time difference:    Private intDstoffset =Cal.get (Java.util.Calendar.DST_OFFSET);  Public Static voidMain (string[] args) {getutctime gc=NewGetutctime (); LongMill =Gc.getutctimestr ();    Gc.setutctime (mill); }     Public LongGetutctimestr () {System.out.println ("Local Millis =" + Cal.gettimeinmillis ());//equivalent System.currenttimemillis (), uniform value, no time zone//These differences are deducted from local time, that is, UTC time can be obtained:Cal.add (Java.util.Calendar.MILLISECOND,-(Zoneoffset +dstoffset)); LongMills =Cal.gettimeinmillis (); System.out.println ("UTC =" +Mills); returnMills; }     Public voidSetutctime (LongMillis)        {Cal.settimeinmillis (Millis); SimpleDateFormat Foo=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String Time=Foo.format (Cal.gettime ()); System.out.println ("GMT time=" +Time ); //These differences are deducted from local time, that is, UTC time can be obtained:Cal.add (Java.util.Calendar.MILLISECOND, (Zoneoffset +dstoffset)); time=Foo.format (Cal.gettime ()); System.out.println ("Local time =" +Time ); }     Public voidGetgmttime () {//mothed 2TimeZone gmttime = Timezone.gettimezone ("GMT"); Date Date=NewDate (); SimpleDateFormat format=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");        Format.settimezone (GmtTime); System.out.println ("GMT Time:" +Format.format (date)); //Method 2Calendar Calendar1 =calendar.getinstance ();        Calendar1.settimezone (GmtTime); //System.out.println (Calendar1.gettime ()); //Invalid time zone//System.out.println (Calendar1.gettimeinmillis ());//Invalid time zoneSystem.out.println ("GMT hour =" +Calendar1.get (Calendar.hour)); }}

Java local time vs. UTC time Conversion

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.