1. 程式人生 > >java8新特性LocalDate、LocalTime、LocalDateTime的學習

java8新特性LocalDate、LocalTime、LocalDateTime的學習

以前操作時間都是使用SimpleDateFormat類改變Date的時間格式,使用Calendar類操作時間。但是SimpleDateFormat是執行緒不安全的,原始碼如下:

 1 private StringBuffer format(Date date, StringBuffer toAppendTo,
 2                                 FieldDelegate delegate) {
 3         // Convert input date to time field list
 4         calendar.setTime(date);
 5 
 6         boolean useDateFormatSymbols = useDateFormatSymbols();
 7 
 8         for (int i = 0; i < compiledPattern.length; ) {
 9             int tag = compiledPattern[i] >>> 8;
10             int count = compiledPattern[i++] & 0xff;
11             if (count == 255) {
12                 count = compiledPattern[i++] << 16;
13                 count |= compiledPattern[i++];
14             }
15 
16             switch (tag) {
17             case TAG_QUOTE_ASCII_CHAR:
18                 toAppendTo.append((char)count);
19                 break;
20 
21             case TAG_QUOTE_CHARS:
22                 toAppendTo.append(compiledPattern, i, count);
23                 i += count;
24                 break;
25 
26             default:
27                 subFormat(tag, count, delegate, toAppendTo, useDateFormatSymbols);
28                 break;
29             }
30         }
31         return toAppendTo;
32     }

 

calendar是共享變數,並且這個共享變數沒有做執行緒安全控制。當多個執行緒同時使用相同的SimpleDateFormat物件呼叫format方法時,多個執行緒會同時呼叫calendar.setTime方法,可能一個執行緒剛設定好time值另外的一個執行緒馬上把設定的time值給修改了導致返回的格式化時間可能是錯誤的。

所以JAVA8推出了LocalDate、LocalTime、LocalDateTime是執行緒安全、簡易、高可靠的時間包。原始碼如下

  1. LocalDate  獲取年月日

     1 @Test
     2 public void localDataTest(){
     3     //建立LocalDate獲取當前年月日
     4     LocalDate now = LocalDate.now();
     5     System.out.println("當前時間:" + now);
     6     //構造指定的年月日
     7     LocalDate localDate = LocalDate.of(2020, 11, 24);
     8     System.out.println("當前指定時間:" + localDate);
     9     int year = now.getYear();
    10     System.out.println("當前年year:" + year);
    11     int year1 = now.get(ChronoField.YEAR);
    12     System.out.println("當前年year1:" + year1);
    13     Month month = now.getMonth();
    14     System.out.println("當前月month:" + month);
    15     int month1 = now.get(ChronoField.MONTH_OF_YEAR);
    16     System.out.println("當前月month1:" + month1);
    17     int day = now.getDayOfMonth();
    18     System.out.println("當前天day:" + day);
    19     int day1 = now.get(ChronoField.DAY_OF_MONTH);
    20     System.out.println("當前天day1:" + day1);
    21     DayOfWeek dayOfWeek = now.getDayOfWeek();
    22     System.out.println("當前星期幾dayOfWeek:" + dayOfWeek);
    23     int dayOfWeek1 = now.get(ChronoField.DAY_OF_WEEK);
    24     System.out.println("當前星期幾dayOfWeek1:" + dayOfWeek1);
    25  }

     

  2. LocalTime   獲取時分秒

     1 @Test
     2 public void localTimeTest(){
     3     //建立LocalTime獲取當前時分秒
     4     LocalTime now = LocalTime.now();
     5     System.out.println("當前時間:" + now);
     6     LocalTime localTime = LocalTime.of(13, 51, 10);
     7     System.out.println("指定時間:" + localTime);
     8     int hour = now.getHour();
     9     System.out.println("當前小時數hour:"+ hour);
    10     int hour1 = now.get(ChronoField.HOUR_OF_DAY);
    11     System.out.println("當前小時數hour1:"+ hour1);
    12     int minute = now.getMinute();
    13     System.out.println("當前分鐘數minute:"+ minute);
    14     int minute1 = now.get(ChronoField.MINUTE_OF_DAY);
    15     System.out.println("當前分鐘數minute1:"+ minute1);
    16     int second = now.getSecond();
    17     System.out.println("當前秒數second:"+ second);
    18     int second1 = now.get(ChronoField.SECOND_OF_MINUTE);
    19     System.out.println("當前秒數second1:"+ second1);
    20 }

     

  3. LocalDateTime   獲取年月日時分秒,相當於LocalDate+LocalTime,可與LocalDate、LocalTime相互轉換

     1 @Test
     2 public void localDataTimeTest(){
     3    //建立localDateTime獲取當前年月日時分秒
     4     LocalDateTime localDateTime = LocalDateTime.now();
     5     System.out.println("當前時間:" + localDateTime);
     6     LocalDateTime localDateTime1 = LocalDateTime.of(2020, Month.SEPTEMBER, 10, 14, 46, 56);
     7     System.out.println("指定時間:" + localDateTime1);
     8     LocalDateTime of = LocalDateTime.of(LocalDate.now(), LocalTime.now());
     9     System.out.println("當前時間1:" + of);
    10     LocalDateTime localDateTime2 = LocalDate.now().atTime(LocalTime.now());
    11     System.out.println("當前時間2:" + localDateTime2);
    12     LocalDateTime localDateTime3 = LocalTime.now().atDate(LocalDate.now());
    13     System.out.println("當前時間3:" + localDateTime3);
    14     LocalDate localDate = localDateTime.toLocalDate();
    15     System.out.println("當前年月日:" + localDate);
    16     LocalTime localTime = localDateTime.toLocalTime();
    17     System.out.println("當前時分秒:" + localTime);
    18 }

     

  4. Period  時間長度類   Period表示以年、月、日衡量的時長,通過Period增加、減少年月日

     1 @Test
     2 public void periodTest(){
     3     //建立localDateTime獲取當前年月日時分秒
     4     LocalDateTime localDateTime = LocalDateTime.now();
     5     //建立Period物件,設定一年
     6     Period period = Period.ofYears(1);
     7     //增加一年
     8     LocalDate localDate = localDateTime.toLocalDate().plus(period);
     9     System.out.println("增加一年後的時間:" + localDate);
    10     Period period1 = Period.ofYears(-1);
    11     //減少一年
    12     LocalDate localDate1 = localDateTime.toLocalDate().plus(period1);
    13     System.out.println("減少一年後的時間:" + localDate1);
    14     //建立Period物件,設定一月
    15     Period period2 = Period.ofMonths(1);
    16     //增加一個月
    17     LocalDate localDate2 = localDateTime.toLocalDate().plus(period2);
    18     System.out.println("增加一個月後的時間:" + localDate2);
    19     //建立Period物件,設定一週
    20     Period period3 = Period.ofWeeks(1);
    21     //增加一週
    22     LocalDate localDate3 = localDateTime.toLocalDate().plus(period3);
    23     System.out.println("增加一週後的時間:" + localDate3);
    24     //建立Period物件,設定一天
    25     Period period4 = Period.ofDays(1);
    26     //增加一天
    27     LocalDate localDate4 = localDateTime.toLocalDate().plus(period4);
    28     System.out.println("增加一天後的時間:" + localDate4);
    29 }

     

  5. Duration  時間長度類   Duration表示以時、分、秒衡量的時長,通過Duration增加、減少時分秒

     1 @Test
     2 public void durationTest(){
     3       //建立localDateTime獲取當前時間時分秒
     4       LocalDateTime localDateTime = LocalDateTime.now();
     5       //建立Duration物件,設定1天
     6       Duration duration = Duration.ofDays(1);
     7       //增加一天
     8       LocalTime localTime = localDateTime.toLocalTime().plus(duration);
     9       System.out.println("增加一天後時間:" + localTime);
    10       //建立Duration物件,設定一小時
    11       Duration duration1 = Duration.ofHours(-1);
    12       //減少一天
    13       LocalTime localTime1 = localDateTime.toLocalTime().plus(duration1);
    14       System.out.println("增加一天後時間:" + localTime1);
    15       //建立Duration物件,設定一小時
    16       Duration duration2 = Duration.ofHours(1);
    17       //增加一小時
    18       LocalTime localTime2 = localDateTime.toLocalTime().plus(duration2);
    19       System.out.println("增加一小時後時間:" + localTime2);
    20       //建立Duration物件,設定一分鐘
    21       Duration duration3 = Duration.ofMinutes(1);
    22       //增加一分鐘
    23       LocalTime localTime3 = localDateTime.toLocalTime().plus(duration3);
    24       System.out.println("增加一分鐘後時間:" + localTime3);
    25       //建立Duration物件,設定十秒鐘
    26       Duration duration4 = Duration.ofSeconds(10);
    27       //增加十秒鐘
    28       LocalTime localTime4 = localDateTime.toLocalTime().plus(duration4);
    29       System.out.println("增加十秒鐘後時間:" + localTime4);
    30       //建立Duration物件,設定一百毫秒
    31       Duration duration5 = Duration.ofMillis(100);
    32       //增加一百毫秒
    33       LocalTime localTime5 = localDateTime.toLocalTime().plus(duration5);
    34       System.out.println("增加一百毫秒後時間:" + localTime5);
    35 }

     

  6. ZonedDateTime   時區處理類   可以設定時區,用法和LocalDataTime基本相同

    @Test
    public void zonedDateTimeTest(){
        ZoneId zoneId = ZoneId.of("UTC+1");
        //設定指定時間、時區
        ZonedDateTime zonedDateTime = ZonedDateTime.of(2020, 11, 24, 15, 29, 53, 1026, zoneId);
        //獲取今天是今年第幾天
        int dayOfYear = zonedDateTime.getDayOfYear();
        System.out.println("今天是今年第"+dayOfYear+"天");
        //獲取今天是這月第幾天
        int dayOfMonth = zonedDateTime.getDayOfMonth();
        System.out.println("今天是這月第"+dayOfMonth+"天");
        //獲取今天是這周第幾天
        int dayOfWeek = zonedDateTime.getDayOfWeek().get(ChronoField.DAY_OF_WEEK);
        System.out.println("今天是這周第"+dayOfWeek+"天");
        //增加一年
        ZonedDateTime zonedDateTime1 = zonedDateTime.plusYears(1);
        System.out.println("增加一年後的時間:"+zonedDateTime1);
        //減少一年
        ZonedDateTime zonedDateTime2 = zonedDateTime.minusYears(1);
        System.out.println("減少一年後的時間:"+zonedDateTime2);
    }