In Java> = 8, you can use the new java.time API .
Input contains:
- a unix timestamp (
1325134800000 ), which is the number of milliseconds since unix ( 1970-01-01T00:00Z ) - a UTC offset (
-0500 ), which is a difference from UTC (in this case, 5 hours after UTC)
There are many different types of date / time objects in the new java.time API. In this case, we can choose a java.time.Instant (which represents the number of nanoseconds since unix) or java.time.OffsetDateTime (which represents Instant , converted to a date / time at a specific offset).
To java.time.format.DateTimeFormatterBuilder String , I use java.time.format.DateTimeFormatterBuilder to create java.time.format.DateTimeFormatter . I also use java.time.temporal.ChronoField to indicate which fields are processed:
DateTimeFormatter fmt = new DateTimeFormatterBuilder() // epoch seconds .appendValue(ChronoField.INSTANT_SECONDS) // milliseconds .appendValue(ChronoField.MILLI_OF_SECOND, 3) // offset .appendPattern("xx") // create formatter .toFormatter();
I also use regex to extract only the relevant part from String input (although you can also use substring() to get it):
String s = "/Date(1325134800000-0500)/"; // get just the "1325134800000-0500" part - you can also do s.substring(6, 24) s = s.replaceAll(".*/Date\\(([\\d\\+\\-]+)\\)/.*", "$1");
Then I can parse the type I want:
// parse to Instant Instant instant = Instant.from(fmt.parse(s)); // parse to OffsetDateTime OffsetDateTime odt = OffsetDateTime.parse(s, fmt);
Instant equivalent to 2011-12-29T05:00:00Z ( Instant is just a point on the timeline, and you may think that it is always in UTC). OffsetDateTime has the same moment, but is converted to an offset of -0500 , so its value is 2011-12-29T00:00-05:00 . But both Instant and OffsetDateTime represent the same point in time.
To convert to java.util.Date , use Instant :
// convert to java.util.Date Date date = Date.from(instant); // if you have an OffsetDateTime, you can do this: Date date = Date.from(odt.toInstant());
This is because java.util.Date does not have timezone / offset information , and it simply represents the number of milliseconds since unix (same concept of Instant ), so it can be easily converted from Instant .
Java 6 and 7
For Java 6 and 7, you can use ThreeTen Backport , the excellent backport for Java 8 of the new date and time classes. And for Android, you'll also need ThreeTenABP (more on how to use it here ).
The difference from Java 8 is package names (in Java 8 it is java.time , and in ThreeTen Backport (or Android ThreeTenABP) it is org.threeten.bp ), but the classes and methods of names are the same. Thus, the formatting and parsing code for Instant and OffsetDateTime same.
Another difference is that in Java <= 7, the java.util.Date class does not have a from() method. But you can use the org.threeten.bp.DateTimeUtils class to convert:
// convert to java.util.Date Date date = DateTimeUtils.toDate(instant); // or from the OffsetDateTime Date date = DateTimeUtils.toDate(odt.toInstant());