Speaking of Java time, you may be familiar with the date, calendar, simpledateformat, and so on. But do you really have a deep understanding of them? Today, I want to talk about timezone.
Some people often find that the time is incorrect, for example, the difference of 8 hours. The real reason is timezone. Only correct and reasonable use of timezone can ensure that the system time is accurate at any time.
Factors Affecting timezone:
1. Set the time zone of the operating system.
2. Data Transmission Time Zone settings.
The first reason is actually the root cause. when data is transferred between different operating systems, there may be time deviations due to differences in operating systems, by default, the JVM obtains the time zone settings of the operating system. Therefore, it is best to set a time zone in advance in the project, for example:
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
So what should I do if there is a time deviation caused by different time zone settings? How to convert it?
● Use simpledateformat as follows:
public static void main(String[] args) {Date date = new Date(1359641834000L);// 2013-1-31 22:17:14String dateStr = "2013-1-31 22:17:14";SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));try {Date dateTmp = dateFormat.parse(dateStr);System.out.println(dateTmp);} catch (ParseException e) {e.printStackTrace();}String dateStrTmp = dateFormat.format(date);System.out.println(dateStrTmp);}
Running result:
Fri Feb 01 06:17:14 CST 20132013-01-31 14:17:14
We found that the running results of strings and dates are different at the same time. So what should we do? Everything should be based on the time of the current operating system. My operating system is "Asia/Shanghai", that is, Beijing time of GMT + 8. Therefore, when the format method of date to string is executed, the default time zone of the operating system is used when the date is generated, therefore, 22:17:14 is the Beijing time. Therefore, it takes 8 hours to calculate the GMT time zone. When the parse method of string to date is executed, because the string itself does not have the time zone concept, therefore, 22:17:14 refers to GMT (UTC) time [PS: All strings are regarded as GMT time]. When converted to date, the default time zone, that is, "Asia/Shanghai ", therefore, it takes 8 hours.
● Use calendar as follows:
Public static void main (string [] ARGs) {date = new date (1359641834000l); // 2013-1-31 22: 17: 14system. out. println (date); Calendar calendar = calendar. getinstance (); Calendar. settimezone (timezone. gettimezone ("GMT"); // or you can use the calendar ar calendar = calendar ar. getinstance (timezone. gettimezone ("GMT"); Calendar. settime (date); system. out. println (calendar. get (calendar. hour_of_day) + ":" + calendar. get (calendar. minute ));}
Running result:
Thu Jan 31 22:17:14 CST 201314:17
Calendar does not involve conversion of dates and strings, so it is not as complicated as simpledateformat. It is similar to the conversion of dates to strings. However, after setting the time zone, you cannot use the calendar. gettime () is used to directly obtain the date. Therefore, the date at this time is the same value as the start settime. To obtain the time of a time zone, use calendar. in the get () method, how do we obtain a date of the date type?
The correct method is as follows:
Public static void main (string [] ARGs) {date = new date (1359641834000l); system. out. println (date); Calendar calendar = calendar. getinstance (); Calendar. settimezone (timezone. gettimezone ("GMT"); // or you can use the calendar ar calendar = calendar ar. getinstance (timezone. gettimezone ("GMT"); Calendar. settime (date); Calendar calendar2 = calendar. getinstance (); calendar2.set (calendar. get (calendar. year), calendar. get (calendar. month), calendar. get (calendar. day_of_month), calendar. get (calendar. hour_of_day), calendar. get (calendar. minute), calendar. get (calendar. second); system. out. println (calendar2.gettime ());}
Running result:
Thu Jan 31 22:17:14 CST 2013Thu Jan 31 14:17:14 CST 2013
● Perfect general Conversion Method
In fact, the above two conversion methods are affected by the time zone settings of the operating system. If the software runs in different operating systems, there will still be time errors, then how can we unify them?
Public static void main (string [] ARGs) {date = new date (1359641834000l); system. out. println (date); Date = changetimezone (date, timezone. gettimezone ("Asia/Shanghai"), timezone. gettimezone ("GMT"); system. out. println (date );} /*** get the date after the time zone is changed * @ Param date * @ Param oldzone Old Time Zone object * @ Param newzone new zone object * @ return date */public static date changetimezone (date, timezone oldzone, timezone newzone ){ Date datetmp = NULL; If (date! = NULL) {int timeoffset = oldzone. getrawoffset ()-newzone. getrawoffset (); datetmp = new date (date. gettime ()-timeoffset);} return datetmp ;}
Running result:
Thu Jan 31 22:17:14 CST 2013Thu Jan 31 14:17:14 CST 2013
From the above, we can see that the time zone does have a lot of things that need our attention and research. In fact, this is not a profound thing, but also the basis of Java dates, I believe that blind learning of various open-source frameworks cannot become a true master. The framework is developed based on JDK and does not understand the basic knowledge of JDK, it is very difficult to really raise the level of the architect, so the foundation is very important. Mastering Basic JDK skills is indeed a prerequisite for the "hero". Please work together!