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     //時間格式轉換成yyyy-MM-dd HH:mm:ss
    19     String timeStr1=LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    20     String timeStr2=LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));
    21     System.out.println("當前時間為:"+timeStr1);
    22     System.out.println("當前時間為:"+timeStr2);
    23 }

     

     

  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);
    }

     

      


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM