问题描述
如何将下面的日期字符串解析成日期
对象?
How do I parse the date string below into a Date
object?
String target = "Thu Sep 28 20:29:30 JST 2000";
DateFormat df = new SimpleDateFormat("E MM dd kk:mm:ss z yyyy");
Date result = df.parse(target);
抛出异常...
java.text.ParseException: Unparseable date: "Thu Sep 28 20:29:30 JST 2000"
at java.text.DateFormat.parse(DateFormat.java:337)
推荐答案
模式是错误的。你有一个3个字母的天缩写,所以它必须是 EEE
。你有一个3个字母的月份缩写,所以它必须是 MMM
。由于这些日期和月份缩写语言区域设置敏感,因此您希望明确指定 SimpleDateFormat
语言环境为英语,否则将使用平台默认语言环境,可能不是
The pattern is wrong. You have a 3-letter day abbreviation, so it must be EEE
. You have a 3-letter month abbreviation, so it must be MMM
. As those day and month abbreviations are locale sensitive, you'd like to explicitly specify the SimpleDateFormat
locale to English as well, otherwise it will use the platform default locale which may not be English per se.
public static void main(String[] args) throws Exception {
String target = "Thu Sep 28 20:29:30 JST 2000";
DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH);
Date result = df.parse(target);
System.out.println(result);
}
此处打印
Thu Sep 28 07:29:30 BOT 2000
根据我的时区是正确的。
which is correct as per my timezone.
如果您不想使用 HH
而不是 kk
,我也会重新考虑。有关有效模式的详细信息,请阅读。
I would also reconsider if you wouldn't rather like to use HH
instead of kk
. Read the javadoc for details about valid patterns.
这篇关于如何解析日期字符串到日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!