10

StrDate = "2011-07-19T18:23:20+0000";

How can I get an epoch time for the above date format in android

also I would like to know how to convert a epoch time to the above date format.

I would appreciate a direct answer with an example.

Ramji
  • 2,536
  • 7
  • 34
  • 54

3 Answers3

15

Example code using Joda-Time 2.3.

Unix time is number of seconds since beginning of 1970 in UTC/GMT.

How can I get an epoch time for the above date format in android

DateTime dateTimeInUtc = new DateTime( "2011-07-19T18:23:20+0000", DateTimeZone.UTC );
long secondsSinceUnixEpoch = ( dateTimeInUtc.getMillis() / 1000 ); // Convert milliseconds to seconds.

…and…

how to convert a epoch time to the above date format.

String dateTimeAsString = new DateTime( secondsSinceUnixEpoch * 1000, DateTimeZone.UTC ).toString();

To dump those values to the console…

System.out.println( "dateTimeInUtc: " + dateTimeInUtc );
System.out.println( "secondsSinceUnixEpoch: " + secondsSinceUnixEpoch );
System.out.println( "dateTimeAsString: " + dateTimeAsString );

Bonus: Adjust to another time zone.

DateTime dateTimeMontréal = dateTimeInUtc.withZone( DateTimeZone.forID( "America/Montreal" ) );
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
7

You should use SimpleDateFormat. That class both supports formatting, and parsing.

Sample code:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZZZZ");
Date gmt = formatter.parse("2011-07-19T18:23:20+0000");
long millisecondsSinceEpoch0 = gmt.getTime();
String asString = formatter.format(gmt);

Note that a Date instance in Java, always represent milliseconds since epoch 0, in UTC/GMT, but it is printed in local time when you print it.

Kaj
  • 10,862
  • 2
  • 33
  • 27
  • I am not able to access the milliseconds value of date object. You are trying to give me date formats, I am looking for epoch time value, I would appreciated a direct answer something like, long epochtime = blahblah(); is'nt there a direct function to get that epoch time. – Ramji Jul 20 '11 at 08:21
  • What do you mean by epoch time? The `Date` object that you get represents milliseconds since epoch 0. I updated my reply to show how you get the milliseconds. The code displays have to convert from String to Date, and from Date to String. – Kaj Jul 20 '11 at 08:34
  • 1
    dateobj.getTime() gets me the number of milliseconds from Jan 31 1970 GMT when I do a simpledateformatter parse to a date and then do a tostring it automatically shows in current timezone, I just want to know now if the gettime() will return me the correct Epoch time based on my timezones – Ramji Jul 20 '11 at 09:41
  • 2
    As I said, the milliseconds that `getTime()` returns is _always_ _always_ _always_ milliseconds in *GMT*. `toString()` will always print the time in local time, but that's a pure presentation issue. The Date is just a wrapper around the milliseconds in GMT. – Kaj Jul 20 '11 at 11:01
3

To answer your question a bit late but Joda-Time will be able to handle both in a simply and clean way.

Using Joda-Time

1.Epoch time to Date

Where date is your epoch time

DateTime dateTime = new DateTime(date*1000L);
System.out.println("Datetime ..." + dateTime);

Datetime from Epoch ...2014-08-01T13:00:00.000-04:00

2.Date to epoch

 DateTime fromDate = new DateTime("2011-07-19T18:23:20+0000");
 long epochTime =  fromDate.getMillis();
 System.out.println("Date is.." + fromDate + " epoch of date " + epochTime);

 Date is..2011-07-19T14:23:20.000-04:00 epoch of date 1311099800000
Skillachie
  • 3,176
  • 1
  • 25
  • 26