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

原文地址:https://www.cnblogs.com/smileblogs/p/13426355.html