java 如何将日期解析为 EEE MMM dd HH:mm:ss zzz yyyy 格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16345711/
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-10-31 22:42:15  来源:igfitidea点击:

How to parse date to EEE MMM dd HH:mm:ss zzz yyyy format?

javaparsingdate

提问by developer

I am getting date from database as 2013-05-03 00:20:29.0. I want to parse the date to EEE MMM dd HH:mm:ss zzz yyyyformat, but when I am parsing it, I am getting this exception:

我从数据库中获取日期为2013-05-03 00:20:29.0. 我想将日期解析为EEE MMM dd HH:mm:ss zzz yyyy格式,但是在解析它时,出现以下异常:

java.text.ParseException: Unparseable date: "2013-05-03 00:20:29.0"

java.text.ParseException: Unparseable date: "2013-05-03 00:20:29.0"

My code is below:

我的代码如下:

    String createdDate = "2013-05-03 00:20:29.0";
    SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
    Date parseDate = format.parse(createdDate);

回答by cinhtau

You have to adjust the Format pattern. Format must be yyyy-MM-dd HH:mm:ss.S

您必须调整格式模式。格式必须是 yyyy-MM-dd HH:mm:ss.S

Try this

试试这个

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");

See http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

请参阅http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

回答by Evgeniy Dorofeev

As I understod you want to convert your date to a new format

正如我所理解的,您想将日期转换为新格式

Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse("2013-05-03 00:20:29.0");
String str = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").format(date);

You can also try to get the date from the DB not as String but as java.sql.Date then the first step will be unnecessary

您也可以尝试从数据库中获取日期而不是字符串而是 java.sql.Date 那么第一步将是不必要的

回答by Reimeus

Your SimpleDateFormatpattern does not match createdDate. The format should be:

您的SimpleDateFormat模式不匹配createdDate。格式应该是:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");

See the javadoc

请参阅javadoc

回答by Sankar

Here is an example:

下面是一个例子:

String str_date= "29/02/2013";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");        
java.util.Date dt = sdf.parse(str_date);
DateFormat df = new SimpleDateFormat("EEEE");
System.out.println(df.format(dt));