简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM