Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Setting zoned date time as UTC in a new java.util.Date object
/*
*
* Methods manipulate milliseconds field (fastTime) in java.util.Date object.
*
* UTC : 12/10/2017 11:15:00
* Local date time : 12/10/2017 14:15:00 +03:00
*
* @return new java.util.Date
* UTC : 12/10/2017 14:15:00
* Local date time : 12/10/2017 17:15:00 +03:00
*/
Date inJodaTimeWay(Date date) {
return new DateTime(date.getTime(), DateTimeZone.forID("Europe/Istanbul"))
.toLocalDateTime()
.toDate(TimeZone.getTimeZone("UTC"));
}
Date inJavaTimeWay(Date date) {
return new Date(date
.toInstant()
.atZone(ZoneId.of("Europe/Istanbul"))
.toLocalDateTime()
.toInstant(ZoneOffset.UTC)
.toEpochMilli());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment