jdk8 日期格式轉換"yyyyMM"


 

LocalDate必須有年,月,日三個內容

 

        //LocalDate -> YearMonth -> String
        LocalDate localDate = LocalDate.now();
        YearMonth yearMonth = YearMonth.of(localDate.getYear(), localDate.getMonthValue());
        String str = yearMonth.format(DateTimeFormatter.ofPattern("yyyyMM"));
        System.out.println(str);

 

 

     //String -> YearMonth -> LocalDate
        YearMonth yearMonth = YearMonth.parse("202008", DateTimeFormatter.ofPattern("yyyyMM"));
        LocalDate localDate = LocalDate.of(yearMonth.getYear(), yearMonth.getMonthValue(), 1);
        System.out.println(localDate.toString())

 

 

參考: https://blog.csdn.net/neweastsun/article/details/81702426    YearMonth


免責聲明!

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



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