Create a Timestamp without timeZone

22,485

Solution 1

A Timestamp doesn't have a timezone. When you display the timestamp as a String, it displays a time and mentions the timezone, because else you couldn't know what time it represents. And it chooses to use the default timezone (yours), because that's the one you're the most familiar with.

Saying, it's 12:00:00 doesn't mean anything. Saying it's 12:00:00 in your timezone means something. But the timestamp only contains an instant in time. You may display this instant in time in any time zone you want using a DateFormat.

Note: Timestamp.valueOf("2010-10-23 12:05:16"); means "create a timestamp with the given time in the default timezone".

Solution 2

tl;dr

… can I create a java.sql.timestamp without timezone …

No, you cannot. Wrong class. Use java.time.LocalDateTime instead.

Details

A java.sql.Timestamp is the wrong class to use. It represents a moment in UTC with a resolution of nanoseconds.

If you want a date with time-of-day irrespective of time zone or offset-from-UTC (so, not a moment), use another class that is fit-for-purpose.

By the way… The java.sql.Timestamp is a terrible old class badly designed, and was supplanted years ago by the class java.time.Instant (always in UTC) or alternatively java.time.OffsetDateTime if set to an offset of UTC.

LocalDateTime

The proper class for a date with time-of-day without any concept of time zone or offset-from-UTC is LocalDateTime.

As noted above, this means this class is not a moment, not a point on the timeline, and should not be used to track actual points of time when an event happened in some place.

TIMESTAMP WITHOUT TIME ZONE

In a SQL-standard compliant database such as Postgres, use a column of data type TIMESTAMP WITHOUT TIME ZONE (not the WITH type).

LocalDateTime ldt = myResultSet.getObject( … , LocalDateTime.class ) ;

And…

myPreparedStatement.setObject( … , ldt ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Share:
22,485
JMarques
Author by

JMarques

Updated on November 22, 2020

Comments

  • JMarques
    JMarques over 3 years

    How can I create a java.sql.timestamp without timezone (i´m getting 2007-09-23T10:10:10Z and I pretend 2007-09-23T10:10:10).

    I try:

    Timestamp timestamp = Timestamp.valueOf("2007-09-23 10:10:10");
    

    but in debug i saw that the cdate is 2007-09-23T10:10:10.000+0100 instead of 2007-09-23T10:10:10

  • JMarques
    JMarques over 12 years
    That means that there isn´t a way to say that a don´t want the default timezone(or the same using a Date)? I pretend to ignore the Z in "23T10:10:10Z".
  • JB Nizet
    JB Nizet over 12 years
    Use a DateFormat, set its time zone and pattern to the ones you want, parse your date/time String, and instantiate the Timestamp with the milliseconds from the Date. To display the timestamp, instantiate a DateFormat, set its time zone and pattern to the ones you want, and format the timestamp.