Convert Epoch time to date and Date to Epoch time in Android

24,374

Solution 1

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" ) );

Solution 2

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.

Solution 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
Share:
24,374
Ramji
Author by

Ramji

A Simple Guy

Updated on September 22, 2020

Comments

  • Ramji
    Ramji almost 4 years

    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
    Ramji almost 13 years
    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.
  • Kaj
    Kaj almost 13 years
    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.
  • Ramji
    Ramji almost 13 years
    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
  • Kaj
    Kaj almost 13 years
    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.