![]() OffsetDateTime odt = myResultSet.getObject( …, OffsetDateTime.class ) The OffsetDateTime is required by JDBC, while Instant & ZonedDateTime are optional. In JDBC 4.2 and later, we can exchange java.time objects with the database. ![]() To retrieve a moment from the database, your column should be of a data type akin to the SQL-standard TIMESTAMP WITH TIME ZONE. Same moment, same point on the timeline, different wall-clock time. To see that same moment through the wall-clock time used by the people of a particular region, adjust into a time zone. Instant instant = Instant.now() // Capture current moment in UTC. This class represent a moment in UTC, always in UTC by definition. To capture the current moment in UTC, use an Instant. I need to force any time related operations to GMT/UTCįor an offset of zero hours-minutes-seconds, use the constant ZoneOffset.UTC. A time zone is a history of the past, present, and future changes to the offset used by the people of a particular region. An offset is merely a number of hour-minutes-seconds ahead or behind the prime meridian. To represent an offset-from-UTC, use ZoneOffset. The legacy date-time classes have been supplanted by the java.time classes defined in JSR 310. The terrible date-time classes bundled with the earliest versions of Java were flawed, written by people who did not understand the complexities and subtleties of date-time handling. Some of the other Answers were correct, but are now outmoded. Such a call affects all apps within that JVM immediately. And be aware that the JVM’s current default time zone can change at any moment during runtime, with any code in any thread of any app calling tDefault. You need not depend on the JVM’s current default time zone. I recommend you write all your code to explicitly state the desired/expected time zone. OffsetDateTime odt = myResultSet.getObject( 1, OffsetDateTime.class ) Īvoid depending on host OS or JVM for default time zone Tl dr String sql = "SELECT CURRENT_TIMESTAMP You can also associate a timezone with the value using a Calendar object: TimeZone tz = TimeZone.getTimeZone("") ĭate dateValue = rs.getDate("DateColumn") Ĭalendar calValue = Calendar.getInstance(tz) To format these on output to another timezone, use SimpleDateFormat. ![]() The and classes store the actual time (milliseconds) in UTC. These will ensure that the value stored in the database is consistent when the database column does not keep timezone information. Ps.setDate("DateColumn", dateValue, tzCal) ![]() PreparedStatement ps = conn.createPreparedStatement("update. Or, setting the date in a PreparedStatement: Calendar tzCal = Calendar.getInstance(TimeZone.getTimeZone("GMT")) If you need to set specific time zones when retrieving Date/Time/Timestamp objects from a database ResultSet, use the second form of the getXXX methods that takes a Calendar object: Calendar tzCal = Calendar.getInstance(TimeZone.getTimeZone("GMT")) ĭate dateValue = rs.getDate("DateColumn", tzCal) The OP answered this question to change the default timezone for a single instance of a running JVM, set the user.timezone system property: java -Duser.timezone=GMT. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |