【发布时间】:2017-08-09 18:24:56
【问题描述】:
我正在尝试获取下一个即将到来的星期五的日期并将其格式化为 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?我确定您不想打印一年中的一天,而是一个月中的一天。 -
这绝对是我的意思。今天我学会了。
-
LocalDate.now( ZoneId.of( "Asia/Kolkata" ) ).with( TemporalAdjusters.next( DayOfWeek.FRIDAY ) ).format( DateTimeFormatter.BASIC_ISO_DATE ) -
对于那些使用 org.joda.time.LocalTime 的解决方案是 friday.toString(ISODateTimeFormat.basicDate())
标签: java