转换EEE MMM dd HH:mm:ss ZZZ yyyy到YYYY-MM-dd JAVA [英] Convert EEE MMM dd HH:mm:ss ZZZ yyyy to YYYY-MM-dd JAVA
问题描述
我想转换: Thu Feb 02 00:00:00 WET 2012
到 02/02/2012
(日期类型不是字符串)使用JAVA。
I want to convert : Thu Feb 02 00:00:00 WET 2012
to 02/02/2012
(with date type not string) using JAVA.
我做了
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);
我有: Thu Feb 02 00:00:00 WET 2012
。
有谁能告诉我问题在哪里?
And I'm having : Thu Feb 02 00:00:00 WET 2012
.
Can anyone tell me where is the problem ??
推荐答案
Date对象不包含有关所需显示格式的任何信息。因此,从格式化的日期字符串解析日期不会记住任何格式。
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.
System.out.println(date3)将使用java的toString打印date3的值方法。
System.out.println(date3) will print value of date3 using java's toString method.
您在date2中有格式化的日期字符串。所以System.out.println(date2)应该给你正确的值。
You have the formatted date string in date2. So System.out.println(date2) should give you the right value.
这篇关于转换EEE MMM dd HH:mm:ss ZZZ yyyy到YYYY-MM-dd JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!