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

旧时模样 提交于 2020-01-15 11:10:29

问题


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

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 ??


回答1:


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) will print value of date3 using java's toString method.

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



来源:https://stackoverflow.com/questions/26132782/convert-eee-mmm-dd-hhmmss-zzz-yyyy-to-yyyy-mm-dd-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!