以下代码有什么问题?它抛出一个ParseException,错误偏移量为0。
final DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");
df.parse("Thu Jan 23 14:24:47 2014");
答案 0 :(得分:7)
如果您在构建格式化程序时没有为格式化程序指定Locale
,则会使用您的默认Locale
,它显然不会用英语拼写日期和月份。
因此请为格式化程序指定一个。
final DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", Locale.UK);
答案 1 :(得分:0)
您的区域设置是"EN"
吗?如果您使用英文名称作为日期,请确保使用该区域设置
答案 2 :(得分:0)
SimpleDateFormat
绝对是语言环境敏感的。某些字段(如小时和分钟)与区域设置无关。
SimpleDateFormat
还支持本地化的日期和时间模式字符串。在这些字符串中,上述模式字母可以用其他依赖于语言环境的模式字母替换。SimpleDateFormat
不处理模式字母以外的文本本地化;这取决于班级的客户。
或者,您可以使用本地化友好的DateFormat#getDateInstance()
工厂方法,因为:
public SimpleDateFormat(String pattern, Locale locale)
使用给定模式和给定语言环境的默认日期格式符号构造
SimpleDateFormat
。注意:此构造函数可能不支持所有语言环境。要获得完整的覆盖率,请使用DateFormat
类中的工厂方法。
答案 3 :(得分:0)
SimpleDateFormat
的情况下使用 DateTimeFormatter
或 Locale
由于给定的日期时间是英文的,您应该将 Locale.ENGLISH
与您的日期时间解析器一起使用;否则解析将在使用非英语类型区域设置的系统(计算机、电话等)中失败。
另外,请注意 java.util
的日期时间 API 及其格式 API SimpleDateFormat
已过时且容易出错。建议完全停止使用它们并切换到 modern date-time API。
演示:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
final String strDateTime = "Thu Jan 23 14:24:47 2014";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss uuuu", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);
System.out.println(ldt);
}
}
输出:
2014-01-23T14:24:47
默认情况下,DateTimeFormatter#ofPattern
使用 JVM 在启动时根据宿主环境设置的 default FORMAT locale。 SimpleDateFormat
的情况也是如此。我试图通过以下演示来说明问题:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
final String strDateTime = "Thu Jan 23 14:24:47 2014";
DateTimeFormatter dtfWithDefaultLocale = null;
System.out.println("JVM's Locale: " + Locale.getDefault());
// Using DateTimeFormatter with the default Locale
dtfWithDefaultLocale = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss uuuu");
System.out.println("DateTimeFormatter's Locale: " + dtfWithDefaultLocale.getLocale());
System.out.println(
"Parsed with JVM's default locale: " + LocalDateTime.parse(strDateTime, dtfWithDefaultLocale));
// Setting the JVM's default locale to Locale.FRANCE
Locale.setDefault(Locale.FRANCE);
// Using DateTimeFormatter with Locale.ENGLISH explicitly (recommended)
DateTimeFormatter dtfWithEnglishLocale = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss uuuu", Locale.ENGLISH);
System.out.println("JVM's Locale: " + Locale.getDefault());
System.out.println("DateTimeFormatter's Locale: " + dtfWithEnglishLocale.getLocale());
LocalDateTime zdt = LocalDateTime.parse(strDateTime, dtfWithEnglishLocale);
System.out.println("Parsed with Locale.ENGLISH: " + zdt);
System.out.println("JVM's Locale: " + Locale.getDefault());
// Using DateTimeFormatter with the default Locale
dtfWithDefaultLocale = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss uuuu");
System.out.println("DateTimeFormatter's Locale: " + dtfWithDefaultLocale.getLocale());
System.out.println(
"Parsed with JVM's default locale: " + LocalDateTime.parse(strDateTime, dtfWithDefaultLocale));
}
}
输出:
JVM's Locale: en_GB
DateTimeFormatter's Locale: en_GB
Parsed with JVM's default locale: 2014-01-23T14:24:47
JVM's Locale: fr_FR
DateTimeFormatter's Locale: en
Parsed with Locale.ENGLISH: 2014-01-23T14:24:47
JVM's Locale: fr_FR
DateTimeFormatter's Locale: fr_FR
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Thu Jan 23 14:24:47 2014' could not be parsed at index 0
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:492)
at Main.main(Main.java:33)
以下使用 SimpleDateFormat
的演示只是为了完整起见:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
public static void main(String[] args) throws ParseException {
final String strDateTime = "Thu Jan 23 14:24:47 2014";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", Locale.ENGLISH);
Date date = sdf.parse(strDateTime);
System.out.println(date);
}
}
输出:
Thu Jan 23 14:24:47 GMT 2014