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

时间:2013-06-01 09:48:49

标签: java simpledateformat parseexception

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

    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”   在com.ibm.icu.text.DateFormat.parse(DateFormat.java:510)

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

预期输出:Jun 01,2013 12:53:10

如何解决这个问题?

9 个答案:

答案 0 :(得分: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));

注意:

  • 您应该包含语言环境,就好像您的语言环境不是英语,可能无法识别日期名称
  • IST is ambiguous and can lead to problems因此,如果可能,请在输入中使用正确的时区名称。

答案 1 :(得分: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));

答案 2 :(得分: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);

答案 3 :(得分:2)

将您的格式更新为:

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

答案 4 :(得分:2)

  • 您的格式设置模式无法与输入字符串匹配,如其他答案所述。
  • 您的输入格式可怕
  • 您正在使用多年前由 java.time 类取代的麻烦的旧日期时间类。

ISO 8601

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

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

正确的时区名称

continent/region的格式指定proper time zone name,例如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 [亚洲/加尔各答]

关于 java.time

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

现在位于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

答案 5 :(得分:1)

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

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

答案 6 :(得分: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

答案 7 :(得分:1)

检查您的模式(DD-MMM-YYYY)和parse(“ 29-11-2018”)方法的输入。 解析方法的输入应遵循:DD-MMM-YYYY 2019年8月21日

在我的代码中:

String pattern = "DD-MMM-YYYY";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

    try {
        startDate = simpleDateFormat.parse("29-11-2018");// here no pattern match 
        endDate = simpleDateFormat.parse("28-AUG-2019");// Ok 
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

答案 8 :(得分:-1)

我需要在类ParsePosition的解析方法中添加SimpleDateFormat表达式:

simpledateformat.parse(mydatestring, new ParsePosition(0));
相关问题