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)