Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 765
Takumi Avatar asked Feb 19 '23 08:02

Takumi


2 Answers

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

like image 65
ntalbs Avatar answered Apr 13 '23 06:04

ntalbs


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

like image 42
Manuel Avatar answered Apr 13 '23 05:04

Manuel