Reputation: 1427
In my project ,I get json data from Server,there's a field named 'creat_at' in the json which style is like 'Wed Jun 20 11:01:05 +0800 2012'
How to change it to more easy-to-read style like '2012/06/20'?
(I have tried 'DateFormat.parse' but it dose not work: new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy").parse(dateString) cause java.text.ParseException: Unparseable date: "Wed Jun 20 11:00:53 +0800 2012")
Upvotes: 1
Views: 314
Reputation: 533560
You need to try SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd");
System.out.println(sdf2.format(sdf.parse("Wed Jun 20 11:01:05 +0800 2012")));
prints
2012/06/20
You may also need to set an approriate timezone.
Upvotes: 1
Reputation: 240908
See SimpleDateFormat
API
Date date = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy").parse(dateString);
String formattedDate = new SimpleDateFormat("yyyy/MM/dd").format(date);
Upvotes: 2