Re: java.sql.Timestamp: Bug or...
Hole wrote:
Hi there,
I've noticed some strange problems in my app...after few hours of
debugging I found out that problems are in java.sql.Timestamp...
Like Lew and Patricia, I'm thinking "daylight savings time" is messing
you up. However, I can't duplicate your issue here. A complete SSCCE
would be appreciated. Also, what is your time zone? And lastly please
look up some downloads from Sun for correcting time zone information,
it's a pain to keep all of it straight and you might have a buggy
install if you haven't updated recently. (You'll have to Google for the
updates, I don't have links handy.)
Here's my full example and output.
package test;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/**
*
* @author Brenden
*/
public class TimeStampTest
{
static String testStrings[] =
{
"2009-03-29 01:45:00",
"2009-03-29 02:00:00",
"2009-03-29 02:15:00",
"2009-03-29 02:30:00",
"2009-03-29 02:45:00",
"2009-03-29 03:00:00",
};
public static void main( String[] args ) throws Exception
{
for( String test : testStrings ) {
System.out.println( "Original: "+test );
System.out.println( "Timestamp: "+ toSqlTimestamp( test,
"yyyy-MM-dd HH:mm:ss" ) );
}
}
public static Timestamp toSqlTimestamp( String date, String fmt )
throws
ParseException, Exception
{
Timestamp res = null;
try
{
SimpleDateFormat f = new SimpleDateFormat( fmt );
res = new Timestamp( f.parse( date ).getTime() );
} catch( ParseException pexc )
{
throw pexc;
} catch( Exception exc )
{
throw exc;
}
return res;
}
}
run:
Original: 2009-03-29 01:45:00
Timestamp: 2009-03-29 01:45:00.0
Original: 2009-03-29 02:00:00
Timestamp: 2009-03-29 02:00:00.0
Original: 2009-03-29 02:15:00
Timestamp: 2009-03-29 02:15:00.0
Original: 2009-03-29 02:30:00
Timestamp: 2009-03-29 02:30:00.0
Original: 2009-03-29 02:45:00
Timestamp: 2009-03-29 02:45:00.0
Original: 2009-03-29 03:00:00
Timestamp: 2009-03-29 03:00:00.0
BUILD SUCCESSFUL (total time: 1 second)