我想解析的字符串类型:“ 36/2017”,其中第36个是一年中的第几周,第一个是2017年。
我的代码:
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("w/uuuu")
.parseDefaulting(ChronoField.DAY_OF_WEEK, 1)
.toFormatter();
LocalDate date = LocalDate.parse("36/2017", formatter);
我添加了默认日期。
我收到此消息:
java.time.format.DateTimeParseException: Text '36/2017' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {WeekOfWeekBasedYear[WeekFields[MONDAY,4]]=36, Year=2017, DayOfWeek=1},ISO of type java.time.format.Parsed
有什么主意吗? 谢谢!
答案 0 :(得分:5)
从文档中:https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
如果使用星期,则应使用大写的Y。
public static void main(String[] args) {
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("w/YYYY")
.parseDefaulting(ChronoField.DAY_OF_WEEK, 1)
.toFormatter();
LocalDate date = LocalDate.parse("36/2017", formatter);
}
答案 1 :(得分:4)
模式错误。您必须设置以下字符串
“ w / YYYY”
DateTimeFormatter formatter = new
DateTimeFormatterBuilder()
.appendPattern("w/YYYY")
.parseDefaulting(ChronoField.DAY_OF_WEEK, 1)
.toFormatter();
LocalDate date = LocalDate.parse("36/2017", formatter);