Roh*_*itT 1 java datetime java-8
我想将日期转换为不同格式的字符串,但出现以下错误,
DateTimeException-Field DayOfYear cannot be printed as the value 234 exceeds the maximum print width of 2
Run Code Online (Sandbox Code Playgroud)
下面是不同的格式,
"MMDDYY"
"DD_MM_YY"
"YYYYMMDD"
"MMDD"
"DD-MM-YY"
Run Code Online (Sandbox Code Playgroud)
下面是我的代码,
LocalDate localDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("DD-MM-YY");
String formattToString = localDate.format(formatter);
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么吗?
DD(大写)表示 DD - 一年中的某一天,在本例中打印 234,因此您必须替换为 dd(小写),这样就可以正常工作。YY 不会导致您的情况出现错误,但请将其更改为 yyyy。尝试像这样更改您的代码:
LocalDate localDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
String formattToString = localDate.format(formatter);
Run Code Online (Sandbox Code Playgroud)
本教程有一些模式示例: http://tutorials.jenkov.com/java-internationalization/simpledateformat.html