1. Concurrent processing of temporal data

Java. Text. SimpleDateFormat instance of the object in a multi-threaded Shared use will be thrown when the conversion is unusual, can use ThreadLocal, but can also lead to other problems. Prior to JDK8, joda-time was mostly used as a Time solution. However, since Java SE 8, joda-time officially requires users to migrate to java.time (JSR-310) – replacing the JDK core part of the project.

In JDK8, a number of thread-safe time classes are provided, such as:

  • Local date and time:LocalDateTime.LocalDate.LocalTime;
  • Date and time with time zone:ZonedDateTime;
  • Time:Instant;
  • Time zone:ZoneId.ZoneOffset;
  • Time interval:Duration.
  • Formatting:DateTimeFormatter

DateTimeFormatter is an alternative to SimpleFormatter.

Second, the ISO 8601

International standard ISO 8601, is the international Organization for Standardization of date and time representation method, full name “data storage and exchange forms, information exchange, date and time representation method”. The current third edition of “ISO8601:2004” was issued on 1 December 2004 to replace the first edition of “ISO8601:1998” in 1998 and the second edition of “ISO8601:2000” in 2000.

Common notation

  • Calendar date notation
    • The year is a four-digit number, the month is a two-digit number, and the day in the month is a two-digit number. For example, the date (December 16, 2021) can be expressed as 2021-12-16, or 20211216.
  • Sequential date notation
    • The ordinal number of days in a year can be represented by three digits, 365 in an ordinary year and 366 in a leap year. For example, December 16, 2021 can be 2021-350 or 2021350
  • Week calendar notation
    • The year is a four-digit number, with two digits representing the week of the year (preceded by a capital W) and one digit representing the day of the week. Such asDecember 16, 2021Can be expressed as2021-W50-4.
  • Calendar week notation
    • withWeek calendar notationIt’s similar, except one digit is missing for the day
  • Calendar month notation
    • The number of calendar months of the year is expressed in 2 digits. For example, a date (December 16, 2021) can be expressed as 2021-12
  • Calendar year notation
    • For example, the year 2021 can be written as 2021
  • Time notation
    • A colon (:) is used for an extended interval of hours, minutes, and seconds. Hours, minutes, and seconds are represented by two digits. If the time is in the zero time zone and happens to be the same as COORDINATED Universal time, you also need to add an uppercase at the endZ. For example,15:30:53Z;
    • Other time zones are expressed as the actual time plus the time difference. UTC+8 at the time is 15:30:53+08:00 or 153053+0800, which can also be simplified to 153053+08.

Coordinated Universal Time is the main Time standard for regulating clocks and Time in the world. The difference between Coordinated Universal Time and mean solar Time of 0 degrees longitude is less than one second. If the local time is faster than UTC, for example, the time in mainland China, Hong Kong, Macau, Taiwan, Mongolia, the Philippines, Singapore, Malaysia, and western Australia is 8 hours ahead of UTC, it is called UTC+8. Conversely, if the local time is slower than UTC, for example, Hawaii is 10 hours behind UTC, it is written UTC-10, commonly known as The West 10 Region.

Use DateTimeFormatter

The DateTimeFormatter is used to print and parse date and time objects, while more complex formats can be handled by the DateTimeFormatterBuilder.

Predefined variable

A default format is provided for the time object as follows:

constant describe The instance
ISO_DATE ISO date,Band offset The 2021-12-07 or 2021-12-07 + 01:00
ISO_DATE_TIME Date and time with ZoneId 2021-12-07T09:50:46, 2021-12-07T09:50:46+01:00, 2021-12-07T09:50:46+01:00[Europe/Paris]
ISO_TIME ISO,Band offset Hold, hold + 01:00
ISO_INSTANT An instant ISO date formatted or parsed in UTC format 2021-12-07T09:50:46Z
ISO_LOCAL_DATE ISO local date 2021-12-07
ISO_LOCAL_DATE_TIME ISO local date and time 2021-12-07T09:50:46
ISO_LOCAL_TIME ISO local time 09:50:46
ISO_OFFSET_DATE With the offsetISO date The 2021-12-07 + 01:00
ISO_OFFSET_DATE_TIME ISO date and time with offset 2021-12-07T09:50:46+01:00
ISO_OFFSET_TIME ISO time with offset 09:50:46 + 01:00
ISO_ORDINAL_DATE Used to representyearswithThe day of the year 2021-341.
ISO_WEEK_DATE Years and weeks 2021-W49-2
ISO_ZONED_DATE_TIME Partition date time 2021-12-07T09:50:46+01:00[Europe/Paris]

The instance

Query date and time:

LocalDate d = LocalDate.now(); LocalTime t = localtime.now (); // LocalDateTime dt = localDatetime.now (); // Current date and timeCopy the code

Convert Date to LocalDateTime:

public static void main(String[] args) {
    System.out.println(Instant.ofEpochMilli(new Date().getTime())
            .atZone(ZoneId.systemDefault())
            .toLocalDateTime());
}
Copy the code

Convert Date to LocalDate and LocalTime:

public static void main(String[] args) {
    System.out.println(Instant.ofEpochMilli(new Date().getTime())
            .atZone(ZoneId.systemDefault())
            .toLocalDate());
    System.out.println(Instant.ofEpochMilli(new Date().getTime())
            .atZone(ZoneId.systemDefault())
            .toLocalTime()
        );
}
Copy the code

LocalDate converts to Date:

public Date convertLocalDateToDate(LocalDate localDate) {
    return java.sql.Date.valueOf(localDate);
}
Copy the code

LocalDateTime is converted to Date

public Date convertLocalDateTimeToDate(LocalDateTime localDateTime) {
    return java.sql.Timestamp.valueOf(localDateTime);
}
Copy the code

Get the timestamp of the current time (in seconds) :

public static long getNowSecond() {
    return LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
}
Copy the code

Four, reference

Coordinated Universal Time ISO_8601