DEVFYI - Developer Resource - FYI

What's the fastest way to normalize a Time object?

JDBC Interview Questions and Answers


(Continued from previous question...)

What's the fastest way to normalize a Time object?

Of the two recommended ways when using a Calendar( see How do I create a java.sql.Time object? ), in my tests, this code ( where c is a Calendar and t is a Time ):

c.set( Calendar.YEAR, 1970 );
c.set( Calendar.MONTH, Calendar.JANUARY );
c.set( Calendar.DATE, 1 );
c.set( Calendar.MILLISECOND, 0 );

t = new java.sql.Time( c.getTime().getTime() );
was always at least twice as fast as:

t = java.sql.Time.valueOf(
c.get(Calendar.HOUR_OF_DAY) + ":" +
c.get(Calendar.MINUTE) + ":" +
c.get(Calendar.SECOND) );

When the argument sent to valueOf() was hardcoded ( i.e. valueOf( "13:50:10" ), the time difference over 1000 iterations was negligible.

(Continued on next question...)

Other Interview Questions