Tutorials     About     RSS
Tech and Media Labs

Java's java.sql.Timestamp

Jakob Jenkov
Last update: 2014-06-23

Java's java.sql.Timestamp class is used in the JDBC API. If you need to set a date + time on a java.sql.PreparedStatement or get a date + time from a java.sql.ResultSet, you will interact with java.sql.Timestamp.

Actually, java.sql.Timestamp extends java.util.Date, so anything you can do with a java.util.Date you can also do with a java.sql.Timestamp. Check out java.util.Date for more details.

The biggest difference between java.sql.Date and java.sql.Timestamp is that the java.sql.Date only keeps the date, not the time, of the date it represents. So, for instance, if you create a java.sql.Date using the date and time 2009-12-24 23:20, then the time (23:20) would be cut off. If you use a java.sql.Timestamp then the time is kept.

Nanoseconds

One difference in the java.sql.Timestamp from its superclass java.util.Date is its ability to hold the nanoseconds of a date too. You can get and set the nanoseconds using the getNanos() and setNanos(). Here is how:

long time = System.currentTimeMillis();
java.sql.Timestamp timestamp = new java.sql.Timestamp(time);

timestamp.setNanos(123456);
int nanos = timestamp.getNanos(); // nanos = 123456

Jakob Jenkov

Featured Videos





















Sponsored Ads

Maildroppa - Smart Email Marketing Solution
Close TOC

All Trails

Trail TOC

Page TOC

Previous

Next