Java JDK8 Learning Notes--the 13th chapter time and date

Source: Internet
Author: User
Tags dateformat time zones iso 8601 time and date

13th. Time and Date 13.1 understanding time and date 13.1.1 time measurement

1, Greenwich Mean time GMT
The midday of Greenwich Mean Time is when the sun reaches the highest point in the sky. It is now not used as a standard time.

2. World Time UT
The world is made by observing distant stars crossing the meridian, and GMT and UT are the same before the introduction of UTC.

3. International Atomic Time Tai
The international unit of the second is defined as the time spent on the radiation vibrations of cesium atoms for 91,926,331,770 weeks, starting with UT's 1958 synchronization.

4. UTC at World Coordination
A leap-second correction is used to ensure that UTC differs from UT by no more than 0.9 seconds, and the time to join a leap second is usually at the end of June or the end of December.

5. Unix Time
Defined as UTC time January 1, 1970 00:00:00 A description of the starting point, regardless of the leap second correction, to express a moment on the timeline.

6. Epoch
The beginning of a particular era, a moment on the timeline.

7. Key Summary

(1) Even if the mark is GMT time, actually speaking of time refers to UTC time.
(2) The unit definition of the second is based on the Tai, that is, the number of cesium atom radiation vibrations.
(3) UTC takes into account that the rotation of the earth is slower and more slowly and is modified to make sure that the difference between UTC and UT does not exceed 0.9 seconds.
(4) Unix time is the number of seconds elapsed from January 1, 1970 00:00:00 for the starting point, regardless of leap seconds.

About 13.1.2 Almanac

1. Julian calendar
The Julian calendar is the predecessor of the current Gregorian calendar, used to replace the Ides, fixed the Roman calendar three years set a leap year error, four years to change a leap.

2. Gregorian Calendar
The Gregorian calendar will be the next day of the Julian calendar on Thursday, October 4, 1582, on Friday, October 15, 1582, for Gregorian calendar.

3. ISO 8601 Standard
ISO 8601 is not a calendar system, but a standard for time and date representations, a data interchange format that is intended to unify time and date. In the definition of ISO 8601, 19th century refers to 1900 to 1999, and the Gregorian calendar of 19th century means 1801 to 1900.

13.1.3 recognize time zones

1. The z symbol is usually identified on the time representation of the UTC offset.

2. Daylight Saving Time
Also known as summer time, basically is on the first day of time, let the day increase by an hour, and the end of the day after the adjustment of an hour back.

13.2 recognize date and date on Calendar13.2.1 timeline

1. One way to get the system time is:
The System.currenttimemillis () method is used to return an integer of type long, representing the number of milliseconds since January 1, 1970 00:00:00.

2. Date instance
The date instance is basically recommended to be used only as a moment on the timeline. Processing of the string time format is no longer a duty of date.

13.2.2 Formatting the time date DateFormat

1, DateFormat is an abstract class, its operation class is Java.text.SimpleDateFormat.

Methods for obtaining SimpleDateFormat instances

(1) Building SimpleDateFormat instances directly
(2) Use static methods such as Getdateinstance (), Gettimeinstance (), Getdatetimeinstance () of DateFormat.

2. The benefit of building SimpleDateFormat directly is that you can use a pattern string to customize the format.

3, relative to the DateFormat can be date-time format, Java.text.NumberFormat can be used for digital format, they are Java.text.Format subclasses.

Calendar for 13.2.3 processing time date

1. Calendar instance
Used to get information about a time or date, or to manipulate a time date.
Calendar is an abstract class, Java.util.GregorianCalendar is its subclass, manipulating the Julian calendar and Gregorian calendar of the mixed calendar, through the calendar of getinstance () to obtain the calendar instance, The default is to get an instance of GregorianCalendar.

2. You can use the roll () method if you intend to add or subtract only one field in the date.

3, Calendar time can be modified using GregorianCalendar's Setgregoriancalendar () method, set to date (Long.maxVALUE) is the pure Julian calendar, set to date (Long.min VALUE) is a purely Gregorian calendar.

4. Note:

An operation such as add () on the calendar instance modifies the calendar instance itself, in order to avoid the passing of the calendar argument that was passed after calling Yearsbetween (), Daysbetween (), to be modified. Each of the two methods clones () The action of the first argument to copy the object.

13.2.4 Setting TimeZone

Use Java.util.TimeZone's Getdefault () to obtain the default time zone information.

13.3 JDK8 New Time date API13.3.1 machine time View API

1, the calendar gettime () returns false instance, obtains the date instance, next should obtain the time information, should be obtains the epoch millisecond number through the date gettime ().

2, machine-related time concept--instant class
The number of milliseconds that are used to represent a point in time after the custom Java epoch, which is essentially milliseconds. Use the static method of instant now () to obtain a instant instance representing the number of milliseconds in the Java epoch, and after obtaining the instant instance, you can use Plusseconds (), Plusmillis (), Plusnanos (), Minusseconds (), Minusmillis (), Minusnanos () do the operations on the timeline, the instant instance itself does not change, and these operations return a new instant instance, representing the instantaneous after the operation.

13.3.2 API for Human time perspective

1, LocalDateTime, Localdate and LocalTime
These classes are based on the ISO 8601 calendar system, which is a time and date definition without losing time zones. Class names, such as LocalDateTime, Localdate, and localtime, begin with local, indicating that they are only descriptions of time and do not have time zone information.

2, Zoneddatetime and Offsetdatetime
Between Zoneddatetime and Offsetdatetime can be transferred through the ToXxx () method, instant through Atzone () and Atoffset () to Zoneddatetime and Offsetdatetime, Zoneddatetime and Offsetdatetime can also be obtained through toinstant () Instant,zoneddatetime and Offsetdatetime have Tolocaldate (), ToLocalTime (), Tolocaldatetime () method can obtain Localdate, localtime and LocalDateTime.

3, year, Yearmonth, month and MonthDay
Month is the enum type, you want to get the number representing the month, do not use the Oridinal () method, you need to use the GetValue () method.

13.3.3 the operation of time

1, Temporalamount
For time measurement, the new time and date API is defined by the class duration, can have the measurement days, hours, minutes, seconds time difference, precision adjustment can reach the nanosecond level, and the maximum value of seconds can be a long type can be saved value. The period class is used to define the date difference for the year, month, week, and day.

2, Temporalunit
Plus () method another overloaded version that accepts Java.time.temporal.TemporalUnit instances, Java.time.temporal.ChronoUnit is a Temporalunit implementation class that uses the enum implementation.

3, temporal
Instant, LocalDateTime, Localdate, LocalTime, Zoneddatetime, and offsetdatetime all operate temporal interfaces.

4, Temporalaccessor
Temporalaccessor defines a read-only time object read operation, in fact temporal is a temporalaccessor sub-interface, adding time to the processing operation.

MonthDay is read-only.

13.3.4 Calendar System Design

1. The class in the Java.time suite is a single ISO8601 calendar system when it is necessary to implement the annual calendar system.

2. When you need to use another calendar system
Need to explicitly implement the Java.util.chrono medium Operation Java.time.chrono.Chronology interface class.

Java JDK8 Learning Notes--the 13th chapter time and date

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.