将 EEE MMM dd HH:mm:ss ZZZ yyyy 转换为 YYYY-MM-dd JAVA

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

Convert EEE MMM dd HH:mm:ss ZZZ yyyy to YYYY-MM-dd JAVA

javadateformat

提问by user3862042

I want to convert : Thu Feb 02 00:00:00 WET 2012to 02/02/2012(with date type not string) using JAVA.

我想使用 JAVA将 : 转换Thu Feb 02 00:00:00 WET 201202/02/2012(日期类型不是字符串)。

I did

我做了

String date = "Thu Feb 02 00:00:00 WET 2012";
SimpleDateFormat formatnow = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy", Locale.ENGLISH); 
SimpleDateFormat formatneeded=new SimpleDateFormat("YYYY-MM-dd");
Date date1 = formatnow.parse(date);
String date2 = formatneeded.format(date1);
Date date3= formatneeded.parse(date2);
System.out.println(date3);

And I'm having : Thu Feb 02 00:00:00 WET 2012. Can anyone tell me where is the problem ??

我有:Thu Feb 02 00:00:00 WET 2012。谁能告诉我问题出在哪里??

回答by ashutosh

The Date object does not hold any information about the display format you want. So parsing a date from a formatted date string is not going to 'remember' any formatting.

Date 对象不包含有关您想要的显示格式的任何信息。因此,从格式化的日期字符串中解析日期不会“记住”任何格式。

System.out.println(date3) will print value of date3 using java's toString method.

System.out.println(date3) 将使用 java 的 toString 方法打印 date3 的值。

You have the formatted date string in date2. So System.out.println(date2) should give you the right value.

您在 date2 中有格式化的日期字符串。所以 System.out.println(date2) 应该给你正确的值。