topxebec
topxebec

Reputation: 1427

How to handle the Time data from the Style just like "Wed Jun 20 11:01:05 +0800 2012"?

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

Answers (2)

Peter Lawrey
Peter Lawrey

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

Jigar Joshi
Jigar Joshi

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

Related Questions