How do I create a java.sql.Time object?
Joe Sam Shirah java.sql.Time descends from java.util.Date, but uses only the hour, minute and second values. There are two methods to create a Time object. The first uses a Calendar object, setting the year, month and day portions to January 1, 1970, which is Java's zero epoch. The millisecond value must also be set to zero. At that point, Calendar.getTime().getTime() is invoked to get the time in milliseconds. That value is then passed to a Time constructor:
Calendar cal = Calendar.getInstance(); // set Date portion to January 1, 1970 cal.set( cal.YEAR, 1970 ); cal.set( cal.MONTH, cal.JANUARY ); cal.set( cal.DATE, 1 ); cal.set( cal.MILLISECOND, 0 ); java.sql.Time jsqlT = new java.sql.Time( cal.getTime().getTime() );The second method is Time's valueOf method. valueOf() accepts a String, which must be the time in JDBC time escape format - "hh:mm:ss". For example,
java.sql.Time jsqlT = java.sql.Time.valueOf( "18:05:00" );creates a Time object representing 6:05 p.m. To use this method with a Calendar object, use:
java.sql.Time jsqlT = java.sql.Time.valueOf( cal.get(cal.HOUR_OF_DAY) + ":" + cal.get(cal.MINUTE) + ":" + cal.get(cal.SECOND) );which produces a Time object with the same value as the first example.