LocalDateTime的方法總結

藍色的猴子發表於2020-12-03

 LocalDateTime的使用方法

1  public static LocalDateTime now():從指定時區的系統時鐘獲取當前的日期時間。

2  public static LocalDateTime now(Clock clock) 從指定的時鐘獲取當前的日期時間,clock不能為空。

3 public static LocalDateTime of(LocalDate date, LocalTime time)從日期和時間獲取一個 LocalDateTime的例項。

4 public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second) 從年,月,日,小時,分鐘和秒獲得LocalDateTime的例項,將納秒設定為零。

 year - 從預設的 MIN_YEAR到 MAX_YEAR 的年份   

 month - 代表月份的1-12,不為空

 dayOfMonth - 代表月份中1-31,以月份中天數大小為準。

 hour - 從0到23的小時。

 minute - 從0到59的分鐘。

 second - 從0到59的秒其他的同上


5 public int getDayOfYear() 獲得日期欄位  (獲得是年份中的第幾天) 例如 2020/12/3 獲得2020的第338天。
6 public int getDayOfMonth() 獲取月份欄位 (獲取月份中的第幾天) 例如 2020/12/3 獲得12月的第3天。
7 public DayOfWeek getDayOfWeek()獲取星期幾欄位,這是一個列舉DayOfWeek 。(獲得整個星期的星期幾)例如 2020/12/3 獲得這周的星期四。
8 public int getYear()獲取年份欄位
9 public Month getMonth()使用Month列舉獲取月份欄位(獲得不是陣列而是英文單詞)
10 public int getHour()獲取時間欄位
11 public int getMinute()獲取分鐘欄位
12 public int getSecond() 獲得第秒的欄位


13 public LocalDateTime withYear(int year)返回此年份變更的LocalDateTime的副本。 時間不影響計算,結果將相同。 如果一年中的日期無效,它將被更改為該月的最後一個有效日期。
14 public LocalDateTime withMonth(int month)返回此年份更改的LocalDateTime的副本。 時間不影響計算,結果將相同。 如果一年中的日期無效,它將被更改為該月的最後一個有效日期。
15 public LocalDateTime withDayOfMonth(int dayOfMonth)返回這個LocalDateTime的副本,並更改日期。 如果結果LocalDateTime無效,則會丟擲異常。 時間不影響計算,結果將相同。 
16 public LocalDateTime withDayOfYear(int dayOfYear)返回此LocalDateTime的副本。 如果得到LocalDateTime是無效的,則丟擲異常。
17 public LocalDateTime withHour(int hour)返回此日期值更改的LocalDateTime的副本。
18 public LocalDateTime withMinute(int minute)返回這個LocalDateTime的副本,小時值更改。
19 public LocalDateTime withSecond(int second)返回這個LocalDateTime的副本,並更改秒數值。

20 public LocalDateTime plusYears(long years)返回這個LocalDateTime的副本,並以指定的時間段新增。
   該方法通過三個步驟將指定的數量新增到年份欄位:
   1.將輸入年份新增到年份欄位
   2.檢查結果日期是否無效
   3.如果有必要,將月份調整到最後一個有效日期
   例如,2008-02-29(閏年)加上一年將導致無效日期2009-02-29(標準年)。 而不是返回無效結果,而是選擇2009-02-28的最後一個有效日期。

21 public LocalDateTime plusMonths(long months)返回這個LocalDateTime的副本,其中指定的時間段以月為單位。
   此方法通過三個步驟將指定的數量新增到月份欄位:
   1.將輸入的月份新增到月份欄位
   2.檢查結果日期是否無效
   3.如果有必要,將月份調整到最後一個有效日期
   例如,2007-03-31加上一個月將導致無效日期2007-04-31。 而不是返回無效結果,而是選擇2007-04-30的最後一個有效日期。

22 public LocalDateTime plusWeeks(long weeks)返回這個LocalDateTime的副本,其中指定的週期以周為單位。
   該方法將指定的數量以周為單位,根據需要增加月份和年份欄位的日期欄位,以確保結果保持有效。 如果超過最大/最小年份,結果將無效。
   例如,2008-12-31加上一週將導致2009-01-07。

23 public LocalDateTime plusDays(long days)返回此LocalDateTime的指定期間的LocalDateTime的副本。
   此方法將指定的金額新增到天數字段中,根據需要增加月份和年份欄位,以確保結果保持有效。 如果超過最大/最小年份,結果將無效。
   例如,2008-12-31加上一天會導致2009-01-01。

24 public LocalDateTime plusHours(long hours)以指定的時間段返回此LocalDateTime的副本,小時數。
25 public LocalDateTime plusMinutes(long minutes)以指定的時間(以分鐘為單位)返回此LocalDateTime的副本。
26 public LocalDateTime plusSeconds(long seconds)返回此副本LocalDateTime在加秒的規定時間。

27 public LocalDateTime minusYears(long years)返回此LocalDateTime的副本,以減去的年份為單位。
   該方法從三個步驟中減去指定的數量:
   1.從年度欄位減去輸入年數
   2.檢查結果日期是否無效
   3.如果有必要,將月份調整到最後一個有效日期
   例如,2008-02-29(閏年)減去一年將導致無效日期2009-02-29(標準年)。 而不是返回無效結果,而是選擇2009-02-28的最後一個有效日期。

28 public LocalDateTime minusMonths(long months)返回此LocalDateTime的副本,指定的時間以月為單位減去。
   該方法從以下三個步驟中減去月份欄位中指定的數量:
   1.從月份欄位減去輸入月份
   2.檢查結果日期是否無效
   3.如果有必要,將月份調整到最後一個有效日期
   例如,2007-03-31減去一個月將導致無效日期2007-04-31。 而不是返回無效結果,而是選擇2007-04-30的最後一個有效日期。
29 public LocalDateTime minusWeeks(long weeks)返回此LocalDateTime一個副本,以指定的週期扣除。
   該方法從必要的日期欄位減去月和年欄位中減去指定的數量,以確保結果保持有效。 如果超過最大/最小年份,結果將無效。
   例如,2009-01-07減去一週將導致2008-12-31。
30 public LocalDateTime minusDays(long days)返回此LocalDateTime的副本,其中指定的時間間隔以天為單位。
   該方法從天數字段減去指定數量,根據需要增加月份和年份欄位,以確保結果保持有效。 如果超過最大/最小年份,結果將無效。
   例如,2009-01-01減去一天會導致2008-12-31。
31 public LocalDateTime minusHours(long hours)以指定的時間段返回此LocalDateTime的副本,以減少的小時數。
32 public LocalDateTime minusMinutes(long minutes)返回此LocalDateTime的副本,其中指定的時間間隔以分鐘為單位。
33 public LocalDateTime minusSeconds(long seconds)返回此LocalDateTime的副本,其中指定的時間間隔以秒為單位。

34 public String format(DateTimeFormatter formatter)使用指定的格式化程式格式化此日期時間。這個日期時間將傳遞給格式化程式以生成一個字串。
35 public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter)使用特定的格式化LocalDateTime從文字字串獲取LocalDateTime的例項。
36 public boolean isBefore(ChronoLocalDateTime<?> other) 檢查此日期時間是否在指定的日期時間之前
37 public boolean isAfter(ChronoLocalDateTime<?> other)檢查這個日期時間是否在指定的日期之後。
38 public boolean equals(Object obj) 檢查這個日期時間是否等於另一個日期時間。
39public boolean isEqual(ChronoLocalDateTime<?> other)檢查此日期時間是否等於指定的日期時間。
40 public String toString()將此日期時間輸出為String,如2007-12-03T10:15:30。

 以下是測試方法,可以copy測試。

@RunWith(SpringRunner.class)
@SpringBootTest
public class DataTimeUtil {
    @Test
    public void test(){
        LocalDateTime local=LocalDateTime.now();
        System.out.println("獲取預設當前時間為 "+local);

        Clock clock=Clock.systemDefaultZone();
        System.out.println("獲取預設當前的Clock "+clock.toString());
        LocalDateTime localDateTime1=LocalDateTime.now(clock);
        System.out.println("獲取Clock時區的時間為"+localDateTime1);

        LocalDate localDate= LocalDate.now();
        LocalTime localTime=LocalTime.now();
        LocalDateTime localDateTime2=LocalDateTime.of(localDate,localTime);
        System.out.println("localDate是 "+localDate+" localTime是 "+localTime+" 獲取的localDateTime2 "+localDateTime2);

        System.out.println("分割線-------------------------------------------------------------------分割線");
        Integer dayOfYear=local.getDayOfYear();
        Integer dayOfMonth=local.getDayOfMonth();
        DayOfWeek dayOfWeek=local.getDayOfWeek();
        System.out.println("獲得是年份中的第幾天 "+dayOfYear+" 獲取月份中的第幾天 "+dayOfMonth+" 獲得整個星期的星期幾 "+dayOfWeek);

        Integer year=local.getYear();
        Month month=local.getMonth();
        Integer hour=local.getHour();
        Integer minute=local.getMinute();
        Integer second=local.getSecond();
        System.out.println("獲得年份 "+year+" 獲得月份 "+month+" 獲得小時 "+hour+" 獲得分鐘 "+minute+ " 獲得秒鐘 "+second);

        System.out.println("分割線-------------------------------------------------------------------分割線");
        LocalDateTime currentTimeWithDayOfYear=local.withDayOfYear(12);
        LocalDateTime currentTimeWithDayOfMonth=local.withDayOfMonth(11);
        LocalDateTime currentTimeWithYear=local.withYear(2019);
        LocalDateTime currentTimeWithMonth=local.withMonth(1);
        LocalDateTime currentTimeWithHour=local.withHour(12);
        LocalDateTime currentTimeWithMinute=local.withMinute(22);
        LocalDateTime currentTimeWithSecond=local.withSecond(12);
        System.out.println("年份中天變更 "+currentTimeWithDayOfYear+" 月份中天變更 "+currentTimeWithDayOfMonth+" 年份變更 "+currentTimeWithYear
                          +"月份變更 "+currentTimeWithMonth+" 時變更 "+currentTimeWithHour+" 分鐘變更 "+currentTimeWithMinute
                          +"秒變更 "+currentTimeWithSecond);

        System.out.println("分割線-------------------------------------------------------------------分割線");
        LocalDateTime currentTimeAddYear=local.plusYears(1);
        LocalDateTime currentTimeAddMonth=local.plusMonths(1);
        LocalDateTime currentTimeAddDay=local.plusDays(1);
        LocalDateTime currentTimeAddWeek=local.plusWeeks(1);
        LocalDateTime currentTimeAddHour=local.plusHours(1);
        LocalDateTime currentTimeAddSecond=local.plusSeconds(1);
        LocalDateTime currentTimeAddMinutes=local.plusMinutes(1);
        System.out.println("加年份 "+currentTimeAddYear+" 加月份 "+currentTimeAddMonth+" 加天 "+currentTimeAddDay
                         +" 加周 "+currentTimeAddWeek+" 加小時 "+currentTimeAddHour+" 加分鐘 "+currentTimeAddMinutes
                         +" 加秒 "+currentTimeAddSecond);

        LocalDateTime currentTimeMinusYear=local.minusYears(1);
        LocalDateTime currentTimeMinusMonth=local.minusMonths(1);
        LocalDateTime currentTimeMinusWeek=local.minusWeeks(1);
        LocalDateTime currentTimeMinusDay=local.minusDays(1);
        LocalDateTime currentTimeMinusHour=local.minusHours(1);
        LocalDateTime currentTimeMinusMinute=local.minusMinutes(1);
        LocalDateTime currentTimeMinusSecond=local.minusSeconds(1);
        System.out.println("減年份 "+currentTimeMinusYear+" 減月份 "+currentTimeMinusMonth+" 減天 "+currentTimeMinusDay
                         +" 減周 "+currentTimeMinusWeek+" 減小時 "+currentTimeMinusHour+" 減分鐘 "+currentTimeMinusMinute
                         +" 減秒 "+currentTimeMinusSecond);
        System.out.println("分割線-------------------------------------------------------------------分割線");

        DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //它會把時間格式化為指定格式的字串 建立例項才能使用format()方法
        String nowTime=local.format(formatter);
        System.out.println("格式化後的資料時(得到String型別) "+nowTime);
        //parse()方法通過LocalDateTime
        String str11="2007-12-03T10:15:30";
        //注意當String str="2010-1-1 10:10:10"格式會報錯
        String str="2010-01-01 10:10:10";
        LocalDateTime parseTime=LocalDateTime.parse(str11);
        LocalDateTime parseTime1=LocalDateTime.parse(str,formatter);
        System.out.println("格式化後的資料時(得到LocalDateTime型別) "+ parseTime+
                           "格式化後的資料時(得到LocalDateTime型別)有倆個引數 "+parseTime1);
        //傳入的引數是LocalDateTime型別
        LocalDateTime date=parseTime;
        boolean flag=local.isBefore(date);
        boolean flag1=local.isAfter(date);
        System.out.println("傳入的引數是 "+date+" 是否在傳入的資料之前 "+flag+"是否在傳入的資料之後 "+flag1);
        //是否相等
        boolean flag2=local.equals(date);
        System.out.println("localDateTime1和localDateTime2是否相等 "+flag2);
        boolean flag3=local.isEqual(date);
        System.out.println("localDateTime1和localDateTime2是否相等 "+flag3);

        String Str=date.toString();
        System.out.println("輸出字串 "+Str);
    }
}

 

 

相關文章