简体   繁体   中英

Convert Date format from “29/05/2011 00:00:00 ZE8” into “Sat, 28 May 2011”in Java

I am struggling to convert my String "29/05/2011 00:00:00 ZE8" into the following format in "Sat, 28 May 2011" in Java.

This is an abstract of my code. It seems to work but my colleague in Chile gets a date that is 2 years in the future when he runs the code.

SimpleDateFormat df2 = new SimpleDateFormat("EEE, dd MMM yyyy");
Date etdDate = df2.parse(OCD_ETD_String);
String OCD_ETD_StringFormatted = dateFormat.format(etdDate);  

Hope someone could shed some light.

Best Regards

You need two different date format strings, one for the "incoming" format and one for the "outgoing" format. Then parse with one and format with the other. Like

SimpleDateFormat in=new SimpleDateFormat("dd/MM/yyyy HH:mm:ss zzz");
SimpleDateFormat out=new SimpleDateFormat("EEE dd MMM yyyy");
Date somedate=in.parse(dateFormatIn);
String dateFormatOut=out.format(somedate);

In your example you are attempting to parse with the desired output format. This won't work: If it was in that format already, you wouldn't need to do any of this.

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