Reputation: 181
I have a string date Wed, 30 Mar 2016 01:39:56 +0000 which i want to convert to the date format "yyyy-MM-dd"
Below are the lines of code i am trying to achieve it. But it is returning unparseable date exception
String date_s = "Wed, 30 Mar 2016 01:39:56 +0000";
SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd");
Date date = dt.parse(date_s);
SimpleDateFormat dt1 = new SimpleDateFormat("MMM d, yyyy");
System.out.println(dt1.format(date));
Any advice?
Upvotes: 0
Views: 761
Reputation:
Since Java 8, you can do it in one line with the java.time library.
LocalDateTime.parse("Wed, 30 Mar 2016 01:39:56 +0000", DateTimeFormatter.RFC_1123_DATE_TIME)
.format(DateTimeFormatter.ISO_LOCAL_DATE);
Upvotes: 4
Reputation: 11267
'Wed, 30 Mar 2016 01:39:56 +0000' is not in the right format for yyyy-MM-dd
to parse it
If you want it to work, try this:
SimpleDateFormat in = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd");
Date date = in.parse("Wed, 30 Mar 2016 01:01:01 +0000");
String outdate = out.format(date);
System.out.println(outdate);
Upvotes: 4