Last active
April 16, 2021 12:49
-
-
Save firegate666/06a8cc63bf2269429df875214dadeda8 to your computer and use it in GitHub Desktop.
Change time to another timezone
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
// get now date | |
LocalDateTime ldt = LocalDateTime.now(); | |
// tell him that this is Europe/Berlin | |
ZonedDateTime serverTime = ldt.atZone(ZoneId.of("Europe/Berlin")); | |
// then tell him to the same time but in another zone | |
ZonedDateTime playerTime = serverTime.withZoneSameInstant(ZoneId.of("UTC")); | |
System.out.println(serverTime); | |
System.out.println(playerTime); | |
// 2021-04-16T14:45:31.096+02:00[Europe/Berlin] | |
// 2021-04-16T12:45:31.096Z[UTC] | |
// DO NOT MIX IT UP WITH THIS APPROACH | |
ldt = LocalDateTime.now(); | |
serverTime = ldt.atZone(ZoneId.of("Europe/Berlin")); | |
playerTime = serverTime.withZoneSameLocal(ZoneId.of("UTC")); | |
System.out.println(serverTime); | |
System.out.println(playerTime); | |
// 2021-04-16T14:47:11.918+02:00[Europe/Berlin] | |
// 2021-04-16T14:47:11.918Z[UTC] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment