使用 SimpleDateFormat 将自定义日期格式转换为另一种格式时出错
我下面的代码有什么问题吗?
try {
// dataFormatOrigin (Wed Jun 01 14:12:42 2011)
// this is original string with the date information
SimpleDateFormat sdfSource = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");
Date date = sdfSource.parse(dataFormatOrigin);
// (01/06/2011 14:12:42) - the destination format that I want to have
SimpleDateFormat sdfDestination = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
dataFormatDest = sdfDestination.format(date);
System.out.println("Date is converted to MM-dd-yyyy hh:mm:ss");
System.out.println("Converted date is : " + dataFormatDest);
} catch (ParseException pe) {
System.out.println("Parse Exception : " + pe);
}
What's wrong with my code below?
try {
// dataFormatOrigin (Wed Jun 01 14:12:42 2011)
// this is original string with the date information
SimpleDateFormat sdfSource = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");
Date date = sdfSource.parse(dataFormatOrigin);
// (01/06/2011 14:12:42) - the destination format that I want to have
SimpleDateFormat sdfDestination = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
dataFormatDest = sdfDestination.format(date);
System.out.println("Date is converted to MM-dd-yyyy hh:mm:ss");
System.out.println("Converted date is : " + dataFormatDest);
} catch (ParseException pe) {
System.out.println("Parse Exception : " + pe);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有什么。这在我的电脑上运行得很好。
编辑:这没有帮助。您可能有需要考虑的特定区域设置。如果您的区域设置需要不同的月份名称/日期名称,您将得到例外。
编辑2:试试这个:
Nothing. This works just fine on my computer.
EDIT: that wasn't helpful. You may have specific Locale settings that need to be considered. If your Locale expects different month names/day names you will get an exception.
EDIT 2: Try this:
这应该有效:
This should work: