简体   繁体   English

java.text.ParseException:无法解析的日期:“2/9/2016 10:30:00 AM (GMT-05:00) 东部时间(美国和加拿大)”

[英]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,希望这对你有帮助,

java.time java.时间

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 使用的时区数据库中不被视为时区,因此我们无法使用DateTimeFormatterSimpleDateFormat解析这部分字符串。 To parse only a portion of the string I am using the DateTimeFormatter.parse method that accepts a ParsePosition .为了仅解析字符串的一部分,我使用了接受ParsePositionDateTimeFormatter.parse方法。 If we wanted, we might use the ParsePosition afterwards for determining how much of the string had been parsed.如果需要,我们可以在之后使用ParsePosition来确定已解析了多少字符串。

What went wrong in your code?你的代码出了什么问题?

  • The reason for your 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.或者更准确地说,您的格式模式不包括要解析的字符串中的圆括号。
  • As others have said, you need uppercase M for month in the format pattern.正如其他人所说,格式模式中的月份需要大写M Lowercase m is for minute.小写的m代表分钟。 This would cause an incorrect result.这会导致不正确的结果。 While SimpleDateFormat ought to throw an exception on the grounds of this error alone, it does not.虽然SimpleDateFormat应该仅基于此错误而抛出异常,但事实并非如此。
  • You finally need lower case 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.您最终需要小写hhh来表示从 01 到 12 的 AM 或 PM 中的小时。大写HH是从 00 到 23 的一天中的小时。

Link关联

Oracle tutorial: Date Time explaining how to use java.time. Oracle 教程:日期时间解释如何使用 java.time。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不可解析的日期java.text.ParseException:不可解析的日期:“ 2015-10-08 05:00:00.0” - Unparsable Date java.text.ParseException: Unparseable date: “2015-10-08 05:00:00.0” java.text.ParseException:无法解析日期:“2016年3月4日20:33 + 05:30”(偏移3处) - java.text.ParseException: Unparseable date: “04 Mar 2016 20:33+05:30” (at offset 3) java.text.ParseException:无法解析的日期:“1901-01-01 00:00:00” - java.text.ParseException: Unparseable date: “1901-01-01 00:00:00” java.text.ParseException:无法解析的日期:“ 1991-04-14 00:00:00” - java.text.ParseException: Unparseable date: “1991-04-14 00:00:00” java.text.ParseException:无法解析的日期:“ CET 2012年1月11日星期三00:00:00” - java.text.ParseException: Unparseable date: “Wed Jan 11 00:00:00 CET 2012” java.text.ParseException:无法解析的日期:“ 2015-08-19T00:00:00” - java.text.ParseException: Unparseable date: “2015-08-19T00:00:00” java.text.ParseException:无法解析的日期:“8:30 AM” - java.text.ParseException: Unparseable date: "8:30 AM" java.text.ParseException:不可解析的日期:“ 20年1月20日,上午10:28”(偏移量为0) - java.text.ParseException: Unparseable date: “20, 1 20, 2016 10:28 am” (at offset 0) java.text.ParseException:无法解析的日期:“ 2012年1月11日08:00:00” - java.text.ParseException: Unparseable date:“01/11/2012 08:00:00” Gson:java.text.ParseException:无法解析的日期:“ 2018-04-09T09:00:00 + 02:00” - Gson: java.text.ParseException: Unparseable date: “2018-04-09T09:00:00+02:00”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM