Last active
February 15, 2022 08:37
-
-
Save baybatu/ebfeed272fb002c8f357fc6492c5a92f to your computer and use it in GitHub Desktop.
Setting zoned date time as UTC in a new java.util.Date object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* 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