Java:无法解析的日期异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2009207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: unparseable date exception
提问by Niko Gamulin
While trying to transform the date format I get an exception:unparseable date and don't know how to fix this problem.
在尝试转换日期格式时,我收到一个异常:无法解析的日期并且不知道如何解决这个问题。
I am receiving a string which represents an event date and would like to display this date in different format in GUI.
我收到一个表示事件日期的字符串,并希望在 GUI 中以不同的格式显示此日期。
What I was trying to do is the following:
我试图做的是以下内容:
private String modifyDateLayout(String inputDate){
try {
//inputDate = "2010-01-04 01:32:27 UTC";
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").parse(inputDate);
return new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date);
} catch (ParseException e) {
e.printStackTrace();
return "15.01.2010";
}
}
Anyway the line
反正线
String modifiedDateString = originalDate.toString();
is dummy. I would like to get a date string in the following format:
是假的。我想获得以下格式的日期字符串:
dd.MM.yyyy HH:mm:ss
dd.MM.yyyy HH:mm:ss
and the input String example is the following:
输入字符串示例如下:
2010-01-04 01:32:27 UTC
2010-01-04 01:32:27 UTC
Does anyone know how to convert the example date (String) above into a String format dd.MM.yyyy HH:mm:ss?
有谁知道如何将上面的示例日期(字符串)转换为字符串格式 dd.MM.yyyy HH:mm:ss?
Thank you!
谢谢!
Edit: I fixed the wrong input date format but still it doesn't work. Above is the pasted method and below is the screen image from debugging session.
编辑:我修复了错误的输入日期格式,但仍然不起作用。上面是粘贴的方法,下面是调试会话的屏幕图像。
alt text http://img683.imageshack.us/img683/193/dateproblem.png
替代文字 http://img683.imageshack.us/img683/193/dateproblem.png
#UpdateI ran
#更新我跑了
String[] timezones = TimeZone.getAvailableIDs();
and there is UTC String in the array. It's a strange problem.
并且数组中有 UTC 字符串。这是一个奇怪的问题。
I did a dirty hack that works:
我做了一个肮脏的黑客,有效:
private String modifyDateLayout(String inputDate){
try {
inputDate = inputDate.replace(" UTC", "");
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(inputDate);
return new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date);
} catch (ParseException e) {
e.printStackTrace();
return "15.01.2010";
}
}
But still I would prefer to transform the original input without cutting timezone away.
但我仍然更愿意在不减少时区的情况下转换原始输入。
This code is written for Android phone using JDK 1.6.
此代码是为使用 JDK 1.6 的 Android 手机编写的。
采纳答案by BalusC
What you're basically doing here is relying on Date#toString()
which already has a fixed pattern. To convert a Java Date
object into another human readable String pattern, you need SimpleDateFormat#format()
.
你在这里所做的基本上是依赖于Date#toString()
已经有固定模式的。要将 JavaDate
对象转换为另一个人类可读的 String 模式,您需要SimpleDateFormat#format()
.
private String modifyDateLayout(String inputDate) throws ParseException{
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").parse(inputDate);
return new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date);
}
By the way, the "unparseable date" exception can here onlybe thrown by SimpleDateFormat#parse()
. This means that the inputDate
isn't in the expected pattern "yyyy-MM-dd HH:mm:ss z"
. You'll probably need to modify the pattern to match the inputDate
's actual pattern.
顺便说一句,“无法解析的日期”异常在这里只能由SimpleDateFormat#parse()
. 这意味着inputDate
不在预期的模式中"yyyy-MM-dd HH:mm:ss z"
。您可能需要修改模式以匹配inputDate
的实际模式。
Update:Okay, I did a test:
更新:好的,我做了一个测试:
public static void main(String[] args) throws Exception {
String inputDate = "2010-01-04 01:32:27 UTC";
String newDate = new Test().modifyDateLayout(inputDate);
System.out.println(newDate);
}
This correctly prints:
这正确打印:
03.01.2010 21:32:27
(I'm on GMT-4)
(我在 GMT-4)
Update 2:as per your edit, you really got a ParseException
on that. The most suspicious part would then be the timezone of UTC
. Is this actually knownat your Java environment? What Java version and what OS version are you using? Check TimeZone.getAvailableIDs()
. There must be a UTC
in between.
更新 2:根据您的编辑,您确实ParseException
对此有所了解。最可疑的部分将是 的时区UTC
。这实际上在您的 Java 环境中是已知的吗?您使用的是什么 Java 版本和什么操作系统版本?检查TimeZone.getAvailableIDs()
。必须有一个UTC
中间。
回答by Pixel Power LLC
I encountered this error working in Talend. I was able to store S3 CSV files created from Redshift without a problem. The error occurred when I was trying to load the same S3 CSV files into an Amazon RDS MySQL database. I tried the default timestamp Talend timestamp formats but they were throwing exception:unparseable date when loading into MySQL.
我在 Talend 中遇到了这个错误。我能够毫无问题地存储从 Redshift 创建的 S3 CSV 文件。当我尝试将相同的 S3 CSV 文件加载到 Amazon RDS MySQL 数据库时发生错误。我尝试了默认的时间戳 Talend 时间戳格式,但它们在加载到 MySQL 时抛出异常:无法解析的日期。
This from the accepted answer helped me solve this problem:
这来自接受的答案帮助我解决了这个问题:
By the way, the "unparseable date" exception can here only be thrown by SimpleDateFormat#parse(). This means that the inputDate isn't in the expected pattern "yyyy-MM-dd HH:mm:ss z". You'll probably need to modify the pattern to match the inputDate's actual pattern
顺便说一下,这里的“无法解析的日期”异常只能由 SimpleDateFormat#parse() 抛出。这意味着 inputDate 不在预期的模式“yyyy-MM-dd HH:mm:ss z”中。您可能需要修改模式以匹配 inputDate 的实际模式
The key to my solution was changing the Talend schema. Talend set the timestamp field to "date" so I changed it to "timestamp" then I inserted "yyyy-MM-dd HH:mm:ss z" into the format string column view a screenshot here talend schema
我的解决方案的关键是更改 Talend 架构。Talend 将时间戳字段设置为“日期”,因此我将其更改为“时间戳”,然后将“yyyy-MM-dd HH:mm:ss z”插入到格式字符串列中查看屏幕截图talend schema
I had other issues with 12 hour and 24 hour timestamp translations until I added the "z" at the end of the timestamp string.
我在 12 小时和 24 小时时间戳转换方面遇到了其他问题,直到我在时间戳字符串的末尾添加了“z”。
回答by sdev
From Oracle docs, Date.toString()method convert Date objectto a String of the specific form - do not use toString method on Date object. Try to use:
从 Oracle 文档中,Date.toString()方法将Date 对象转换为特定形式的 String - 不要在 Date 对象上使用 toString 方法。尝试使用:
String stringDate = new SimpleDateFormat(YOUR_STRING_PATTERN).format(yourDateObject);
Next step is parse stringDate to Date:
下一步是将 stringDate 解析为 Date:
Date date = new SimpleDateFormat(OUTPUT_PATTERN).parse(stringDate);
Note that, parsemethod throws ParseException
请注意,parse方法抛出 ParseException