Reputation: 81792
Using the ThreeTen Android Backport library, what is the simplest way to convert a ZonedDateTime
or OffsetDateTime
into an old-school java.util.Date
instance?
If I had full Java 8 libs at my disposal, this of course would be the way to do it (as in this question):
Date.from(zonedDateTime.toInstant());
But I cannot use that on Android; specifically Date.from(Instant instant)
is missing.
Upvotes: 29
Views: 74333
Reputation: 63405
See DateTimeUtils
which handles the methods added to classes like java.util.Date
:
http://www.threeten.org/threetenbp/apidocs/org/threeten/bp/DateTimeUtils.html
Edit: using that, the complete code would be:
DateTimeUtils.toDate(zonedDateTime.toInstant())
Upvotes: 19
Reputation: 81792
Well, one straightforward way is to get milliseconds since epoch and create the Date from that:
long epochMilli = zonedDateTime.toInstant().toEpochMilli();
Date date = new Date(epochMilli);
Feel free to point out if there's some preferable way.
Upvotes: 49