JDK8-日期時(shí)間新方式

日期時(shí)間新方式
? 在日常開(kāi)發(fā)中,對(duì)于日期操作是非常常見(jiàn)的,但是對(duì)于有經(jīng)驗(yàn)的開(kāi)發(fā)人員來(lái)說(shuō)Java8之前的日期操作是有較大問(wèn)題 的。比方說(shuō)SimpleDateFormat。但是在Java8之后提出了DateTimeFormatter用于解決之前的問(wèn)題。

SimpleDateFormat的那些坑

SimpleDateFormat本身是線程不安全的,同時(shí)繼承的DateFormat類也不是線程安全的,在多線程環(huán)境下,如果多個(gè)線程使用同一個(gè)類解析日期,如果將SimpleDateFormat定義為static,所以在多線程下它的實(shí)例會(huì)被多線程共享,線程之間相互讀取時(shí)間,就會(huì)出現(xiàn)時(shí)間差異和其他的那些異常。

DateTimeFormatter 、LocalDate、LocalTime、LocalDateTime

Java8對(duì)于日期時(shí)間操作提供了一些新類供我們進(jìn)行使用,現(xiàn)在可以通過(guò)DateTimeFormatter來(lái)替換掉 SimpleDateFormat。通過(guò)LocalDate、LocalTime、LocalDateTime類來(lái)操作日期+時(shí)間。而且由源碼可知,這些類都是不可變更,線程安全的類。其內(nèi)部提供了若干用于操作日期的方法。

DateTimeFormatter 、LocalDate、LocalTime、LocalDateTime的常用方法

/**

  • @author 我是七月呀

  • @date 2020/12/28
    */
    public class LocalDateDemp {

    public static void main(String[] args) {

     /**
      * 根據(jù)自己需求獲取任意時(shí)間,例如:2020-12-28 14:57:00
      * 
      */
     LocalDateTime of = LocalDateTime.of(2020, 12, 28, 14, 57, 00);
     String format = of.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
     System.out.println(format);
    
     /**
      * 注意:這里的YYYY是表示:當(dāng)天所在的周屬于的年份,一周從周日開(kāi)始,周六結(jié)束,只要本周跨年,那么這周就算入下一年。
      * 結(jié)果為:2020-12-28 15:39:37
      */
     String formatOne = of.format(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss"));
     System.out.println(formatOne);
    
    
     /**
      * 獲取當(dāng)前時(shí)間的年月日 時(shí)分秒,例如:2020-12-28 15:39:37
      */
     DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
     String format1 = dateTimeFormatter1.format(LocalDateTime.now());
     System.out.println(format1);
    
     /**
      * 獲取當(dāng)前時(shí)間的年月日,例如:2020-12-28
      */
     DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
     String format2 = dateTimeFormatter2.format(LocalDateTime.now());
     System.out.println(format2);
    
     /**
      * 獲取當(dāng)前時(shí)間的時(shí)分秒,例如:15:39:37
      */
     DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofPattern("HH:mm:ss");
     String format3 = dateTimeFormatter3.format(LocalDateTime.now());
     System.out.println(format3);
    
     /**
      * 獲取當(dāng)前時(shí)間的年月日 時(shí)分秒(無(wú)符號(hào)),例如:20201228153937
      */
     DateTimeFormatter dateTimeFormatter4 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
     String format4 = dateTimeFormatter4.format(LocalDateTime.now());
     System.out.println(format4);
    
     /**
      * 獲取當(dāng)前時(shí)間的年月日(無(wú)符號(hào)),例如:20201228
      */
     DateTimeFormatter dateTimeFormatter5 = DateTimeFormatter.ofPattern("yyyyMMdd");
     String format5 = dateTimeFormatter5.format(LocalDateTime.now());
     System.out.println(format5);
    
     /**
      * 獲取當(dāng)前時(shí)間三天后的日期,例如:2020-12-31T15:39:37.807
      * 這里的ChronoUnit.DAYS是時(shí)間單位,可以是WEEKS、MONTHS、YEARS
      */
     LocalDateTime plus = LocalDateTime.now().plus(3, ChronoUnit.DAYS);
     System.out.println(plus);
    
     /**
      * 獲取當(dāng)前時(shí)間三天前的日期,例如:2020-12-25T15:39:37.807
      * 這里的ChronoUnit.DAYS是時(shí)間單位,可以是WEEKS、MONTHS、YEARS
      */
     LocalDateTime plus1 = LocalDateTime.now().minus(3, ChronoUnit.DAYS);
     System.out.println(plus1);
    
     /**
      * 獲取指定時(shí)間當(dāng)年的最后一天,例如:2020-12-31 23:59:59
      */
     String format6 = dateTimeFormatter1.format(LocalDateTime.now().with(TemporalAdjusters.lastDayOfYear()).withHour(23).withMinute(59).withSecond(59));
     System.out.println(format6);
    
    
     /**
      * 獲取指定時(shí)間當(dāng)年的第一天,例如:2020-01-01 00:00:00
      */
     String format7 = dateTimeFormatter1.format(LocalDateTime.now().with(TemporalAdjusters.firstDayOfYear()).withHour(00).withMinute(00).withSecond(00));
     System.out.println(format7);
     String format8 = dateTimeFormatter1.format(LocalDateTime.now().withDayOfYear(1).withHour(00).withMinute(00).withSecond(00));
     System.out.println(format8);
    
    
     /**
      * 獲取指定時(shí)間的前三天,例如:2020-12-25
      */
     LocalDate minus = LocalDate.now().minus(3, ChronoUnit.DAYS);
     System.out.println(minus);
     LocalDate localDate = LocalDate.now().minusDays(3);
     System.out.println(localDate);
     LocalDateTime minus1 = LocalDateTime.now().minus(3, ChronoUnit.DAYS);
     DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
     String format9 = minus1.format(dateTimeFormatter);
     System.out.println(format9);
    
     /**
      * 獲取指定時(shí)間的后三天,例如:2020-12-31
      */
     LocalDate plus2 = LocalDate.now().plus(3, ChronoUnit.DAYS);
     System.out.println(plus2);
     LocalDate localDate1 = LocalDate.now().plusDays(3);
     System.out.println(localDate1);
     LocalDateTime plus3 = LocalDateTime.now().plus(3, ChronoUnit.DAYS);
     DateTimeFormatter dateTimeFormatter6 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
     String format10 = plus3.format(dateTimeFormatter6);
     System.out.println(format10);
    

    }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容