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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36606547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:40:07  来源:igfitidea点击:

java.text.ParseException: Unparseable date exception

java

提问by Magesh Balasubramaniam

If I do toStringon a Dateobject, I am getting the output as below

如果我toString在一个Date对象上做,我会得到如下输出

2016-04-13 22:00:01.0

I am doing the below to convert the object to Data again

我正在执行以下操作以再次将对象转换为数据

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd:HH:mm:SS");
Date convertedDate = (Date) formatter.parse(timestamp.toString());

But getting the ParseException.

但是得到ParseException.

I am trying to get the output as Date object as below 2016-04-13 22:00:01

我试图将输出作为日期对象,如下 2016-04-13 22:00:01

回答by Beri

This test should cover your example:

此测试应涵盖您的示例:

First from comment:

首先来自评论:

    SimpleDateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss zzz yyyy");
    Date convertedDate = formatter.parse("Thu Apr 14 15:24:14 CEST 2016")
    System.out.print(convertedDate);

Second from question:

第二个问题:

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    Date convertedDate = formatter.parse("2016-04-13 22:00:01.0");
    System.out.print(convertedDate);

It all depends on input String that you are receiving.

这一切都取决于您收到的输入字符串。

You should not use Date::toString method, use formatter, as you willl have no problem with zones and formats. It's better to have full flow control over your data.

你不应该使用 Date::toString 方法,使用格式化程序,因为你不会有区域和格式的问题。最好对您的数据进行完全的流量控制。

Now if you want to convert it to a " 2016-04-13 22:00:01" format, simply use:

现在,如果您想将其转换为“2016-04-13 22:00:01”格式,只需使用:

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String formatedDate = formatter.format(convertedDate);

回答by subrunner

Parse from String with a certain date-format to a Date object:

从具有特定日期格式的 String 解析为 Date 对象:

    String dateString = "2016-04-13 22:00:01.0";
SimpleDateFormat formatterFullMilliseconds = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SSS" );

Date dateFromDateString = formatterFullMilliseconds.parse( dateString );

System.out.println( dateFromDateString ); // Output: Wed Apr 13 22:00:01 CEST 2016

Parse a java.util.Date object to the string representation you want:

将 java.util.Date 对象解析为您想要的字符串表示形式:

SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
System.out.println( formatter.format( dateFromDateString ) ); // 2016-04-13 22:00:01
System.out.println( formatter.format( new Date() ) ); // 2016-04-14 15:29:04

回答by Firdous Ahmad

This is exactly what you need to do, to escape java.text.ParseException: Unparseable date exception

这正是您需要做的,以逃避 java.text.ParseException: Unparseable date exception

Date date = new Date();

SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt = sp.parse(sp.format(date));
System.out.println(dt.toString());

回答by John K

yyyy-MM-dd:HH:mm:SS

should be

应该

yyyy-MM-dd HH:mm:ss.S