convert date to string显示无法打印错误字段dayofyear,因为值234超过最大打印宽度2

8yoxcaq7  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(299)

这个问题在这里已经有了答案

Java8LocalDate.plusmonths正在添加天数和月份(1个答案)
5个月前关门了。
我想把日期转换成不同格式的字符串,但是我得到了下面的错误,

DateTimeException-Field DayOfYear cannot be printed as the value 234 exceeds the maximum print width of 2

以下是不同的格式,

"MMDDYY"
"DD_MM_YY" 
"YYYYMMDD"
"MMDD"
"DD-MM-YY"

下面是我的密码,

LocalDate localDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("DD-MM-YY");
String formattToString = localDate.format(formatter);

我是不是漏了什么?

cfh9epnr

cfh9epnr1#

dd(大写)表示dd-一年中的某一天,在本例中是打印234,所以您必须替换为dd(小写),这样就可以了。yy不会导致您案例中的错误,但请将其更改为yyyy。尝试像这样更改代码:

LocalDate localDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
String formattToString = localDate.format(formatter);

本教程提供了一些模式示例:http://tutorials.jenkov.com/java-internationalization/simpledateformat.html

相关问题