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

Joe Sam Shirah

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.
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.