[英]java.text.ParseException: Unparseable date: "2/9/2016 10:30:00 AM (GMT-05:00) Eastern Time (US & Canada)"
I am getting exception when trying to parse this date:尝试解析此日期时出现异常:
"2/9/2016 10:30:00 AM (GMT-05:00) Eastern Time (US & Canada)" code “2/9/2016 10:30:00 AM (GMT-05:00) 东部时间(美国和加拿大)”代码
DateFormat format = new SimpleDateFormat("d/m/yyyy HH:mm:ss aaa zzz", Locale.ENGLISH);
format.parse("2/9/2016 10:30:00 AM (GMT-05:00) Eastern Time (US & Canada)");
You have used m
for the month which is wrong.你用
m
作为月份,这是错误的。 You have to use M
for the month and m
for the minute.您必须使用
M
代表月份,使用m
代表分钟。
I also recommend you use DateTimeFormatter instead of using the outdated SimpleDateFormat
.我还建议您使用DateTimeFormatter而不是使用过时的
SimpleDateFormat
。 Check this to learn more about the modern date/time API.检查此以了解有关现代日期/时间 API 的更多信息。
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("d/M/yyyy hh:mm:ss a (zzz)")
.toFormatter(Locale.ENGLISH);
ZonedDateTime zdt = ZonedDateTime.parse("2/9/2016 10:30:00 AM (GMT-05:00)", formatter);
System.out.println(zdt);
}
}
Output: Output:
2016-09-02T10:30-05:00[GMT-05:00]
Also,还,
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("d/M/yyyy hh:mm:ss a (zzz)")
.appendLiteral(" Eastern Time (US & Canada)")
.toFormatter(Locale.ENGLISH);
ZonedDateTime zdt = ZonedDateTime.parse("2/9/2016 10:30:00 AM (GMT-05:00) Eastern Time (US & Canada)",
formatter);
System.out.println(zdt);
System.out.println(formatter.format(zdt));
}
}
Output: Output:
2016-09-02T10:30-05:00[GMT-05:00]
2/9/2016 10:30:00 AM (GMT-05:00) Eastern Time (US & Canada)
The date you are parsing does not match with the format,您正在解析的日期与格式不匹配,
m is for a minute, M for Month, This will works, m 代表一分钟,M 代表月份,这会起作用,
DateFormat format = new SimpleDateFormat("d/M/yyyy HH:mm:ss aaa (zzz)", Locale.ENGLISH);
format.parse("2/9/2016 10:30:00 AM (GMT)");
You can use Java SimpleDateFormat Online Tester to create format for date:2/9/2016 10:30:00 AM (GMT-05:00) Eastern Time (US & Canada).您可以使用Java SimpleDateFormat Online Tester创建日期格式:2/9/2016 10:30:00 AM (GMT-05:00) 东部时间(美国和加拿大)。
Hope this help you,希望这对你有帮助,
Like others I recommend that you use java.time, the modern Java date and time API, for your date and time work.像其他人一样,我建议您使用 java.time,现代 Java 日期和时间 API,用于日期和时间工作。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/u h:mm:ss a (OOOO)", Locale.ENGLISH);
String stringToParse = "2/9/2016 10:30:00 AM (GMT-05:00) Eastern Time (US & Canada)";
ParsePosition pp = new ParsePosition(0);
TemporalAccessor parsed = formatter.parse(stringToParse, pp);
OffsetDateTime dateTime = OffsetDateTime.from(parsed);
System.out.println(dateTime);
Output is: Output 是:
2016-09-02T10:30-05:00
2016-09-02T10:30-05:00
Eastern Time (US & Canada)
is not considered a time zone in the time zone database that Java uses, so we cannot parse this part of the string using DateTimeFormatter
or SimpleDateFormat
. Eastern Time (US & Canada)
在 Java 使用的时区数据库中不被视为时区,因此我们无法使用DateTimeFormatter
或SimpleDateFormat
解析这部分字符串。 To parse only a portion of the string I am using the DateTimeFormatter.parse
method that accepts a ParsePosition
.为了仅解析字符串的一部分,我使用了接受
ParsePosition
的DateTimeFormatter.parse
方法。 If we wanted, we might use the ParsePosition
afterwards for determining how much of the string had been parsed.如果需要,我们可以在之后使用
ParsePosition
来确定已解析了多少字符串。
ParseException
is that (GMT-05:00)
doesn't match zzz
in the format pattern string.ParseException
的原因是(GMT-05:00)
与格式模式字符串中的zzz
不匹配。 Or more precisely, your format pattern doesn't include the round bracket that is in the string to be parsed.M
for month in the format pattern.M
Lowercase m
is for minute.m
代表分钟。 This would cause an incorrect result.SimpleDateFormat
ought to throw an exception on the grounds of this error alone, it does not.SimpleDateFormat
应该仅基于此错误而抛出异常,但事实并非如此。h
or hh
for hour within AM or PM from 01 through 12. Upper case HH
is for hour of day from 00 through 23.h
或hh
来表示从 01 到 12 的 AM 或 PM 中的小时。大写HH
是从 00 到 23 的一天中的小时。Oracle tutorial: Date Time explaining how to use java.time. Oracle 教程:日期时间解释如何使用 java.time。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.