Java 8 introduced LocalDate, LocalTime, LocalDateTime this three time processing class, in order to make up for the previous date and time class deficiencies, simplify the operation of date and time.

The Java8 date and time classes include LocalDate, LocalTime, Instant, Duration, and Period, all of which are included in the java.time package


Prior to Java8, the classes that handled Date and time were Date and Calendar.

  • The java.util.Date and java.util.Calendar classes are not easy to use, do not support time zones, and neither is thread-safe;
  • The DateFormat class for formatting dates is in the java.text package. It is an abstract class, so we need to instantiate a SimpleDateFormat object to handle date formatting, and DateFormat is also non-thread-safe. This means that if you call the same DateFormat object in a multithreaded program, you will get unexpected results.
  • The calculation of dates is tedious and error-prone, because months start at 0 and need to be incremented by one to represent the current month from the Calendar.

There are free tutorials on Flutter on watermelon video, updated daily. Please pay attention to them and receive alerts

[x1] Click to view the prompts

【 X2 】 Various series of tutorials


1 Java8 Obtains the current time data

The now() methods of LocalDate and LocalDateTime use the system default time zone.

@SpringBootTest
class DemoDateTests {

  / / log
  private static final Logger LOG = LoggerFactory.getLogger(DemoDateTests.class);

  @Test
  void test(a) {
    // Only the date of the current time zone is retrieved
    LocalDate localDate = LocalDate.now();
    LOG.info("localDate: " + localDate);
    //localDate: 2020-09-23
    
    // Get the current time only
    LocalTime localTime = LocalTime.now();
    LOG.info("localTime: " + localTime);
    / / a localTime: 23:41:43. 991

    // Get the date and time of progress
    LocalDateTime localDateTime2 = LocalDateTime.now();
    LOG.info("localDateTime2: " + localDateTime2);
    / / localDateTime2: the 2020-09-23 T23:41:43. 991}}Copy the code

The time obtained here is 2020-09-23T23:41:43.991 with a T in the middle refers to UTC time.

Coordinated Universal Time (UTC) is the Time in the zero hour zone.

Another common abbreviation is GMT, Greenwich Mean Time. Greenwich Is in the zero time zone, so UTC and GMT are numerically the same.

In 1884, the International Longitude Conference divided the earth’s surface into 24 equal zones, called time zones. That is, with the prime meridian as the benchmark, the east and west longitude 7.5 degrees of the range as the zero hour zone, and then every 15 degrees for the one hour zone, each time zone difference of one hour, Beijing time zone for the east eight area. For example: Beijing time 2020-09-23 12:00:00 then utc time is 2020-09-23 04:00:00;

The time format obtained may not be the one we want, so we can use DateTimeFormatter to format it as follows:

 @Test
 void test1(a) {
   
   // Generate this object using static methods
   LocalDateTime localDateTime = LocalDateTime.now();
   LOG.info("localDateTime: " + localDateTime);
   / / / localDateTime: the 2020-09-23 T23:47:41. 752
   

   // Format the time
   DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss");
   // Time after formatting
   String formatDate = localDateTime.format(formatter);

   LOG.info("Time after formatting:" + formatDate);
   // After formatting time: 2020-09-23 23:47:41
 }
Copy the code
1.1 LocalDate

The LocalDate class represents a specific date, but does not contain a specific time, nor does it contain time zone information. An instance can be created using the static of() method of LocalDate, which also contains methods to get the year, month, day, day, and so on.

@Test
 void test3(a) {
   // You can also use the current time
   //LocalDate localDate = LocalDate.now();
   // Initialize a date: 2020-09-24
   LocalDate localDate = LocalDate.of(2020.9.24);    
    
   int year = localDate.getYear();                     // Year: 2020
   Month month = localDate.getMonth();                 // Month: SEPTEMBER
   int dayOfMonth = localDate.getDayOfMonth();         // Day of the month: 24
   DayOfWeek dayOfWeek = localDate.getDayOfWeek();     // Day of the week: THURSDAY
   int length = localDate.lengthOfMonth();             // Days of the month: 30
   boolean leapYear = localDate.isLeapYear();          // Leap year: true
 }
Copy the code

The breakpoint unit test runs as follows

1.2 a LocalTime

LocalTime is similar to LocalDate except that LocalDate does not contain a specific time, whereas LocalTime contains a specific time

@Test
void test4(a) {
  // The current time
  LocalTime localTime = LocalTime.now();
  
  int hour = localTime.getHour();       // 时:07
  int minute = localTime.getMinute();   / / points: 22
  int second = localTime.getSecond();   / / s: 20
}
Copy the code

The breakpoint unit test runs as follows

1.3 LocalDateTime

The LocalDateTime class is a combination of LocalDate and LocalTime and can be created directly with the of() method, You can also combine a LocalDate or LocalTime into a LocalDateTime by calling the atTime() method of LocalDate or the atDate() method of LocalTime.

@Test
void test5(a) {
  
  // create LocalDateTime with the of method
  LocalDateTime localDateTime = LocalDateTime.of(
      2020./ / year
      Month.JANUARY,/ / month
      4./ /,
      / / SEC
      17.23.52);

}
Copy the code

Conversion of LocalDate and LocalTime to LocalDateTime is the process of combining the two, as shown below:

  @Test
  void test6(a) {


    // Create LocalDate with the of method
    // The parameter is year month day
    LocalDate localDate = LocalDate.of(2020, Month.JANUARY, 4);

    // Create LocalTime with the of method
    // The parameter is minutes and seconds
    LocalTime localTime = LocalTime.of(07.23.52);

    / / LocalDate LocalDateTime
    LocalDateTime localDateTime = localDate.atTime(localTime);

    // LocalTime  转 LocalDateTime
    LocalDateTime localDateTime1 = localTime.atDate(localDate);

  }
Copy the code

The breakpoint unit test runs as follows

You can also reverse LocalDateTime to LocalDate or LocalTime, as shown below:


LocalDate date = localDateTime.toLocalDate();

LocalTime time = localDateTime.toLocalTime();

Copy the code
LocalDateTime and milliseconds

The time stamp refers to the total number of seconds from 00 00 00 00 00 00 00 GMT on 01 01 1970 to the present day

ZoneId region ZoneOffset Offset data

 @Test
 void test2(a) {

   // Generate this object using static methods
   LocalDateTime localDateTime = LocalDateTime.now();
   LOG.info("localDateTime: " + localDateTime);
   / / / localDateTime: the 2020-09-23 T23: leave. 832

   // Convert to timestamp (seconds)
   long epochSecond = localDateTime.toEpochSecond(ZoneOffset.of("+ 8"));
   // convert to milliseconds
   long epochMilli = localDateTime.atZone(ZoneOffset.systemDefault()).toInstant().toEpochMilli();

   LOG.info("Time stamp :(seconds)"   + epochSecond + "; (ms):" + epochMilli);
   /// the timestamp is :(seconds) 1600876151; (ms): 1600876151832



   // The timestamp (milliseconds) is converted to LocalDateTime
   Instant instant = Instant.ofEpochMilli(epochMilli);
   LocalDateTime localDateTime3 = LocalDateTime.ofInstant(instant, ZoneOffset.systemDefault());
  
   // The timestamp (s) is converted to LocalDateTime
   Instant instant2 = Instant.ofEpochSecond(epochSecond);
   LocalDateTime localDateTime4 = LocalDateTime.ofInstant(instant2, ZoneOffset.systemDefault());
  

 }
Copy the code
LocalDateTime and Date

The Instant class represents a time and is nanosecond accurate, similar to system.currentTimemillis (), but Instant is nano-second accurate. The system.currentTimemillis () method is only accurate to milliseconds (Mili-second).

If a nanosecond is used to represent a time, the original bit of Long is not enough. It takes up a bit more storage space, and actually consists of two Long fields inside.

Note that Instant represents a time and does not include the concept of a time zone.

  // Date is converted to LocalDateTime
  public static LocalDateTime dateToLocalDate(Date date) {
    / / Date nanoseconds
    Instant instant = date.toInstant();
    // Obtain the default system time zone
    ZoneId zoneId = ZoneId.systemDefault();
    / / conversion
    return instant.atZone(zoneId).toLocalDateTime();
  }


  // LocalDateTime converts to Date
  public static Date localDateTimeToDate(LocalDateTime localDateTime) {
    ZoneId zoneId = ZoneId.systemDefault();
    ZonedDateTime zdt = localDateTime.atZone(zoneId);
    return Date.from(zdt.toInstant());
  }
Copy the code
 // Date Converts millisecond data to LocalDateTime
 public static LocalDateTime dateToLocalDateMil(Long datemilli) {
   // Convert millisecond data to nanosecond
   Instant instant = Instant.ofEpochMilli(datemilli);
   ZoneId zoneId = ZoneId.systemDefault();
   return instant.atZone(zoneId).toLocalDateTime();
 }

Copy the code

Of course Instant has a lot going for it

Instant uses two constants internally, seconds for the number of seconds since 1970-01-01 00:00:00 and nanos for the nanosecond part (nanos does not exceed 999,999,999).

In addition to using the now() method, Instant can also be created using the ofEpochSecond method:

@Test
void test7(a) {

  // Parameter 1 is second, parameter 2 is nanosecond,
  // The code here represents the time of 100,000 nanoseconds three minutes after 1970-01-01 00:00:00
  Instant instant = Instant.ofEpochSecond(180.100000);

  LOG.info("Time is :"   + instant );

}
Copy the code

The breakpoint unit test runs as follows

4 Duration class

The internal implementation of Duration is similar to Instant in that it has two parts: seconds for seconds and nanos for nanoseconds. The difference is that Instant is used to represent a timestamp (or point in time), while Duration represents a time period, so the Duration class does not contain the now() static method. A Duration object can be created using the duration.between () method.

@Test
 void test8(a) {

   /// start time
   LocalDateTime from = LocalDateTime.of(2020, Month.JANUARY, 9.10.7.0);    / / the 2017-01-05 10:07:00
   // End time
   LocalDateTime to = LocalDateTime.of(2020, Month.FEBRUARY, 10.10.7.0);

   // indicates the interval between 2020-09-05 10:07:00 and 2020-09-05 10:07:00
   Duration duration = Duration.between(from, to);

   long days = duration.toDays();              // Total number of days during this period
   long hours = duration.toHours();            // The number of hours during this period
   long minutes = duration.toMinutes();        // The number of minutes during this period
   long seconds = duration.getSeconds();       // The number of seconds during this period
   long milliSeconds = duration.toMillis();    // The number of milliseconds during this period
   long nanoSeconds = duration.toNanos();      // The number of nanoseconds during this period


   LOG.info("Time is :"   + duration );

 }
Copy the code

The breakpoint unit test runs as follows

5 Period which

Period is conceptually similar to Duration, except that a Period is measured in years, months, and days, such as 1 year, 3 months, and 6 days (Listing 5-1).

Period objects can be created using constructors and between() methods. It is worth noting that since Period measures time in years, months and days, the between() method can only accept arguments of type LocalDate.

// Listing 5-1
 @Test
 void test9(a) {

   //1 year, 3 months and 6 days
   Period period = Period.of(1.3.6);

   // 2017-01-05 to 2017-02-05
   Period period2 = Period.between(
       LocalDate.of(2020.1.5),
       LocalDate.of(2020.2.5));

   LOG.info("Time is :"   + period );
 }
Copy the code

The breakpoint unit test runs as follows

6 Time operations before and after the specified date

// Listing 6-1
@Test
void test10(a) {
   // Use the current time
  LocalDate localDate = LocalDate.now();

  LocalDate date4 = localDate.plusYears(1);                // Add one year 2021-09-24
  LocalDate date5 = localDate.minusMonths(2);              // Reduce by 2 months 2021-07-24
  LocalDate date6 = localDate.plus(5, ChronoUnit.DAYS);    // Add 5 days 2021-09-29

  LOG.info("Time is :"   + date6 );
}
Copy the code

The breakpoint unit test runs as follows

7 Formatting dates

The new date API provides a DateTimeFormatter class (also used above) for handling date formatting operations. It is included in the java.time.format package. Java 8’s date class has a format() method for formatting dates as strings. This method receives and processes a DateTimeFormatter type parameter

// Listing 7-1
@Test
void test11(a) {
  // Use the current time
  LocalDateTime dateTime = LocalDateTime.now();


  String strDate1 = dateTime.format(DateTimeFormatter.BASIC_ISO_DATE);    / / 20200924
  String strDate2 = dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE);    / / 2020-09-24
  String strDate3 = dateTime.format(DateTimeFormatter.ISO_LOCAL_TIME);    / / 08:23:31. 463
  String strDate4 = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));   / / 2020-09-24

  LOG.info("Time is :"   + dateTime );
}
Copy the code

The breakpoint unit test runs as followsParse the existing date string into LocalDateTime, LocalDate, LocalTime as shown in Listing 7-2.

The idea behind parsing is to define the format mapping DateTimeFormatter and then call the corresponding parse method

// Listing 7-2
@Test
void test12(a) {

  String strDate = "2020-09-24";
  String strDate2 = "The 2020-09-24 08:23:31";

  LocalDate date = LocalDate.parse(strDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
  
  LocalTime time = LocalTime.parse(strDate, DateTimeFormatter.ofPattern(" HH:mm:ss"));

  / / define the formatter
  DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  // Parse operations
  LocalDateTime dateTime = LocalDateTime.parse(strDate2, dateTimeFormatter);

  LOG.info("Time is :"   + dateTime );

}
Copy the code

The breakpoint unit test runs as follows


Of course, there are also public accounts, interested partners can pay attention to