No.3.1_ 28_ 5. Detailed explanation of ChronoField class of jdk8 new feature [Date]

Posted by myfafa on Fri, 11 Feb 2022 02:00:13 +0100

Related links

5. Detailed explanation of new API

id=24 ChronoField time bound

idClass nameeffect
24 Enum ChronoField
Time bound
  all methods in the class will return a TemporalAdjuster object.
   represents some common time nodes for time calculation. (commonly used g. date adjuster with: as a parameter of *. With, the time is calculated according to some special rules, and the get method is transformed downward)
  for example: nanosecond (in seconds) (ChronoField.NANO_OF_SECOND) value range [0, 9999999]
       nanosecond (in day) (ChronoField.NANO_OF_DAY) value range [0, 8639999999999], getLong method is required
     microseconds (in seconds) (ChronoField.MICRO_OF_SECOND) value range [0, 999999]
       microseconds (in days) (ChronoField.MICRO_OF_DAY) value range [0, 8639999999], getLong method needs to be used
       milliseconds (in seconds) (ChronoField.MILLI_OF_SECOND) value range [0, 999]
       milliseconds (in days) (ChronoField.MILLI_OF_DAY) value range [0, 86399999]
      seconds (in minutes) (ChronoField.SECOND_OF_MINUTE) value range [0, 59]
                           81
      seconds (seconds offset from UTC / Greenwich) (ChronoField.OFFSET_SECONDS) value range [- 64800, 64800]
       note: the object must contain time zone information (OffsetDateTime, ZonedDateTime)
       example: long offsetsecond = zoneddatetime of(ldt, ZoneId.of(“Asia/Shanghai”)). getLong(ChronoField.OFFSET_SECONDS); => Obtain the offset seconds of China's time zone "29143" ≈ 8.059 hours, i.e. East Zone 8
                                 
        LONG.MIN_VALUE = 0x8000000000000000L = - 264
        LONG.MAX_VALUE = 0x7fffffffffffffffL = 264 - 1
       note: the object must contain time zone information (OffsetDateTime, ZonedDateTime)
         minutes (in the middle of the day) (ChronoField.MINUTE_OF_DAY) value range [0, 1439]
       minutes (in hours) (ChronoField.MINUTE_OF_HOUR) value range [0, 59]
      12 hour system (0 represents 0:00AM or 0:00PM of the day) (ChronoField.HOUR_OF_AMPM) value range [0, 11]
      12 hour system (12 represents 0:00AM or 0:00PM of the day) (ChronoField.CLOCK_HOUR_OF_AMPM) value range [1, 12]
       2022-02-01T01:00. With (chronofield. Clock_hour_of_ampm, 5) = > 2022-02-01t05:00
       2022-02-01T14:00. With (chronofield. Clock_hour_of_ampm, 5) = > 2022-02-01t17:00
      24-hour system (0 represents 0 o'clock of the day) (ChronoField.HOUR_OF_DAY) value range [0, 23]
      24-hour system (24 represents 0:00 of the day) (ChronoField.CLOCK_HOUR_OF_DAY) value range [1, 24]
     half day (AM=0,PM=1) (ChronoField.AMPM_OF_DAY) value range [0,1]
                      day (in the middle of the week) value range [1,7]
The value range of                              
The value range of                              
The value range of                                    
       Year.MIN_VALUE = -999999999;   = >   minimum   year MIN_ VALUE * 365.25 = -365249999634
       Year.MAX_VALUE = 999999999;   = >   maximum   year MAX_ VALUE * 365.25 = 365249999634
       day of the week (month alignment 3) (ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH) value range [1, 7]
       week (year alignment 4) (ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR) value range [1, 7]
                                    
       week (year alignment) (ChronoField.ALIGNED_WEEK_OF_YEAR) value range [1, 53]
      month (in the middle of the year) (ChronoField.MONTH_OF_YEAR) value range [1, 12]
The value range of                                   8195
       Year.MIN_VALUE = -999999999;   = >   minimum   year MIN_ VALUE * 12L = 11999999988
       Year.MAX_VALUE = 999999999;   = >   maximum   year MAX_ VALUE * 12L + 11 = 11999999999
The value range of                              81
       Year.MIN_VALUE = -999999999;   = >   minimum value
       Year.MAX_VALUE = 999999999;   = >   maximum
The value range of                             
          ① time before 0001-01-01T00:00 (including the present moment),   belongs to BC, and the value range is [1, year. Max_value + 1] = > [1, 1000000000]
         ② the time after 0001-01-01T00:00 (excluding the present moment) belongs to BC, and the value range is [1, year. Max_value] = > [1, 9999999]
          the reason is that the year ∈ [- 9999999999, 0] is BC. The year ∈ [1, 99999999] is after A.D. In terms of time range, it is 1 year more before B.C. than after B.C.
       era (BC = 0, BC = 1) (ChronoField.ERA) value range [0, 1], example (pseudo code):
       2022-02-01T00:00. With (chronofield. Epoch_day, 0) = > - 2022-02-01t00:00 (becomes negative)
       2022-02-01T00:00. With (chronofield. Epoch_day, 1) = > 2022-02-01t00:00 (unchanged)
  example (pseudo code): 2022-01-01T02:03:04
      "2022-01-01T02:03:04".get(ChronoField.MINUTE_OF_DAY); => 123
      "2022-01-01T02:03:04".getLong(ChronoField.MINUTE_OF_HOUR);; => 3
--------------------------------------------------------------------------------------
  19.TemporalAdjusters and 24 Chronofield time boundary can be used as the with parameter to modify the time (of course, the Chinese names of these two are compiled by myself, and there are too few online materials). The difference is:
  the modification date of TemporalAdjusters is based on the current time, and the rules are relatively simple (for example, last Monday, the first Friday of the current month, the fifth day of next year, etc.).
   the modification time of ChronoField is based on a new time, and the rules are complex (for example, ChronoField.SECOND_OF_DAY: first obtain the day according to the current time, discard the information of hours and minutes, and then set a new second based on this time)
      = > value range of seconds (ChronoField.SECOND_OF_DAY) [0, 86399]. The following is a line of pseudo code to understand what is "based on a new time"
      => 2022-01-01T02:03:04.000056789.with(ChronoField.SECOND_OF_DAY, 8) => 2022-01-01T00:00:08.000056789
    2022-01-01t02:03:04.000056789 = > get the information of the day first   = > 2022-01-01t00:00:00.000056789 (information less than seconds is still retained)
  2022-01-01t00:00:00.000056789 = > assign it second = 8 = > 2022-01-01t00:00:08.000056789

  1   UNIX timestamp: Unix timestamp is the number of seconds elapsed since January 1, 1970 (midnight of UTC/GMT), regardless of leap seconds.

  2   era time: refers to the starting point at midnight in Greenwich on January 1, 1970. The definition of era day is related to the development history of UNIX.

  3   month alignment: redefine a new rule about the day of the week. Rule Description: the first day of each month is Monday (1.1 / 2.1 /... 12.1). For example, "February 1, 2022" is Tuesday, but under the new rule of aligned_xxxx_month, "February 1, 2022" is Monday

  4   year alignment: redefine a new rule about the day of the week. Rule Description: January 1 of each year is Monday, for example, "January 1, 2022" is Saturday, but under the new rule of aligned_xxxx_year, "January 1, 2022" is Monday

  5   ad: AD, the Gregorian calendar, is a dating method derived from western society. Originally known as the Christian era, also known as the Western calendar or the Western era.

Seconds * 10


Nanosecond (in seconds) (ChronoField.NANO_OF_SECOND) value range [0, 9999999]
Nanosecond (in days) (ChronoField.NANO_OF_DAY) value range [0, 86399999999], getLong method is required
Microseconds (in seconds) (ChronoField.MICRO_OF_SECOND) value range [0, 999999]
Microseconds (in days) (ChronoField.MICRO_OF_DAY) value range [0, 8639999999], getLong method is required
Milliseconds (in seconds) (ChronoField.MILLI_OF_SECOND) value range [0, 999]
Milliseconds (in days) (ChronoField.MILLI_OF_DAY) value range [0, 86399999]
Seconds (in minutes) (ChronoField.SECOND_OF_MINUTE) value range [0, 59]
Seconds (in days) (ChronoField.SECOND_OF_DAY) value range [0, 86399]
Seconds (seconds offset from UTC / Greenwich) (ChronoField.OFFSET_SECONDS) value range [- 64800, 64800]
  note: the object must contain time zone information (OffsetDateTime, ZonedDateTime)
  example: long offsetsecond = zoneddatetime of(ldt, ZoneId.of(“Asia/Shanghai”)). getLong(ChronoField.OFFSET_SECONDS); => Obtain the offset seconds of China's time zone "29143" ≈ 8.059 hours, i.e. East Zone 8
Second (Unix timestamp 1) (chronofield. Install_seconds) value range [Long.MIN_VALUE, Long.MAX_VALUE], you need to use getLong method, where:
 LONG.MIN_VALUE = 0x8000000000000000L = - 264
 LONG.MAX_VALUE = 0x7fffffffffffffffL = 264 - 1
  note: the object must contain time zone information (OffsetDateTime, ZonedDateTime)

/*
2022-01-01T02:03:04.000056789
56789 => Nanosecond (in seconds) parameter: chronofield NANO_ OF_ SECOND
7384000056789 => Nanosecond (in days) parameter: chronofield NANO_ OF_ DAY
56 => Microsecond (in seconds) parameter: chronofield MICRO_ OF_ SECOND
7384000056 => Microsecond (in days) parameter: chronofield MICRO_ OF_ DAY
0 => Milliseconds (in seconds) parameter: chronofield MILLI_ OF_ SECOND
7384000 => Milliseconds (in days) parameter: chronofield MILLI_ OF_ DAY
4 => Second (in minutes) parameter: chronofield SECOND_ OF_ MINUTE
7384 => Second (in days) parameter: chronofield SECOND_ OF_ DAY
28800 => Seconds (seconds offset from UTC / Greenwich) parameter: chronofield OFFSET_ SECONDS
1640973784 => Second (Unix timestamp) parameter: chronofield INSTANT_ SECONDS
*/
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.of(2022, 1, 1, 2, 3, 4, 56789);
        System.out.println(ldt);
        
        int i1 = ldt.get(ChronoField.NANO_OF_SECOND);
        System.out.println(i1 + " => nanosecond(In seconds) Parameters: ChronoField.NANO_OF_SECOND");

        //You need to use the getLong method
        long l2 = ldt.getLong(ChronoField.NANO_OF_DAY);
        System.out.println(l2 + " => nanosecond(In the sky) Parameters: ChronoField.NANO_OF_DAY");

        int i3 = ldt.get(ChronoField.MICRO_OF_SECOND);
        System.out.println(i3 + " => Microsecond(In seconds) Parameters: ChronoField.MICRO_OF_SECOND");

        //You need to use the getLong method
        long l4 = ldt.getLong(ChronoField.MICRO_OF_DAY);
        System.out.println(l4 + " => Microsecond(In the sky) Parameters: ChronoField.MICRO_OF_DAY");

        int i5 = ldt.get(ChronoField.MILLI_OF_SECOND);
        System.out.println(i5 + " => millisecond(In seconds) Parameters: ChronoField.MILLI_OF_SECOND");

        int i6 = ldt.get(ChronoField.MILLI_OF_DAY);
        System.out.println(i6 + " => millisecond(In the sky) Parameters: ChronoField.MILLI_OF_DAY");

        int i7 = ldt.get(ChronoField.SECOND_OF_MINUTE);
        System.out.println(i7 + " => second(In the score) Parameters: ChronoField.SECOND_OF_MINUTE");

        int i8 = ldt.get(ChronoField.SECOND_OF_DAY);
        System.out.println(i8 + " => second(In the sky) Parameters: ChronoField.SECOND_OF_DAY");

        //The object must contain time zone information (OffsetDateTime, ZonedDateTime)
        ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.of("+08:00"));
        int i9 = zdt.get(ChronoField.OFFSET_SECONDS);
        System.out.println(i9 + " => second(from UTC/Greenwich offset seconds) Parameters: ChronoField.OFFSET_SECONDS");

        //The object must contain time zone information (OffsetDateTime, ZonedDateTime), and the getLong method is required
        long l10 = zdt.getLong(ChronoField.INSTANT_SECONDS);
        System.out.println(l10 + " => second(Unix time stamp) Parameters: ChronoField.INSTANT_SECONDS");
    }
/*
2022-01-01T02:03:04.000056789
2022-01-01T02:03:04.000000001 => Nanosecond (in seconds) parameter: chronofield NANO_ OF_ SECOND, 1
2022-01-01T00:00:00.000000002 => Nanosecond (in days) parameter: chronofield NANO_ OF_ DAY, 2
2022-01-01T02:03:04.000003 => Microsecond (in seconds) parameter: chronofield MICRO_ OF_ SECOND, 3
2022-01-01T00:00:00.000004 => Microsecond (in days) parameter: chronofield MICRO_ OF_ DAY, 4
2022-01-01T02:03:04.005 => Milliseconds (in seconds) parameter: chronofield MILLI_ OF_ SECOND, 5
2022-01-01T00:00:00.006 => Milliseconds (in days) parameter: chronofield MILLI_ OF_ DAY, 6
2022-01-01T02:03:07.000056789 => Second (in minutes) parameter: chronofield SECOND_ OF_ MINUTE, 7
2022-01-01T00:00:08.000056789 => Second (in days) parameter: chronofield SECOND_ OF_ DAY, 8
2022-01-01T02:03:04.000056789+08:00 => Seconds (seconds offset from UTC / Greenwich) parameter: chronofield OFFSET_ SECONDS, 9
1970-01-01T08:00:10.000056789+08:00 => Second (Unix timestamp) parameter: chronofield INSTANT_ SECONDS, 10
*/
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.of(2022, 1, 1, 2, 3, 4, 56789);
        System.out.println(ldt);
        
        //Nanosecond (in seconds) (ChronoField.NANO_OF_SECOND) value range [0, 9999999]
        LocalDateTime ldt1 = ldt.with(ChronoField.NANO_OF_SECOND, 1);
        System.out.println(ldt1 + " => nanosecond(In seconds)1.NANO_OF_SECOND");

        //Nanosecond (in days) (ChronoField.NANO_OF_DAY) value range [0, 86399999999]
        LocalDateTime ldt2 = ldt.with(ChronoField.NANO_OF_DAY, 2);
        System.out.println(ldt2 + " => nanosecond(In the sky)2.NANO_OF_DAY");

        //Microseconds (in seconds) (ChronoField.MICRO_OF_SECOND) value range [0, 999999]
        LocalDateTime ldt3 = ldt.with(ChronoField.MICRO_OF_SECOND, 3);
        System.out.println(ldt3 + " => Microsecond(In seconds)3.MICRO_OF_SECOND");

        //Microseconds (in days) (ChronoField.MICRO_OF_DAY) value range [0, 8639999999]
        LocalDateTime ldt4 = ldt.with(ChronoField.MICRO_OF_DAY, 4);
        System.out.println(ldt4 + " => Microsecond(In the sky)4.MICRO_OF_DAY");

        //Milliseconds (in seconds) (ChronoField.MILLI_OF_SECOND) value range [0, 999]
        LocalDateTime ldt5 = ldt.with(ChronoField.MILLI_OF_SECOND, 5);
        System.out.println(ldt5 + " => millisecond(In seconds) 5.MILLI_OF_SECOND");

        //Milliseconds (in days) (ChronoField.MILLI_OF_DAY) value range [0, 86399999]
        LocalDateTime ldt6 = ldt.with(ChronoField.MILLI_OF_DAY, 6);
        System.out.println(ldt6 + " => millisecond(In the sky) 6.MILLI_OF_DAY:");

        //Seconds (in minutes) (ChronoField.SECOND_OF_MINUTE) value range [0, 59]
        LocalDateTime ldt7 = ldt.with(ChronoField.SECOND_OF_MINUTE, 7);
        System.out.println(ldt7 + " => second(In the score)7.SECOND_OF_MINUTE:");

        //Seconds (in days) (ChronoField.SECOND_OF_DAY) value range [0, 86399]
        LocalDateTime ldt8 = ldt.with(ChronoField.SECOND_OF_DAY, 8);
        System.out.println(ldt8 + " => second(In the sky)8.SECOND_OF_DAY:");

        //The object must contain time zone information (OffsetDateTime, ZonedDateTime)
        ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.of("+08:00"));
        //Seconds (seconds offset from UTC / Greenwich) (ChronoField.OFFSET_SECONDS) value range [- 64800, 64800]
        ZonedDateTime zdt9 = zdt.with(ChronoField.OFFSET_SECONDS, 9);
        System.out.println(zdt9 + " => second(from UTC/Greenwich offset seconds)9.OFFSET_SECONDS:");

        //The object must contain time zone information (OffsetDateTime, ZonedDateTime)
        //Second (Unix timestamp 1) (chronofield. Install_seconds) value range [Long.MIN_VALUE, Long.MAX_VALUE]
        ZonedDateTime zdt10 = zdt.with(ChronoField.INSTANT_SECONDS, 10);
        System.out.println(zdt10 + " => second(Unix time stamp)10.INSTANT_SECONDS:");
    }
Minutes * 2


Minutes (in days) (ChronoField.MINUTE_OF_DAY) value range [0, 1439]
Minutes (in hours) (ChronoField.MINUTE_OF_HOUR) value range [0, 59]

/*
2022-01-01T02:03:04.000056789
123 => Minutes (in days) parameter: chronofield MINUTE_ OF_ DAY
3 => Minutes (in hours) parameter: chronofield MINUTE_ OF_ HOUR
*/
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.of(2022, 1, 1, 2, 3, 4, 56789);
        System.out.println(ldt);

        int i1 = ldt.get(ChronoField.MINUTE_OF_DAY);
        System.out.println(i1 + " => minute(In the sky) Parameters: ChronoField.MINUTE_OF_DAY");

        int i2 = ldt.get(ChronoField.MINUTE_OF_HOUR);
        System.out.println(i2 + " => minute(In hours) Parameters: ChronoField.MINUTE_OF_HOUR");
    }
/*
2022-01-01T02:03:04.000056789
2022-01-01T00:01:04.000056789 => Nanosecond (in seconds) parameter: chronofield MINUTE_ OF_ DAY, 1
2022-01-01T02:02:04.000056789 => Nanosecond (in days) parameter: chronofield MINUTE_ OF_ HOUR, 2
*/
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.of(2022, 1, 1, 2, 3, 4, 56789);

        //Minutes (in days) (ChronoField.MINUTE_OF_DAY) value range [0, 1439]
        LocalDateTime ldt1 = ldt.with(ChronoField.MINUTE_OF_DAY, 1);
        System.out.println(ldt1 + " => nanosecond(In seconds) Parameters: ChronoField.MINUTE_OF_DAY, 1");

        //Minutes (in hours) (ChronoField.MINUTE_OF_HOUR) value range [0, 59]
        LocalDateTime ldt2 = ldt.with(ChronoField.MINUTE_OF_HOUR, 2);
        System.out.println(ldt2 + " => nanosecond(In the sky) Parameters: ChronoField.MINUTE_OF_HOUR, 3");
    }
Hours * 4


12 hour system (0 represents 0:00AM or 0:00PM of the day) (ChronoField.HOUR_OF_AMPM) value range [0, 11]
12 hour system (12 represents 0:00AM or 0:00PM of the day) (ChronoField.CLOCK_HOUR_OF_AMPM) value range [1, 12]
 2022-02-01T01:00. With (chronofield. Clock_hour_of_ampm, 5) = > 2022-02-01t05:00
 2022-02-01T14:00. With (chronofield. Clock_hour_of_ampm, 5) = > 2022-02-01t17:00
Value range of 24-hour system (0 represents 0 o'clock of the day) (ChronoField.HOUR_OF_DAY) [0, 23]
24-hour system (24 represents 0:00 of the day) (ChronoField.CLOCK_HOUR_OF_DAY) value range [1, 24]

/*
2022-01-01T14:03:04.000056789
2 => 12 Hour system (counting from 0) parameter: chronofield HOUR_ OF_ AMPM
2 => 12 Hour system (clock number) parameter: chronofield CLOCK_ HOUR_ OF_ AMPM
14 => 24 Hour system (counting from 0) parameter: chronofield HOUR_ OF_ DAY
14 => 24 Hour system (clock number) parameter: chronofield CLOCK_ HOUR_ OF_ DAY
*/
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.of(2022, 1, 1, 14, 3, 4, 56789);
        System.out.println(ldt);
        
        int i1 = ldt.get(ChronoField.HOUR_OF_AMPM);
        System.out.println(i1 + " => 12 Hour system (counting from 0) parameters: ChronoField.HOUR_OF_AMPM");

        int i2 = ldt.get(ChronoField.CLOCK_HOUR_OF_AMPM);
        System.out.println(i2 + " => 12 Hour system (clock number) parameters: ChronoField.CLOCK_HOUR_OF_AMPM");
        
        int i3 = ldt.get(ChronoField.HOUR_OF_DAY);
        System.out.println(i3 + " => 24 Hour system (counting from 0) parameters: ChronoField.HOUR_OF_DAY");

        int i4 = ldt.get(ChronoField.CLOCK_HOUR_OF_DAY);
        System.out.println(i4 + " => 24 Hour system (clock number) parameters: ChronoField.CLOCK_HOUR_OF_DAY");
    }
/*
2022-01-01T15:03:04.000056789
2022-01-01T12:03:04.000056789 => 12 Hourly system (0 represents 0:00AM or 0:00PM of the day) parameter: chronofield HOUR_ OF_ AMPM, 0
2022-01-01T12:03:04.000056789 => 12 Hour system (12 represents 0:00AM or 0:00PM of the day) parameter: chronofield CLOCK_ HOUR_ OF_ AMPM, 12
2022-01-01T00:03:04.000056789 => 24 The current day (chronfield) parameter is 0.0 HOUR_ OF_ DAY, 0
2022-01-01T00:03:04.000056789 => 24 Hour system (24 represents 0:00 of the day) parameter: chronofield CLOCK_ HOUR_ OF_ DAY, 24
*/
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.of(2022, 1, 1, 15, 3, 4, 56789);
        System.out.println(ldt);

        //12 hour system (0 represents 0:00AM or 0:00PM of the day) (ChronoField.HOUR_OF_AMPM) value range [0, 11]
        LocalDateTime ldt1 = ldt.with(ChronoField.HOUR_OF_AMPM, 0);
        System.out.println(ldt1 + " => 12 Hour system (0 represents 0 of the day):00AM Or 0:00PM) Parameters: ChronoField.HOUR_OF_AMPM, 0");

        //12 hour system (12 represents 0:00AM or 0:00PM of the day) (ChronoField.CLOCK_HOUR_OF_AMPM) value range [1, 12]
        LocalDateTime ldt2 = ldt.with(ChronoField.CLOCK_HOUR_OF_AMPM, 12);
        System.out.println(ldt2 + " => 12 Hour system (12 represents the day 0):00AM Or 0:00PM) Parameters: ChronoField.CLOCK_HOUR_OF_AMPM, 12");

        //Value range of 24-hour system (0 represents 0 o'clock of the day) (ChronoField.HOUR_OF_DAY) [0, 23]
        LocalDateTime ldt3 = ldt.with(ChronoField.HOUR_OF_DAY, 0);
        System.out.println(ldt3 + " => 24 Hourly system (0 represents 0:00 of the day) parameters: ChronoField.HOUR_OF_DAY, 0");

        //24-hour system (24 represents 0:00 of the day) (ChronoField.CLOCK_HOUR_OF_DAY) value range [1, 24]
        LocalDateTime ldt4 = ldt.with(ChronoField.CLOCK_HOUR_OF_DAY, 24);
        System.out.println(ldt4 + " => 24 Hourly system (24 represents 0:00 of the day) parameters: ChronoField.CLOCK_HOUR_OF_DAY, 24");
    }
Days * 5


Half day (AM=0,PM=1) (ChronoField.AMPM_OF_DAY) value range [0,1]
Value range of day (in the middle of the week) (ChronoField.DAY_OF_WEEK) [1,7]
The value range of the day (in the middle of the month) (ChronoField.DAY_OF_MONTH) [1,x], where x ∈ [28,31], depends on the number of days in the month
The value range of day (in the middle of the year) (ChronoField.DAY_OF_YEAR) [1,x], where x ∈ [365366], depends on the number of days in the year
The value range of day (from era 2) (ChronoField.EPOCH_DAY) [(Year.MIN_VALUE * 365.25), (Year.MAX_VALUE * 365.25)], you need to use the getLong method, where:
  Year.MIN_VALUE = -999999999;   = >   minimum   year MIN_ VALUE * 365.25 = -365249999634
  Year.MAX_VALUE = 999999999;   = >   maximum   year MAX_ VALUE * 365.25 = 365249999634

/*
2022-02-01T14:03:04.000056789
1 => Half day (AM=0,PM=1) parameter: chronofield AMPM_ OF_ DAY
2 => Day (in the middle of the week) parameter: chronofield DAY_ OF_ WEEK
1 => Day (in the middle of the month) parameter: chronofield DAY_ OF_ MONTH
32 => Day (in the middle of the year) parameter: chronofield DAY_ OF_ YEAR
19024 => Day (from era time) parameter: chronofield EPOCH_ DAY
*/
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.of(2022, 2, 1, 14, 3, 4, 56789);
        System.out.println(ldt);

        int i1 = ldt.get(ChronoField.AMPM_OF_DAY);
        System.out.println(i1 + " => Half a day(AM=0,PM=1) Parameters: ChronoField.AMPM_OF_DAY");

        int i2 = ldt.get(ChronoField.DAY_OF_WEEK);
        System.out.println(i2 + " => day(In the middle of the week) Parameters: ChronoField.DAY_OF_WEEK");

        int i3 = ldt.get(ChronoField.DAY_OF_MONTH);
        System.out.println(i3 + " => day(In the middle of the month) Parameters: ChronoField.DAY_OF_MONTH");

        int i4 = ldt.get(ChronoField.DAY_OF_YEAR);
        System.out.println(i4 + " => day(In the middle of the year) Parameters: ChronoField.DAY_OF_YEAR");
        
        //You need to use the getLong method
        long l5 = ldt.getLong(ChronoField.EPOCH_DAY);
        System.out.println(l5 + " => day(Since the era) Parameters: ChronoField.EPOCH_DAY");
    }
/*
2022-02-01T15:03:04.000056789 It's Tuesday
2022-02-01T03:03:04.000056789 => Half day (AM=0,PM=1) parameter: chronofield AMPM_ OF_ DAY, 0
2022-01-31T15:03:04.000056789 => Day (in the middle of the week) parameter: chronofield DAY_ OF_ WEEK, 1
2022-02-02T15:03:04.000056789 => Day (in the middle of the month) parameter: chronofield DAY_ OF_ MONTH, 2
2022-01-03T15:03:04.000056789 => Day (in the middle of the year) parameter: chronofield DAY_ OF_ YEAR, 3
1970-01-05T15:03:04.000056789 => Day (from era time) parameter: chronofield EPOCH_ DAY, 4
*/
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.of(2022, 2, 1, 15, 3, 4, 56789);
        System.out.println(ldt + "yes" + ldt.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINESE));

        //Half day (AM=0,PM=1) (ChronoField.AMPM_OF_DAY) value range [0,1]
        LocalDateTime ldt1 = ldt.with(ChronoField.AMPM_OF_DAY, 0);
        System.out.println(ldt1 + " => Half a day(AM=0,PM=1) Parameters: ChronoField.AMPM_OF_DAY, 0");

        //Value range of day (in the middle of the week) (ChronoField.DAY_OF_WEEK) [1,7]
        LocalDateTime ldt2 = ldt.with(ChronoField.DAY_OF_WEEK, 1);
        System.out.println(ldt2 + " => day(In the middle of the week) Parameters: ChronoField.DAY_OF_WEEK, 1");

        //The value range of the day (in the middle of the month) (ChronoField.DAY_OF_MONTH) [1,x], where x ∈ [28,31], depends on the number of days in the month
        LocalDateTime ldt3 = ldt.with(ChronoField.DAY_OF_MONTH, 2);
        System.out.println(ldt3 + " => day(In the middle of the month) Parameters: ChronoField.DAY_OF_MONTH, 2");

        //The value range of day (in the middle of the year) (ChronoField.DAY_OF_YEAR) [1,x], where x ∈ [365366], depends on the number of days in the year
        LocalDateTime ldt4 = ldt.with(ChronoField.DAY_OF_YEAR, 3);
        System.out.println(ldt4 + " => day(In the middle of the year) Parameters: ChronoField.DAY_OF_YEAR, 3");

        //Value range of day (from era time) (ChronoField.EPOCH_DAY) [(Year.MIN_VALUE * 365.25), (Year.MAX_VALUE * 365.25)]
        LocalDateTime ldt5 = ldt.with(ChronoField.EPOCH_DAY, 4);
        System.out.println(ldt5 + " => day(Since the era) Parameters: ChronoField.EPOCH_DAY, 4");
    }
Week * 4


Value range of ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH [1, 7]
Value range of ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR [1, 7]
The value range of the week (month alignment) (ChronoField.ALIGNED_WEEK_OF_MONTH) [1,5], where x ∈ [4,5] depends on the number of weeks in the current month (the actual test is also OK if x=5 in February of normal year)
Week number (year alignment) (ChronoField.ALIGNED_WEEK_OF_YEAR) value range [1, 53]

  note: 2022.2.8 should be Tuesday, but according to the monthly alignment, 2022.2.1 is Monday (2022.2.8 is also Monday), so i1 = 1, the same is true for others.

/*
2022-02-08T14:05:06 It's Tuesday
1 => Week (month alignment) parameter: chronofield ALIGNED_ DAY_ OF_ WEEK_ IN_ MONTH
4 => Week (year alignment) parameter: chronofield ALIGNED_ DAY_ OF_ WEEK_ IN_ YEAR
2 => Week (month alignment) parameter: chronofield ALIGNED_ WEEK_ OF_ MONTH
6 => Week (year alignment) parameter: chronofield ALIGNED_ WEEK_ OF_ YEAR
*/
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.of(2022, 2, 8, 14, 5, 6);
        System.out.println(ldt + "yes" + getDayOfWeekStr(ldt));

        int i1 = ldt.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH);
        System.out.println(i1 + " => Week (month) alignment) Parameters: ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH");

        int i2 = ldt.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR);
        System.out.println(i2 + " => Week (year)) Parameters: ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR");

        int i3 = ldt.get(ChronoField.ALIGNED_WEEK_OF_MONTH);
        System.out.println(i3 + " => Week (month) alignment) Parameters: ChronoField.ALIGNED_WEEK_OF_MONTH");

        int i4 = ldt.get(ChronoField.ALIGNED_WEEK_OF_YEAR);
        System.out.println(i4 + " => Week (year)) Parameters: ChronoField.ALIGNED_WEEK_OF_YEAR");
    }

    /**
     * Tool class
     */
    public static String getDayOfWeekStr(LocalDateTime ldt) {
        return ldt.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINESE);
    }

  note: 2022.1.8 should be Saturday, but according to the monthly alignment, 2022.1.1 is Monday, chronofield ALIGNED_ DAY_ OF_ WEEK_ IN_ Month first calculates that under the rule of month alignment: [2022.1.8 ~ 2022.1.15] belongs to the second week (2022.1.1 is the Monday of the first week and 2022.1.8 is the Monday of the second week), so Tuesday of the second week is 2022.1.9, so ldt1 = "2022-01-09T15:03:04", the same is true for others.

/*
2022-01-08T15:03:04 It's Saturday. According to the rule of month alignment, it's Monday. According to the method of year alignment, it's Monday
2022-01-09T15:03:04 => Week (month alignment) parameter: chronofield ALIGNED_ DAY_ OF_ WEEK_ IN_ MONTH, 1
2022-01-10T15:03:04 => Week (year alignment) parameter: chronofield ALIGNED_ DAY_ OF_ WEEK_ IN_ YEAR, 2
2022-01-22T15:03:04 => Week (month) parameter: chronofield ALIGNED_ WEEK_ OF_ MONTH, 3
2022-01-29T15:03:04 => Week number (year alignment) parameter: chronofield ALIGNED_ WEEK_ OF_ YEAR, 4
*/
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.of(2022, 1, 8, 15, 3, 4);
        System.out.println(ldt + "yes" + getDayOfWeekStr(ldt) +
                                   ",The week is aligned according to the month rule" + ldt.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH) +
                                   ",The year alignment is week" + ldt.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR));

        //Value range of ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH [1, 7]
        LocalDateTime ldt1 = ldt.with(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH, 2);
        System.out.println(ldt1 + " => Week (month) alignment) Parameters: ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH, 1");

        //Value range of ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR [1, 7]
        LocalDateTime ldt2 = ldt.with(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR, 3);
        System.out.println(ldt2 + " => Week (year)) Parameters: ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR, 2");

        //The value range of the week (month alignment) (ChronoField.ALIGNED_WEEK_OF_MONTH) [1,5], where x ∈ [4,5] depends on the number of weeks in the current month (the actual test is also OK if x=5 in February of normal year)
        LocalDateTime ldt3 = ldt.with(ChronoField.ALIGNED_WEEK_OF_MONTH, 4);
        System.out.println(ldt3 + " => Week (month)) Parameters: ChronoField.ALIGNED_WEEK_OF_MONTH, 3");

        //Week number (year alignment) (ChronoField.ALIGNED_WEEK_OF_YEAR) value range [1, 53]
        LocalDateTime ldt4 = ldt.with(ChronoField.ALIGNED_WEEK_OF_YEAR, 5);
        System.out.println(ldt4 + " => Week (year)) Parameters: ChronoField.ALIGNED_WEEK_OF_YEAR, 4");
    }
    
    /**
     * Tool class
     */
    public static String getDayOfWeekStr(LocalDateTime ldt) {
        return ldt.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINESE);
    }
Month * 2 months


Month (in the middle of the year) (ChronoField.MONTH_OF_YEAR) value range [1, 12]
The value range of month (since 50 AD) (ChronoField.PROLEPTIC_MONTH) [Year.MIN_VALUE * 12L, Year.MAX_VALUE * 12L + 11] requires the getLong method, where:
 Year.MIN_VALUE = -999999999;   = >   minimum   year MIN_ VALUE * 12L = 11999999988
 Year.MAX_VALUE = 999999999;   = >   maximum   year MAX_ VALUE * 12L + 11 = 11999999999

/*
2022-02-01T14:03:04.000056789
2 => Month (in the middle of the year) parameter: chronofield MONTH_ OF_ YEAR
24265 => Month (since ad 0) parameter: chronofield PROLEPTIC_ MONTH
*/
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.of(2022, 2, 1, 14, 3, 4, 56789);
        System.out.println(ldt);

        int i1 = ldt.get(ChronoField.MONTH_OF_YEAR);
        System.out.println(i1 + " => month(In the middle of the year) Parameters: ChronoField.MONTH_OF_YEAR");

        //You need to use the getLong method
        long l2 = ldt.getLong(ChronoField.PROLEPTIC_MONTH);
        System.out.println(l2 + " => month(Since ad 0) Parameters: ChronoField.PROLEPTIC_MONTH");
    }
/*
2022-02-03T15:03:04.000056789
2022-01-03T15:03:04.000056789 => Month (in the middle of the year) parameter: chronofield MONTH_ OF_ YEAR, 1
0000-03-03T15:03:04.000056789 => Month (since ad 0) parameter: chronofield PROLEPTIC_ MONTH, 2
*/
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.of(2022, 2, 3, 15, 3, 4, 56789);
        System.out.println(ldt);

        //ChronoField.MONTH_OF_YEAR [1, 12]
        LocalDateTime ldt1 = ldt.with(ChronoField.MONTH_OF_YEAR, 1);
        System.out.println(ldt1 + " => month(In the middle of the year) Parameters: ChronoField.MONTH_OF_YEAR, 1");

        //Month (from year 0) (ChronoField.PROLEPTIC_MONTH) value range [Year.MIN_VALUE * 12L, Year.MAX_VALUE * 12L + 11]
        LocalDateTime ldt2 = ldt.with(ChronoField.PROLEPTIC_MONTH, 2);
        System.out.println(ldt2 + " => month(Since ad 0) Parameters: ChronoField.PROLEPTIC_MONTH, 2");
    }
Year * 2


The value range of year (ChronoField.YEAR) [Year.MIN_VALUE, Year.MAX_VALUE] requires the getLong method, where:
 Year.MIN_VALUE = -999999999;   = >   minimum value
 Year.MAX_VALUE = 999999999;   = >   maximum
The value range of ChronoField.YEAR_OF_ERA needs to be discussed by classification, and the getLong method needs to be used, in which:
  ① time before 0001-01-01T00:00 (including the present moment),   belongs to BC, value range [1, year. Max_value + 1] = > [1, 1000000000]
  ② the time after 0001-01-01T00:00 (excluding the present moment) belongs to the value range of [1, year. Max_value] = > [1, 9999999]
  the reason is that the year ∈ [- 999999999, 0] is BC. The year ∈ [1, 99999999] is after A.D. In terms of time range, it is 1 year more before B.C. than after B.C.

/*
2022-02-01T14:03:04.000056789
2022 => Year parameter: chronofield MONTH_ OF_ YEAR
2022 => Year (since 1 AD) parameter: chronofield PROLEPTIC_ MONTH
*/
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.of(2022, 2, 1, 14, 3, 4, 56789);
        System.out.println(ldt);

        //You need to use the getLong method
        long l1 = ldt.getLong(ChronoField.YEAR);
        System.out.println(l1 + " => Annual parameters: ChronoField.MONTH_OF_YEAR");

        //You need to use the getLong method
        long l2 = ldt.getLong(ChronoField.YEAR_OF_ERA);
        System.out.println(l2 + " => year(Since 1 ad) Parameters: ChronoField.PROLEPTIC_MONTH");
    }
/*
-2022-02-03T15:03:04.000056789
0001-02-03T15:03:04.000056789 => Year parameter: chronofield YEAR, 1
0000-02-03T15:03:04.000056789 => Year (since 1 AD) parameter: chronofield YEAR_ OF_ ERA, 1
*/
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.of(-2022, 2, 3, 15, 3, 4, 56789);
        System.out.println(ldt);

        //Value range of year (ChronoField.YEAR) [Year.MIN_VALUE, Year.MAX_VALUE]
        LocalDateTime ldt1 = ldt.with(ChronoField.YEAR, 1);
        System.out.println(ldt1 + " => Annual parameters: ChronoField.YEAR, 1");

        //The value range of ChronoField.YEAR_OF_ERA needs to be discussed by classification, and the getLong method needs to be used
        LocalDateTime ldt2 = ldt.with(ChronoField.YEAR_OF_ERA, 1);
        System.out.println(ldt2 + " => year(Since 1 ad) Parameters: ChronoField.YEAR_OF_ERA, 1");
    }
Ad * 1


Age (BC = 0, BC = 1) (ChronoField.ERA) value range [0, 1], example (pseudo code):
 2022-02-01T00:00. With (chronofield. Era, 0) = > - 2022-02-01t00:00 (becomes negative)
 2022-02-01T00:00. With (chronofield. Era, 1) = > 2022-02-01t00:00 (unchanged)

/*
-2022-02-01T14:03:04.000056789
0 => Age (BC = 0, BC = 1) parameter: chronofield ERA
2022-02-01T14:03:04.000056789
1 => Age (BC = 0, BC = 1) parameter: chronofield ERA
*/
    public static void main(String[] args) {
        LocalDateTime ldt1 = LocalDateTime.of(-2022, 2, 1, 14, 3, 4, 56789);
        System.out.println(ldt1);

        int i1 = ldt1.get(ChronoField.ERA);
        System.out.println(i1 + " => times(B.C=0,After A.D=1) Parameters: ChronoField.ERA");

        LocalDateTime ldt2 = LocalDateTime.of(2022, 2, 1, 14, 3, 4, 56789);
        System.out.println(ldt2);

        int i2 = ldt2.get(ChronoField.ERA);
        System.out.println(i2 + " => times(B.C=0,After A.D=1) Parameters: ChronoField.ERA");
    }

   since 0001-01-01 is after A.D., it is necessary to add + 1 after removing the negative sign from B.C. to A.D

/*
-2022-02-03T15:03:04.000056789
2023-02-03T15:03:04.000056789 => Age (BC = 0, BC = 1) parameter: chronofield ERA, 1
*/
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.of(-2022, 2, 3, 15, 3, 4, 56789);
        System.out.println(ldt);

        //Age (BC = 0, BC = 1) (ChronoField.ERA) value range [0, 1]
        LocalDateTime ldt1 = ldt.with(ChronoField.ERA, 1);
        System.out.println(ldt1 + " => times(B.C=0,After A.D=1) Parameters: ChronoField.ERA, 1");
    }

22/02/11

M

Topics: Java Back-end