Reciprocal Conversion of UTC (Java)

Source: Internet
Author: User
Tags dateformat time zones local time los angeles time zone

SimpleDateFormat foo =New SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); System.out.println ("foo:" +foo.format (NewDate ())); Calendar GC =Gregoriancalendar.getinstance (); System.out.println ("Gc.gettime ():" +Gc.gettime ()); System.out.println ("Gc.gettimeinmillis ():" +NewDate (Gc.gettimeinmillis ()));//Time of the current system default time zone: Calendar calendar=NewGregorianCalendar (); System.out.print ("Time zone:" +calendar.gettimezone (). GetID () + ""); System.out.println ("Time:" +calendar.get (Calendar.hour_of_day) + ":" +Calendar.get (Calendar.minute));//Los Angeles time zone TimeZone tz=timezone.gettimezone ("America/los_angeles");//Time zone ConversionCalendar.settimezone (TZ); System.out.print ("Time zone:" +calendar.gettimezone (). GetID () + ""); System.out.println ("Time:" +calendar.get (Calendar.hour_of_day) + ":" +Calendar.get (Calendar.minute)); Date time=NewDate ();//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)); // call Cal.get (int x) or Cal.gettimeinmillis () The time taken by the method is the UTC standard Time. System.out.println ("UTC:" +new Date ( Cal.gettimeinmillis ())); Calendar calendar1 = Calendar.getinstance (); TimeZone Tztz = Timezone.gettimezone ("GMT"  
---------------------------------------------------------------------------------------------
Summary of issues (-UTC) time zone acquisition

UTC time: Universal Time, world standard Time also known as Greenwich Day or GMT.

Can be obtained in a variety of ways, here are only two simple: get 0 time zones directly, calculated according to local time.

Method One, get 0 time zone directly:

[Java]
DateFormat dateformatter=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
TimeZone PST = Timezone.gettimezone ("etc/gmt+0");

Date curdate = new Date ();
Dateformatter.settimezone (PST);
String Str=dateformatter.format (curdate);//This is the value we want to get

DateFormat dateformatter=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
TimeZone PST = Timezone.gettimezone ("etc/gmt+0");

Date curdate = new Date ();
Dateformatter.settimezone (PST);
String Str=dateformatter.format (curdate);//This is the value we want to get method two, calculated according to local time:

[Java]
Calendar cal = Calendar.getinstance (Locale.getdefault ());
int zoneoffset = Cal.get (Java.util.Calendar.ZONE_OFFSET);
int dstoffset = Cal.get (Java.util.Calendar.DST_OFFSET);
Cal.add (Java.util.Calendar.MILLISECOND,-(Zoneoffset + dstoffset));
String Str=dateformat.format ("yyyy '-' mm '-' dd ' ' KK ': ' mm ': ' SS '", cal);//This is the value we want to get

Calendar cal = Calendar.getinstance (Locale.getdefault ());
int zoneoffset = Cal.get (Java.util.Calendar.ZONE_OFFSET);
int dstoffset = Cal.get (Java.util.Calendar.DST_OFFSET);
Cal.add (Java.util.Calendar.MILLISECOND,-(Zoneoffset + dstoffset));
String Str=dateformat.format ("yyyy '-' mm '-' dd ' ' KK ': ' mm ': ' SS '", cal);//This is the value we want to get the time zone gets:

String Zone=timezone.getdefault (). toString ();//This method doesn't get what we want.
Timezone.getdefault (). GetID ()//acquired only a city name in a time zone

We want to be able to directly identify the East eight (+8) or West seven (-7) and so on, this can only be obtained by the cheap amount of time to calculate, the method is as follows:

[Java]
Calendar cal = Calendar.getinstance (Locale.getdefault ());
int zoneoffset = Cal.get (Java.util.Calendar.ZONE_OFFSET);
int zone=zoneoffset/60/60/1000;//time zone, East time zone number is positive, West time zone is negative

Calendar cal = Calendar.getinstance (Locale.getdefault ());
int zoneoffset = Cal.get (Java.util.Calendar.ZONE_OFFSET);
int zone=zoneoffset/60/60/1000;//time zone, East time zone number is positive, West time zone is negative
PS:UTC Time + time zone difference = local time

< Span style= "color: #0000ff;" >< Span style= "color: #008000;" >< Span style= "color: #0000ff;" > 

Reciprocal Conversion of UTC (Java)

Related Article

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.