Takumi
Takumi

Reputation: 355

How to format timestamp I got from Dropbox metadata calling into custom form?

I want to format the date/time that I got from metadata calling of Dropbox API. I have read the detail about formatting the date/time from it reference: Dropbox API Date format but still not clear about that. I use JAVA and retrieve the metadata via JSON. What I need to do is

Format: Thu, 27 Sep 2012 13:44:09 +0000 ----to---> 27/09/2012 13:44:09

I have try SimpleDateFormat but it was returned me something like unable to convert string to datetime format. Thank you in advance.

Upvotes: 4

Views: 1595

Answers (3)

rmuller
rmuller

Reputation: 12859

You should use RESTUtility#parseDate(String) for parsing this value.

The Dropbox API returns a String, not a Date.

Upvotes: 0

Manuel
Manuel

Reputation: 4238

Should be something like this:

(Exception handling ommited)

                                            //Thu, 27 Sep 2012 13:44:09 +0000
SimpleDateFormat dfDb = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
Date dateDb = dfDb.parse(yourStringDateFromDb);
                                               //27/09/2012 13:44:09
SimpleDateFormat toYours = new SimpleDateFormat("dd/mm/yyyy HH:mm:ss");
String yourString = toYours.format(dateDb);

See: java.text.SimpleDateFormat

Upvotes: 3

ntalbs
ntalbs

Reputation: 29458

Try this:

SimpleDateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
Date date = df.parse("Thu, 27 Sep 2012 13:44:09 +0000");
System.out.println(date);

When you want to convert date string into Date object, you can use SimpleDateFormat. While creating the object, specify the format code, then parse the string with parse() method of SimpleDateFormat. Other type can also be parsed if you specify proper format code.

Be sure to set the locale when you instantiate SimpleDateFormat. It works well without locale if your VM default locale is US. Otherwise like CJK, you must specify the locale to prevent parsing error.

For more detail, refer to the following URL: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

Upvotes: 5

Related Questions