In Android, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes, together with ThreeTenABP (more on how to use it here).
This API provides a good way to work with dates, much better than the outdated Date
and Calendar
(The old classes (Date
, Calendar
and SimpleDateFormat
) have lots of problems and they're being replaced by the new APIs).
To get a date from a timestamp value (assuming that 1388620296020
is the number of milliseconds from unix epoch), you can use the org.threeten.bp.Instant
class:
// create the UTC instant from 1388620296020
Instant instant = Instant.ofEpochMilli(1388620296020L);
System.out.println(instant); // 2014-01-01T23:51:36.020Z
The output is 2014-01-01T23:51:36.020Z
, because Instant
is always in UTC. If you want to convert this to a date/time in another timezone, you can use the org.threeten.bp.ZoneId
class and create a org.threeten.bp.ZonedDateTime
:
ZonedDateTime z = instant.atZone(ZoneId.of("Europe/Paris"));
System.out.println(z); // 2014-01-02T00:51:36.020+01:00[Europe/Paris]
The output will be 2014-01-02T00:51:36.020+01:00[Europe/Paris]
(the same UTC instant converted to Paris timezone).
Please note that the API avoids the use of 3-letter timezones names (like ECT
or CST
), because they are ambiguous and not standard. Always prefer to use the full names (like Europe/Paris
or America/Los_Angeles
, defined by IANA database) - you can get all the available names by calling ZoneId.getAvailableZoneIds()
.
If you want to print the date in a different format, you can use a org.threeten.bp.format.DateTimeFormatter
(please refer to javadoc to see all the possible formats):
ZonedDateTime z = instant.atZone(ZoneId.of("Europe/Paris"));
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss.SSS Z");
System.out.println(fmt.format(z)); // 02/01/2014 00:51:36.020 +0100