Time and timezone __java in Java

Source: Internet
Author: User
Tags cst time time zones local time
0. Preface:

Time Format:

World standard Time, where T is the start of the minute (or the interval between date and time), z indicates that this is a world standard Time
2017-12-13t01:47:07.081z

//local time, also called No Time zone information, no z
at the end 2017-12-13t09:47:07.153

//time zone information, +08:00 indicates that the time was obtained by the world standard Time plus 8 hours, [Asia/shanghai] represents the time zone
2017-12-13t09 : 47:07.153+08:00[asia/shanghai]

One of the most difficult to understand is the local time, 2017-12-13t09:47:07.153 time itself does not contain time zone information, but the word "local" contains time information. So I think this translation is not good, should not be called "local time", should be translated directly to "No Time zone information".

Coordinated world, also known as World unification Time, world standard Time, international coordination time. As the abbreviation for English (cut) and French (TUC) is different, as a compromise, referred to as UTC.
The time of the world, UT that is Greenwich, is the standard time of Greenwich Place, and also a form of the Earth's rotation rate. A time measurement system based on the rotation of the earth. 1. First Look at the JAVA8:

The main representations of time are 4 types of String, Instant, LocalDateTime, Zoneddatetime.

A string is a formatted time, instant is a time stamp, LocalDateTime is a time that does not contain time zone information, and Zoneddatetime is a time that contains time zone information. 1.1 The relationship between them is: 1.1.1 String is equivalent to LocalDateTime

A string that matches the format can be parsed directly into LocalDateTime, as follows:

System.out.println (Localdatetime.parse ("2017-12-13 10:10:10", Datetimeformatter.ofpattern ("Yyyy-mm-dd HH:mm:ss")) );

Output:
2017-12-13t10:10:10

The best way to discriminate localdatetime is not to think of it as "local time", which is "time without information of time zone". It only stores the time of the month and a minute, no time zone information is stored, which indicates where the times are all dependent on input and output interpretation. Complete equivalence to string, essentially parsing string, is only stored in the object for a minute and a minute, and is convenient to use. 1.1.2 Instant and Zoneddatetime are equivalent.

Instant is the time stamp, refers to the world standard GMT January 01, 1970 00:00 00 seconds (Beijing time January 01, 1970 08:00 00 seconds) up to now the total number of seconds, instant itself actually indicates time zone, 0 time zone.
Zoneddatetime is the time that contains time zone information, which is essentially displayed according to the time zone's instant format.

Zoneddatetime ztime1=zoneddatetime.ofinstant (Instant.now (), Zoneid.systemdefault ());
System.out.println (ztime1);
System.out.println (Ztime1.toinstant ()); 1
System.out.println (Ztime1.tolocaldatetime ());//3
Zoneddatetime ztime2=zoneddatetime.ofinstant ( Instant.now (), Zoneid.of ("Australia/darwin"));
System.out.println (ztime2);
System.out.println (Ztime2.toinstant ()); 2
System.out.println (Ztime2.tolocaldatetime ());//4

output:
2017-12-13t13:24:55.932+08:00[asia/ Shanghai]
2017-12-13t05:24:55.932z
2017-12-13t13:24:55.932
2017-12-13t14:54:55.933+09:30[ Australia/darwin]
2017-12-13t05:24:55.933z
2017-12-13t14:54:55.933

Note 1, 2 output is the same, indicating that the storage nature of zoneddatetime is instant;
Note 3, 4 output is different, stating that Zoneddatetime will be formatted according to the time zone that was passed in when the Zoneddatetime object was created.

The same instant have different display times in different time zones, so it is necessary to pass in the time zone when constructing zoneddatetime with instant; Zoneddatetime can be directly converted to instant, and different zoneddatetime may generate the same instant. 1.2 How to construct a Time object: 1.2.1 Direct Definition

System.out.println (Instant.ofepochmilli (System.currenttimemillis ()));
System.out.println (Localdatetime.of (2017,12,13,10,0,0,0));
System.out.println (Zoneddatetime.of (2017,12,13,10,0,0,0,zoneid.systemdefault ()));

Output:
2017-12-13t06:22:06.581z
2017-12-13t10:00
2017-12-13t10:00+08:00[asia/shanghai]
1.2.2 Get System Current time now ()
System.out.println (Instant.now ()); World Standard Time
System.out.println (Localdatetime.now ());//will convert world standard time to time in this time zone, but time zone information will be discarded
System.out.println ( Zoneddatetime.now ()); Will convert world standard time to time in this time zone, but time zone information will be preserved

System.out.println (Localdatetime.now (Zoneid.of ("+00:00"));//0 Time
zone now System.out.println (Zoneddatetime.now) (Zoneid.of ("+00:00")); 0 time zone now

output:
2017-12-14t02:53:05.830z
2017-12-14t10:53:05.904
2017-12-14t10:53:05.906+ 08:00[asia/shanghai]
2017-12-14t02:53:05.906
2017-12-14t02:53:05.906z
1.2.3 Parsing String
System.out.println (Instant.parse ("2007-12-03t10:15:30z")); You can only parse this format and you cannot specify
System.out.println (Localdatetime.parse ("2017-12-13 11:51:12.083"). Datetimeformatter.ofpattern ("Yyyy-mm-dd HH:mm:ss.") SSS "));
System.out.println (Zoneddatetime.parse ("2017-12-13 11:51:12.083 +04:30", Datetimeformatter.ofpattern ("Yyyy-mm-dd HH:mm:ss. SSS zzzzz "));

Output:
2007-12-03t10:15:30z
2017-12-13t11:51:12.083
2017-12-13t11:51:12.083+04:30
1.3 Conversion between time objects: the conversion between 1.3.1 Instant and LocalDateTime and Zoneddatetime
Instant Instant=instant.now ();
LocalDateTime localdatetime=localdatetime.ofinstant (Instant,zoneid.systemdefault ());
Zoneddatetime zoneddatetime=zoneddatetime.ofinstant (Instant,zoneid.systemdefault ());

SYSTEM.OUT.PRINTLN (instant);
System.out.println (localDateTime);
System.out.println (zoneddatetime);

System.out.println (Zoneoffset.systemdefault ());
System.out.println (ZONEOFFSET.UTC);
System.out.println (zoneoffset.min);
System.out.println (Zoneoffset.of ("+08:00"));
System.out.println (Localdatetime.toinstant (ZONEOFFSET.UTC)); When converting LocalDateTime to instant, it is necessary to explicitly specify that the current time refers to the time System.out.println of that time zone
(Localdatetime.toinstant ( Zoneoffset.of ("+08:00"));
System.out.println (Zoneddatetime.toinstant ());

Output:
2017-12-14t01:50:26.098z
2017-12-14t09:50:26.098
2017-12-14t09:50:26.098+08:00[asia/ Shanghai]
Asia/shanghai
Z
-18:00
+08:00
2017-12-14t09:50:26.098z
2017-12-14t01 : 50:26.098z
2017-12-14t01:50:26.098z
the conversion between 1.3.2 LocalDateTime and Zoneddatetime
Instant Instant=instant.now ();
LocalDateTime localdatetime=localdatetime.ofinstant (Instant,zoneid.systemdefault ());
Zoneddatetime zoneddatetime=zoneddatetime.ofinstant (Instant,zoneid.systemdefault ());

SYSTEM.OUT.PRINTLN (instant);
System.out.println (localDateTime);
System.out.println (zoneddatetime);

System.out.println (Zoneddatetime.of (Localdatetime,zoneid.systemdefault ())); LocalDateTime Turn zoneddatetime 
System.out.println (Zoneddatetime.tolocaldatetime ()); Zoneddatetime turn localdatetime

output:
2017-12-14t02:01:45.145z
2017-12-14t10:01:45.145
2017-12-14t10:01:45.145+08:00[asia/shanghai]
2017-12-14t10:01:45.145+08:00[asia/shanghai]
2017-12-14t10:01:45.145
1.4 Conversion between time zones

Time zone conversions should pay special attention to: the type of time that the user enters has no timezone information and needs to be interpreted artificially.
The parsing step is divided into 2 steps: Combining the context, analyzing the user time zone, converting the user input time into the world standard Time, and then changing the world standard Time to the time zone. 1.5 about the time trap 1.5.1 Questions

Because the concept of time zone exists, so there are 2 problems: users in different time zones, who understand time differently, the same time string in different time zones is not the same time stamp; servers in different time zones have different understandings of time, for example, the same program runs on servers in different time zones, When these programs call Localdatetime.now () at the same time, the result of the return is different, and if the operation is incorrect, the situation is more complex if the foreground and background programs are deployed separately on servers in different time zones. If the user, foreground, and background programs are not in the same time zone, .... The 1.5.2 Solution recommends that the time stamp be used uniformly in the system, including the front background transfer and the database storage, and that it be converted to a string only at the time of presentation, and that all the time will be converted to 0 time zones for easy processing. 2. Java8 Previous Time API

JAVA API Series-date and time related classes
Time zone conversion: Java new Date () becomes gmt&& GMT time with CST time conversion 3. Conversion of new and old time APIs

The new and old time APIs are connected by the date class and the Instant class, both of which are world standard times, but date prints are converted to local time.

Date Date=date.from (Instant.now ());
Instant instant=date.toinstant ();
SYSTEM.OUT.PRINTLN (date);
System.out.println (Date.gettime ()); 1
System.out.println (instant);
System.out.println (Instant.toepochmilli ()); 2

output:
Thu Dec 11:45:58 CST 2017
1513223158588
2017-12-14t03:45:58.588z
1513223158588

Note 1, 2 outputs the same, indicating that the date class and the Instant class are equivalent;

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.