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
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".
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?
- Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
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.