From: Hole on
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...

Please, if you have spare time, could you look at the following code
and test it with some dates?:

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;
}

This code strangely returns wrong hours, but only whit particular
dates. For example, time from 2009-03-29 02:00:00 to 2009-03-29
03:00:00 (this excluded) returns a timestamp of one hour more...

This is the output of a test program I made:

Original: 2009-03-29 01:45:00
toSqlTimestamp: 2009-03-29 01:45:00

Original: 2009-03-29 02:00:00
toSqlTimestamp: 2009-03-29 03:00:00

Original: 2009-03-29 02:15:00
toSqlTimestamp: 2009-03-29 03:15:00

Original: 2009-03-29 02:30:00
toSqlTimestamp: 2009-03-29 03:30:00

Original: 2009-03-29 02:45:00
toSqlTimestamp: 2009-03-29 03:45:00

Original: 2009-03-29 03:00:00
toSqlTimestamp: 2009-03-29 03:00:00

From: Lew on
Hole wrote:
> I've noticed some strange problems in my app...after few hours of
> debugging I found out that problems are in java.sql.Timestamp...
>
> Please, if you have spare time, could you look at the following code
> and test it with some dates?:
>
> public static Timestamp toSqlTimestamp(String date, String fmt) throws
> ParseException, Exception {

Declaring 'throws Exception' is an antipattern.

> Timestamp res = null;
> try {
> SimpleDateFormat f = new SimpleDateFormat(fmt);
> res = new Timestamp(f.parse(date).getTime());
> } catch(ParseException pexc) {
> throw pexc;

This catch clause is redundant since the method already rethrows the exception
without it.

> } catch(Exception exc) {

Catching 'Exception' is usually an antipattern.

Failing to log or handle exceptions is an antipattern.

> throw exc;
> }
> return res;
> }
>
> This code strangely returns wrong hours, but only whit particular
> dates. For example, time from 2009-03-29 02:00:00 to 2009-03-29
> 03:00:00 (this excluded) returns a timestamp of one hour more...
>
> This is the output of a test program I made:
>
> Original: 2009-03-29 01:45:00
> toSqlTimestamp: 2009-03-29 01:45:00
>
> Original: 2009-03-29 02:00:00
> toSqlTimestamp: 2009-03-29 03:00:00
>
> Original: 2009-03-29 02:15:00
> toSqlTimestamp: 2009-03-29 03:15:00
>
> Original: 2009-03-29 02:30:00
> toSqlTimestamp: 2009-03-29 03:30:00
>
> Original: 2009-03-29 02:45:00
> toSqlTimestamp: 2009-03-29 03:45:00
>
> Original: 2009-03-29 03:00:00
> toSqlTimestamp: 2009-03-29 03:00:00

What time zone is the 'DateFormat' under? I'm guessing that its Standard Time
is one hour after GMT, say in Europe somewhere.

You would get similar results if you used the DateFormat to produce a
java.util.Date instead of a java.sql.Timestamp.

--
Lew
From: Patricia Shanahan on
Lew wrote:
> Hole wrote:
>> I've noticed some strange problems in my app...after few hours of
>> debugging I found out that problems are in java.sql.Timestamp...
>>
>> Please, if you have spare time, could you look at the following code
>> and test it with some dates?:
>>
>> public static Timestamp toSqlTimestamp(String date, String fmt) throws
>> ParseException, Exception {
>
> Declaring 'throws Exception' is an antipattern.
>
>> Timestamp res = null;
>> try {
>> SimpleDateFormat f = new SimpleDateFormat(fmt);
>> res = new Timestamp(f.parse(date).getTime());
>> } catch(ParseException pexc) {
>> throw pexc;
>
> This catch clause is redundant since the method already rethrows the
> exception without it.
>
>> } catch(Exception exc) {
>
> Catching 'Exception' is usually an antipattern.
>
> Failing to log or handle exceptions is an antipattern.
>
>> throw exc;
>> }
>> return res;
>> }
>>
>> This code strangely returns wrong hours, but only whit particular
>> dates. For example, time from 2009-03-29 02:00:00 to 2009-03-29
>> 03:00:00 (this excluded) returns a timestamp of one hour more...
>>
>> This is the output of a test program I made:
>>
>> Original: 2009-03-29 01:45:00
>> toSqlTimestamp: 2009-03-29 01:45:00
>>
>> Original: 2009-03-29 02:00:00
>> toSqlTimestamp: 2009-03-29 03:00:00
>>
>> Original: 2009-03-29 02:15:00
>> toSqlTimestamp: 2009-03-29 03:15:00
>>
>> Original: 2009-03-29 02:30:00
>> toSqlTimestamp: 2009-03-29 03:30:00
>>
>> Original: 2009-03-29 02:45:00
>> toSqlTimestamp: 2009-03-29 03:45:00
>>
>> Original: 2009-03-29 03:00:00
>> toSqlTimestamp: 2009-03-29 03:00:00
>
> What time zone is the 'DateFormat' under? I'm guessing that its
> Standard Time is one hour after GMT, say in Europe somewhere.

Also, does that time zone do daylight saving time, and if so when do
clocks go forward? I suspect that may be involved because the OP says
only some dates are affected, and the examples of affected times are
around 2 a.m. on a Sunday morning, a popular time for adjustments.

Patricia
From: markspace on
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)
From: Hole on
On Jul 2, 3:37 pm, markspace <nos...(a)nowhere.com> wrote:
> 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.  

Hi,

Thanks to anyone for every input and for your help.

Yes, indeed I've just came out with the same thing...it's the DST
problem.
My TZ is GMT+1...
Now, how to set a TZ in order to not taking into account the DST? I
would deal with a TZ free of DST issue...