java.sql.Time Examples

The java.sql.Time extends java.util.Date class. java.sql.Time is used in the JDBC API, as a wrapper around java.util.Date that handles SQL specific requirements. This class exists to represent SQL TIME, which keeps time without date. Similar to java.sql.Date, java.sql.Time should be used only when working with databases.

Initialize java.sql.Time

We can initialize a java.sql.Time object using following constructor:

Let's check following example:
import java.sql.Time;

public class SqlTimeInitExample {
    public static void main(String[] args) {
        long now = System.currentTimeMillis();
        Time sqlTime = new Time(now);
        System.out.println("currentTimeMillis: " + now);
        System.out.println("SqlTime          : " + sqlTime);
        System.out.println("SqlTime.getTime(): " + sqlTime.getTime());

Which will produce (result may vary)

currentTimeMillis: 1563994221159
SqlTime          : 02:50:21
SqlTime.getTime(): 1563994221159

Or, you can use static valueOf(...) methods to get an instance of java.sql.Time object

  • static Time valueOf​(String s): Converts a string in JDBC time escape format to a Time value.
  • static Time valueOf(LocalTime time)​: Obtains an instance of Time from a LocalTime object with the same hour, minute and second time value as the given LocalTime. This static method available for Java 8 and above.
import java.sql.Time;
import java.time.LocalTime;

public class SqlTimeValueOfExample {
    public static void main(String[] args) {
        Time sqlTime1 = Time.valueOf("18:45:20");
        System.out.println("SqlTime1: " + sqlTime1);
        Time sqlTime2 = Time.valueOf(LocalTime.of(8, 15, 20, 1000));
        System.out.println("SqlTime2: " + sqlTime2);

SqlTime1: 18:45:20
SqlTime2: 08:15:20

java.sql.Time Methods

Some of methods of java.sql.Time are deprecated. It's because java.sql.Time extends java.util.Date and thus inherits its deprecated methods, or the methods is unusable for java.sql.Time which only keeps time, without date. Here some methods available for java.sql.Time:

  • void setTime(long time): Sets a Time object using a milliseconds time value.
  • LocalTime toLocalTime(): Converts this Time object to a LocalTime. This method available for Java 8 and above.
  • String toString(): Formats a time in JDBC time escape format.
import java.sql.Time;
import java.time.Instant;
import java.time.LocalTime;

public class SqlTimeMethodsExample {
    public static void main(String[] args) {
        long now = System.currentTimeMillis();
        Time sqlTime = new Time(now);
        System.out.println("SqlTime  : " + sqlTime);
        System.out.println("SqlTime  : " + sqlTime);
        LocalTime localTm = sqlTime.toLocalTime();
        System.out.println("LocalTime: " + localTm);
        Instant instant = sqlTime.toInstant();
        System.out.println("Instant  : " + instant);

Which will produce (result may vary)

SqlTime  : 03:03:12
SqlTime  : 10:15:20
LocalTime: 10:15:20
Exception in thread "main" java.lang.UnsupportedOperationException
	at java.sql.Time.toInstant(
	at com.dariawan.datetime.SqlTimeMethodsExample.main(

toInstant() method (available for Java 8 and above) always throws an UnsupportedOperationException and should not be used because SQL Time values do not have a date component.

And similar with java.sql.Date, since java.sql.Time extends java.util.Date, so java.util.Date methods also available for java.sql.Time:
import java.sql.Time;

public class SqlTimeInheritMethodsExample {
    public static void main(String[] args) {
        long now = System.currentTimeMillis();
        Time sqlTime1 = new Time(now);
        System.out.println("sqlTime1: " + sqlTime1);

        Time sqlTime2 = new Time(324126010000L);
        System.out.println("sqlTime2: " + sqlTime2);

        System.out.println("sqlTime1 after sqlTime2    : " + sqlTime1.after(sqlTime2));
        System.out.println("sqlTime2 after sqlTime1    : " + sqlTime2.after(sqlTime1));

        System.out.println("sqlTime1 before sqlTime2   : " + sqlTime1.before(sqlTime2));
        System.out.println("sqlTime2 before sqlTime1   : " + sqlTime2.before(sqlTime1));
        System.out.println("sqlTime1 compareTo sqlTime2: " + sqlTime1.compareTo(sqlTime2));
        System.out.println("sqlTime2 compareTo sqlTime1: " + sqlTime2.compareTo(sqlTime1));        

sqlTime1: 03:13:06
sqlTime2: 18:30:10

sqlTime1 after sqlTime2    : true
sqlTime2 after sqlTime1    : false
sqlTime1 before sqlTime2   : false
sqlTime2 before sqlTime1   : true
sqlTime1 compareTo sqlTime2: 1
sqlTime2 compareTo sqlTime1: -1

Check out java.util.Date for methods available.


Although java.sql.Time inherits from java.util.Date, java.sql.Time only keeps the time, not the date. On another hand, we already learn earlier that java.sql.Date keeps date only, and not time. If you need to keep both date and time, use a java.sql.Timestamp instead.