About
This article is about time zone in Java.
Management
List
Arrays.stream(TimeZone.getAvailableIDs())
.map(id->TimeZone.getTimeZone(id))
.sorted(Comparator.comparing(TimeZone::getRawOffset))
.forEach(tz-> System.out.println(+tz.getRawOffset()/1000/60/24+" - "+tz.getID()+" - "+tz.getDisplayName()));
.................
0 - Europe/Isle_of_Man - Greenwich Mean Time
0 - Europe/Jersey - Greenwich Mean Time
0 - Europe/Lisbon - Western European Time
0 - Europe/London - Greenwich Mean Time
0 - GB - Greenwich Mean Time
0 - GB-Eire - Greenwich Mean Time
0 - GMT - Greenwich Mean Time
0 - GMT0 - Greenwich Mean Time
0 - Greenwich - Greenwich Mean Time
0 - Iceland - Greenwich Mean Time
0 - Portugal - Western European Time
0 - UCT - Coordinated Universal Time
0 - UTC - Coordinated Universal Time
0 - Universal - Coordinated Universal Time
0 - WET - Western European Time
0 - Zulu - Coordinated Universal Time
2 - Africa/Algiers - Central European Time
2 - Africa/Bangui - Western African Time
2 - Africa/Brazzaville - Western African Time
2 - Africa/Ceuta - Central European Time
....................
Calendar
- with a calendar
Calendar.getInstance().getTimeZone()
sql.Timestamp
A ResultSet.getTimestamp(String) will get a Timestamp representing time in the local time zone
from UTC to Timezone: If you've stored my date-time values in UTC, you should specify a time-zone when retrieving, by using a Calendar object.
ResultSet rs;
TimeZone tzUtc = TimeZone.getTimeZone("UTC");
Calendar cUtc = Calendar.getInstance(tzUtc);
Timestamp ts = rs.getTimestamp("dateColumn", cUtc);
System.out.println(ts.getTime()); // prints 0
The same timezone-shifting problem can also occur on the way in. Make sure the value in the database really is 0. If it isn't, use PreparedStatement.setTimestamp(0, cUtc) to prevent the shift.