Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tasdemirbahadir/c9ed99cdd262b91692e269d419758130 to your computer and use it in GitHub Desktop.
Save tasdemirbahadir/c9ed99cdd262b91692e269d419758130 to your computer and use it in GitHub Desktop.
Convert java.util.Date time zone between UTC and Turkish time zones.
private static final ZoneId TURKEY_ZONE_ID = ZoneId.of("Turkey");
private static final ZoneId UTC_ZONE_ID = ZoneId.of("UTC");
public static Date convertTurkeyToUTC(Date date) {
return Date.from(date.toInstant().atZone(UTC_ZONE_ID).toLocalDateTime().atZone(TURKEY_ZONE_ID).toInstant());
}
public static Date convertUTCToTurkey(Date date) {
return Date.from(date.toInstant().atZone(TURKEY_ZONE_ID).toLocalDateTime().atZone(UTC_ZONE_ID).toInstant());
}
@tasdemirbahadir
Copy link
Author

It seems a little bit complex but works like a charm when needed to convert java.util.Date time zone value between UTC and Turkish time zones. I know it is not a best practice to use java.util.Date but you need it with hibernate and when writing unit/integration tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment