Java Date and time zone

Source: Internet
Author: User
Tags time zones local time

In Java, a Date class object contains 2 of information inside:

    • Absolute time, which is the number of milliseconds elapsed from 1970-1-1 00:00:00.000, expressed as a long type
    • Time

The absolute time is independent of the time zone. Suppose that at the same time, the Eastern hemisphere of the earth has a program dog executes the following code, the TA will each see 2 rows of output, where the first row is the same value, and the second row (most likely) shows a different time reading:

1 New Date (); 2 3 System.out.println (Now.gettime ()); 4 System.out.println (now);

When constructing a Date object, an object obtained using the constructor new date (long) has an unambiguous internal absolute time, which is the constructor parameter. But if the other needs to be given a year/month/day/... such as the date/time readings of the constructor obtained by the object, in different time zones, its internal absolute time will not necessarily be the same. Conversely, if the date object is not the same as the region, its internal absolute time is the same, but through Date.getyear ()/date.getmonth ()/... The reading of the method obtained will also be different (this is the case in the previous example). For the date class, all year/month/day/... Readings, whether the Getter method return value or Setter method/constructor parameter, represent local time, and ultimately its internal absolute time depends on the time zone in which it is located.

The time zone can be obtained through the Date.gettimezoneoffset () method. This method returns the difference between the local time and the UTC/GMT, in minutes. For example, our time zone is +08:00, that is, our local time 8 hours earlier than UTC (= 480 minutes), for example, for the same time description "x years x months x days x x x S X milliseconds", the local time occurred UTC 480 minutes earlier, that is, absolute time is less than UTC 480 minutes, therefore, The Date.gettimezoneoffset () return value is-480.

If you want to construct a given UTC year/month/day/... Date object for time readings, which can be obtained by using the DATE.UTC () method to obtain its absolute time, and then the date object through the new date (long) constructor. Note that this gets the date object with its getyear ()/getmonth ()/... The date/time value returned by the method is converted to local time, not (necessarily) the same as the previously given UTC time.

If you want to get a Date object that represents the absolute time of the UTC year/month/day/... Date/Time readings, you can construct a new Date object: new Date (That.gettime ()-60000 * That.gettimezoneoffset ()), and then call its getYear ()/getmonth ()/... and other methods.

If the MVC pattern is used as an analogy, the internal absolute time of the date object is model, its year/month/day/... The reading is view; the same model, in different time zones, shows different view. If you need to convert between local time and UTC, remember a formula for the same view:

local time-UTC = Timezoneoffset

Java Date and time zone

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.