SimpleDateFormat 在 java 中返回错误的日期
private String dateFormatter(String olddate) {
String newDate = "";
try {
SimpleDateFormat initDate = new SimpleDateFormat("dd-MMM-yy - HH:mm");
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy - HH:mm");
newDate = formatter.format(initDate.parse(olddate));
} catch (ParseException e) {
e.printStackTrace();
}
return newDate;
}
输入日期为:29-MAR-22-22:00
所需的输出日期为:29/03/22-22:00
而不是此,我将获得解析异常 当我将当前日期转换为 dd-mmm-yy-hh:mm
格式,它返回29-m03-22-11:38是错误的。
因此,请帮助我解决这个问题。
提前致谢。
private String dateFormatter(String olddate) {
String newDate = "";
try {
SimpleDateFormat initDate = new SimpleDateFormat("dd-MMM-yy - HH:mm");
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy - HH:mm");
newDate = formatter.format(initDate.parse(olddate));
} catch (ParseException e) {
e.printStackTrace();
}
return newDate;
}
Input Date is : 29-Mar-22 - 22:00
Required Output Date is : 29/03/22 - 22:00
Instead of this I will get parse exception
When I convert current date in dd-MMM-yy - HH:mm
format it returns 29-M03-22 - 11:38 Which is wrong.
So please help me to solve this issue.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保您的格式模式使用字母
m
US-ASCII范围Unicode的范围,如 dawood ibn kareem 评论。java.time
您正在使用几年前被现代 java.Time 类取代的可怕日期时间类
。 ://ideone.com/ym2alp“ rel =“ nofollow noreferrer”>代码在ideone.com 中实时运行。
提示:使用四位数。以我的经验,保存两个数字的空间不值得歧义引起的混乱。
提示:(a)仅使用标准 ISO 8601 数据交换,(b)令 java.time 在生成字符串值以呈现给用户时自动本地化。
Be sure your formatting pattern is using the letter
M
from the US-ASCII range of Unicode, as commented by Dawood ibn Kareem.java.time
You are using terrible date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310.
See this code run live at IdeOne.com.
Tip: Use four digit years. In my experience, saving two digits of space is not worth the confusion caused by the ambiguity.
Tip: Rather than hard-code such formats, (a) use only standard ISO 8601 formats for data exchange, and (b) let java.time automatically localize when producing string values for presentation to the user.