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