2021年1月22日星期五

How to convert Java String "EEE MMM dd HH:mm:ss zzz yyyy"date type to Java util.Date "yyyy-MM-dd"

My source date type is EEE MMM dd HH:mm:ss zzz yyyy. For example, Sat Dec 12 00:00:00 KST 2020 But my target date type is Java Date type with format yyyy-MM-dd. So I make the method which convert Java String type EEE MMM dd HH:mm:ss zzz yyyy to java util object.

private static Date getDate(String beforeDate) throws Exception{                    SimpleDateFormat readFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);          Date rdate = readFormat.parse(beforeDate);                    SimpleDateFormat writeFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);           String format = writeFormat.format(rdate);                    System.out.println(format);                    return writeFormat.parse(format);      }  

System.out.println(format) line prints the right values which I expect.

2020-12-12  2020-12-12  2013-01-01  

But the type of return values is wrong. The return value from the method seems not to be influenced.

System.out.println(getDate("Sat Dec 12 00:00:00 KST 2020"));   

The above line prints Sat Dec 12 00:00:00 KST 2020 again. I have no idea which codes are wrong in converting Java date type. Any advice will be appreciated. Best regards

https://stackoverflow.com/questions/65856109/how-to-convert-java-string-eee-mmm-dd-hhmmss-zzz-yyyydate-type-to-java-util January 23, 2021 at 01:41PM

没有评论:

发表评论