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

posted @ 2020-08-03 15:06  Peter.Jones  阅读(1539)  评论(0编辑  收藏  举报