java.text.ParseException:无法解析的日期:java.text.DateFormat.parse(DateFormat.java:579) [英] java.text.ParseException: Unparseable date: java.text.DateFormat.parse(DateFormat.java:579)
问题描述
我对 SimpleDateFormat
有问题.
SimpleDateFormat dtfmt=new SimpleDateFormat("dd MMM yyyy hh:mm a", Locale.getDefault());
Date dt=dtfmt.parse(deptdt);
在Android模拟器中工作正常,但在电话中却出现此错误:
In Android Emulator works fine but in phone I have this error:
W/System.err:java.text.ParseException:无法解析的日期:"2016年10月24日晚上7:31"(偏移量3)W/System.err:位于java.text.DateFormat.parse(DateFormat.java:579)
W/System.err: java.text.ParseException: Unparseable date: "24 Oct 2016 7:31 pm" (at offset 3) W/System.err: at java.text.DateFormat.parse(DateFormat.java:579)
有解决方案吗?
推荐答案
在没有 Locale
的情况下,切勿使用 SimpleDateFormat
或 DateTimeFormatter
由于给定的日期时间是英语,因此您应该在日期时间解析器中使用 Locale.ENGLISH
;否则,解析将在使用非英语类型的语言环境的系统(计算机,电话等)中失败.
Never use SimpleDateFormat
or DateTimeFormatter
without a Locale
Since the given date-time is in English, you should use Locale.ENGLISH
with your date-time parser; otherwise the parsing will fail in a system (computer, phone etc.) which is using a non-English type of locale.
此外,请注意 java.util
的日期时间API及其格式API SimpleDateFormat
已过时且容易出错.建议完全停止使用它们,并切换到现代日期时间API .
Also, note that the date-time API of java.util
and their formatting API, SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API.
- 出于任何原因,如果您必须坚持使用Java 6或Java 7,则可以使用 ThreeTen-Backport ,它将大多数 java.time 功能反向移植到Java 6&7.
- 如果您正在为Android项目工作,并且您的Android API级别仍不符合Java-8,请检查如何在Android Project中使用ThreeTenABP .
- For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7.
- If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
演示:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
final String strDateTime = "24 Oct 2016 7:31 pm";
DateTimeFormatter dtf = new DateTimeFormatterBuilder()
.parseCaseInsensitive() // For case-insensitive (e.g. am, Am, AM) parsing
.appendPattern("d MMM uuuu h:m a") // Pattern conforming to the date-time string
.toFormatter(Locale.ENGLISH); // Locale
LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);
System.out.println(ldt);
}
}
输出:
2016-10-24T19:31
默认情况下,默认的FORMAT语言环境,JVM在启动时根据主机环境设置了该语言环境.
By default, DateTimeFormatter#ofPattern
uses the default FORMAT locale which the JVM sets during startup based on the host environment. Same is the case with SimpleDateFormat
. I have tried to illustrate the problem through the following demo:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
final String strDateTime = "24 Oct 2016 7:31 pm";
DateTimeFormatter dtfWithDefaultLocale = null;
System.out.println("JVM's Locale: " + Locale.getDefault());
// Using DateTimeFormatter with the default Locale
dtfWithDefaultLocale = getDateTimeFormatterWithDefaultLocale();
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 = getDateTimeFormatterWithEnglishLocale();
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 = getDateTimeFormatterWithDefaultLocale();
System.out.println("DateTimeFormatter's Locale: " + dtfWithDefaultLocale.getLocale());
System.out.println(
"Parsed with JVM's default locale: " + LocalDateTime.parse(strDateTime, dtfWithDefaultLocale));
}
static DateTimeFormatter getDateTimeFormatterWithDefaultLocale() {
return new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("d MMM uuuu h:m a")
.toFormatter(); // Using default Locale
}
static DateTimeFormatter getDateTimeFormatterWithEnglishLocale() {
return new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("d MMM uuuu h:m a")
.toFormatter(Locale.ENGLISH); // Using Locale.ENGLISH
}
}
输出:
JVM's Locale: en_GB
DateTimeFormatter's Locale: en_GB
Parsed with JVM's default locale: 2016-10-24T19:31
JVM's Locale: fr_FR
DateTimeFormatter's Locale: en
Parsed with Locale.ENGLISH: 2016-10-24T19:31
JVM's Locale: fr_FR
DateTimeFormatter's Locale: fr_FR
Exception in thread "main" java.time.format.DateTimeParseException: Text '24 Oct 2016 7:31 pm' could not be parsed at index 3
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:34)
以下使用 SimpleDateFormat
的演示仅出于完整性考虑:
The following demo, using SimpleDateFormat
, is just for the sake of completeness:
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 = "24 Oct 2016 7:31 pm";
SimpleDateFormat sdf = new SimpleDateFormat("d MMM yyyy h:m a", Locale.ENGLISH);
Date date = sdf.parse(strDateTime);
System.out.println(date);
}
}
输出:
Mon Oct 24 19:31:00 BST 2016
这篇关于java.text.ParseException:无法解析的日期:java.text.DateFormat.parse(DateFormat.java:579)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!