java.text.ParseException:无法解析的日期

问题描述 投票:43回答:8

我在尝试以下代码时遇到解析异常:

    String date="Sat Jun 01 12:53:10 IST 2013";
    SimpleDateFormat sdf=new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
    Date currentdate;
    currentdate=sdf.parse(date);
    System.out.println(currentdate);

例外:

线程“main”中的异常java.text.ParseException:Unparseable date:“Sat Jun 01 12:53:10 IST 2013”​​at com.ibm.icu.text.DateFormat.parse(DateFormat.java:510)

输入:Sat Jun 01 12:53:10 IST 2013

预期产量:Jun 01,2013 12:53:10

怎么解决这个?

java simpledateformat parseexception
8个回答
94
投票

你的模式根本不符合输入字符串...它不起作用也就不足为奇了。这可能会更好:

SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
                                            Locale.ENGLISH);

然后要使用您所需的格式打印,您需要第二个SimpleDateFormat:

Date parsedDate = sdf.parse(date);
SimpleDateFormat print = new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
System.out.println(print.format(parsedDate));

笔记:


5
投票
        String date="Sat Jun 01 12:53:10 IST 2013";
        SimpleDateFormat sdf=new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");
        Date currentdate=sdf.parse(date);
        SimpleDateFormat sdf2=new SimpleDateFormat("MMM dd,yyyy HH:mm:ss");
        System.out.println(sdf2.format(currentdate));

3
投票

模式错了

    String date="Sat Jun 01 12:53:10 IST 2013";
    SimpleDateFormat sdf=new SimpleDateFormat("E MMM dd hh:mm:ss Z yyyy");
    Date currentdate;
    currentdate=sdf.parse(date);
    System.out.println(currentdate);

2
投票

将您的格式更新为:

SimpleDateFormat sdf=new SimpleDateFormat("E MMM dd hh:mm:ss Z yyyy");

2
投票
  • 您的格式设置模式无法与输入字符串匹配,如其他答案所述。
  • 你的输入格式很糟糕。
  • 您使用的是几年前由java.time类取代的麻烦的旧日期时间类。

ISO 8601

而是像您这样的格式,使用ISO 8601标准格式将日期时间值作为文本进行交换。

在解析/生成字符串时,java.time类在默认情况下使用标准ISO 8601格式。

适当的时区名称

proper time zone name的格式指定continent/region,例如America/MontrealAfrica/CasablancaPacific/Auckland。切勿使用诸如ESTIST之类的3-4字母缩写,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。

您的IST可能意味着冰岛标准时间,印度标准时间,爱尔兰标准时间或其他。 java.time类只留下猜测,因为这种歧义没有逻辑解决方案。

java.time

现代方法使用java.time类。

定义格式模式以匹配输入字符串。

String input = "Sat Jun 01 12:53:10 IST 2013";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM dd HH:mm:ss z uuuu" , Locale.US );
ZonedDateTime zdt = ZonedDateTime.parse( input , f );

zdt.toString():2013-06-01T12:53:10Z [Atlantic / Reykjavik]

如果您的输入不适用于Iceland,则应预先解析字符串以调整为适当的时区名称。例如,如果您确定输入是针对印度的,请将IST更改为Asia/Kolkata

String input = "Sat Jun 01 12:53:10 IST 2013".replace( "IST" , "Asia/Kolkata" );
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM dd HH:mm:ss z uuuu" , Locale.US );
ZonedDateTime zdt = ZonedDateTime.parse( input , f );

zdt.toString():2013-06-01T12:53:10 + 05:30 [亚洲/加尔各答]


About java.time

java.time框架内置于Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,如java.util.DateCalendarSimpleDateFormat

现在在Joda-Timemaintenance mode项目建议迁移到java.time班。

要了解更多信息,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。规格是JSR 310

您可以直接与数据库交换java.time对象。使用符合JDBC driver或更高版本的JDBC 4.2。不需要字符串,不需要java.sql.*类。

从哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展了java.time。该项目是未来可能添加到java.time的试验场。你可能会在这里找到一些有用的类,如IntervalYearWeekYearQuartermore


1
投票

我找到了简单的解决方案来获取当前日期而没有任何解析错误。

Calendar calendar;
calendar = Calendar.getInstance();
String customDate = "" + calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" + calendar.get(Calendar.DAY_OF_MONTH);

1
投票
String date="Sat Jun 01 12:53:10 IST 2013";

SimpleDateFormat sdf=new SimpleDateFormat("MMM d, yyyy HH:mm:ss");

此模式与您的输入字符串不符,它发生异常。

您需要使用以下模式来完成工作。

E MMM dd HH:mm:ss z yyyy

以下代码将帮助您跳过异常。

使用SimpleDateFormat

    String date="Sat Jun 01 12:53:10 IST 2013"; // Input String

    SimpleDateFormat simpleDateFormat=new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy"); // Existing Pattern

    Date currentdate=simpleDateFormat.parse(date); // Returns Date Format,

    SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("MMM dd,yyyy HH:mm:ss"); // New Pattern

    System.out.println(simpleDateFormat1.format(currentdate)); // Format given String to new pattern

    // outputs: Jun 01,2013 12:53:10

-1
投票

我需要将ParsePosition表达式添加到类SimpleDateFormat的parse方法中:

simpledateformat.parse(mydatestring, new ParsePosition(0));
© www.soinside.com 2019 - 2024. All rights reserved.