如何解决将日期转换为字符串会显示错误字段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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。