我正在尝试获取下一个即将到来的星期五的日期,并将其格式设置为yyyyMMDD。如果可能,我想在不使用JodaTime的情况下执行此操作。这里是我的代码:如何将LocalDate格式化为yyyyMMDD(不含JodaTime)
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.time.DayOfWeek;
import java.time.format.DateTimeFormatter;
// snippet from main method
LocalDate friday = LocalDate.now().with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern('yyyyMMDD');
System.out.println(friday.format(formatter));
但是当我运行此我得到以下错误(运行今天20170809)
java.time.DateTimeException: Field DayOfYear cannot be printed as the value 223 exceeds the maximum print width of 2
我在做什么错?
编辑:我使用Java 8
你的意思是'yyyyMMdd'?我相信你不想打印每年的日历,但每个月的日子。 – Kayaman
这绝对是我的意思。今天我学到了。 – zero01alpha
'LocalDate.now(ZoneId.of(“Asia/Kolkata”)).with(TemporalAdjusters.next(DayOfWeek.FRIDAY)).format(DateTimeFormatter.BASIC_ISO_DATE)' –