将日期转换为字符串显示错误字段DayOfYear无法打印,因为值234超过了最大打印宽度2



我想将日期转换为不同格式的字符串,但我得到了以下错误,

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);

我是不是遗漏了什么?

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

相关内容

最新更新