LocalDate, LocalTime, LocalDateTime

  • LocalDate,LocalTime,LocalDateTimeInstances of the class are immutable objects representing the date, time, date, and time using the ISO-8601 calendar system, respectively.
     @Test
      public void localDateTimeTest(a) {
        // Get the current date and time
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now); / / the T10:2021-03-21 saith. 018324300
        // Set the specified date and time to get a new LocalDateTime
        LocalDateTime dateTime = LocalDateTime.of(2021.3.20.23.27.0);
        System.out.println(dateTime);  // 2021-03-20T23:27
        / / modify LocalDateTime
        LocalDateTime plusYears = dateTime.plusYears(10);
        System.out.println(plusYears);  // 2031-03-20T23:27
        LocalDateTime plusMonths = dateTime.plusMonths(10);
        System.out.println(plusMonths);  // 2022-01-20T23:27
        LocalDateTime plusDays = dateTime.plusDays(10);
        System.out.println(plusDays);  // 2021-03-30T23:27
        LocalDateTime plusHours = dateTime.plusHours(10);
        System.out.println(plusHours);  // 2021-03-21T09:27
        LocalDateTime plusMinutes = dateTime.plusMinutes(10);
        System.out.println(plusMinutes);  // 2021-03-20T23:37
        LocalDateTime plusSeconds = dateTime.plusSeconds(10);
        System.out.println(plusSeconds);  // 2021-03-20T23:27:10
    
        LocalDateTime minusDays = dateTime.minusDays(10);
        System.out.println(minusDays);  // 2021-03-10T23:27
    
        LocalDateTime plus = dateTime.plus(Duration.ofDays(1));
        System.out.println(plus);  // 2021-03-21T23:27
    
        LocalDateTime withDayOfMonth = dateTime.withDayOfMonth(1);  // Change to the first day of the month
        System.out.println(withDayOfMonth);  // 2021-03-01T23:27
        LocalDateTime withDayOfYear = dateTime.withDayOfYear(1);  // Change to the first day of the year
        System.out.println(withDayOfYear);  // 2021-01-01T23:27
        LocalDateTime withMonth = dateTime.withMonth(1);  // Change the month to January, other unchanged
        System.out.println(withMonth);  // 2021-01-20T23:27
        LocalDateTime withYear = dateTime.withYear(1);  // Change the year
        System.out.println(withYear);  // 0001-03-20T23:27
        // Get information
        int dayOfMonth = dateTime.getDayOfMonth();  // Get the current date
        System.out.println(dayOfMonth);  / / 20
        int dayOfYear = dateTime.getDayOfYear();  // Get the current day of the year
        System.out.println(dayOfYear);  / / 79
        DayOfWeek dayOfWeek = dateTime.getDayOfWeek();  // Get the day of the week
        System.out.println(dayOfWeek);  // SATURDAY
        Month month = dateTime.getMonth();  // Get the month
        System.out.println(month);  // MARCH
        int monthValue = dateTime.getMonthValue();  // Get the month int value
        System.out.println(monthValue);    / / 3
        int year = dateTime.getYear();  // Get the year
        System.out.println(year);  / / 2021
    
        // Compare dates
        boolean after = dateTime.isAfter(now);
        System.out.println(after);  // false
        boolean before = dateTime.isBefore(now);
        System.out.println(before);  // true
      }
    Copy the code

    The same applies to LocalTime and LocalDate

Instant timestamp

  • It is based on a description of what happened in the first year of Unix (traditionally set at midnight on January 1, 1970 in the UTC time zone)
      @Test
      public void InstantTest(a) {
        Instant now = Instant.now();  // According to standard time, not local time
        System.out.println(now);  / / the 2021-03-21 T05:22:02. 162907300 z
    
        OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);  / / the T13:2021-03-21 22:02. 162907300 + 08:00
    
        long epochSecond = now.getEpochSecond();  // How many seconds have been offset since 1970
        System.out.println(epochSecond);  / / 1616304122
    
        Instant ofEpochSecond = Instant.ofEpochSecond(1);  // Since 1970, offset by one second
        System.out.println(ofEpochSecond);  // 1970-01-01T00:00:01Z
      }
    Copy the code

Duration and Period

  • Duration: used to calculate two “time” intervals; Period: Used to calculate two “date” intervals
      @Test
      public void DurationTest(a) {
        Instant now = Instant.now();
        try {
          Thread.sleep(100);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        Instant now1 = Instant.now();
        // Calculate the time difference
        Duration duration = Duration.between(now, now1);
        System.out.println(duration);  / / PT0.1126982 S
    
        LocalDateTime now2 = LocalDateTime.now();
        try {
          Thread.sleep(100);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        LocalDateTime now3 = LocalDateTime.now();
        // Calculate the time difference
        Duration between = Duration.between(now2, now3);
        System.out.println(between);  / / PT0.1067149 S
    
        // Calculate the date difference
        Period between1 = Period.between(now2.toLocalDate(), now3.toLocalDate());
        System.out.println(between1);
    
        Period between2 = Period.between(LocalDate.of(2010.10.1), LocalDate.now());
        System.out.println(between2);
      }
    Copy the code

4. Date manipulation

  • TemporalAdjuster: Time corrector. Sometimes we may need to get. For example, change the date to next Sunday
  • TemporalAdjusters: This class provides a lot of common usage through static methodsTemporalAdjusterThe implementation of the
      @Test
      public void temporalAdjusterTest(a) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime nextDayOfWeekFRIDAY = now.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));  / / next Friday
        System.out.println(nextDayOfWeekFRIDAY);
    
        LocalDateTime firstDayOfMonth = now.with(TemporalAdjusters.firstDayOfMonth());  // The first day of the month
        System.out.println(firstDayOfMonth);
      }
    Copy the code

Parse and format

  • java.time.format.DateTimeFormatterClass: This class provides three formatting methods:
    • Predefined standard formats
    • Locale-specific formats
    • Custom format
      @Test
      public void DateTimeFormatterTest(a) {
        DateTimeFormatter isoWeekDate = DateTimeFormatter.ISO_LOCAL_DATE;  // Predefined standard format
        LocalDateTime now = LocalDateTime.now();
        String format = now.format(isoWeekDate);  // Convert to String
        System.out.println(format);
    
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");  // Define the format
        String format1 = now.format(formatter);  // Convert to String
        System.out.println(format1);
    
        LocalDateTime parse = LocalDateTime.parse("The 2020-01-01 10:10:10", formatter);  / / parsing
        System.out.println(parse);
      }
    Copy the code

Six, time zone processing

  • Java8 has added support for time zones, with time zones as follows:ZonedDate,ZonedTime,ZonedDateTime
  • Each time zone corresponds to an ID. The region ID is in the format of {region}/{city}, for example:Asia/Shanghai
  • ZoneId: This class contains all time zone information
    • getAvailableZoneIds(): You can obtain the time zone information of all time zones
    • of(id): Obtain the value with the specified time zone informationZoneIdobject
      @Test
      public void ZoneIdTest(a) {
        Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
        availableZoneIds.forEach(System.out::println);  // Get all time zones
    
        LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        System.out.println(now);  / / the 2021-03-21 T13: time. 184581100
        ZonedDateTime now1 = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
        System.out.println(now1);  // 2021-03-21T13:17:21.184581100+08:00[Asia/Shanghai] Bring the local time of the time zone
      }
    Copy the code