Timestamp

A

angelochen960

Hi,

I have a variable:

java.sql.Timestamp timeNow;

I'd like to get another Timestamp variable that is 6 hours from
timeNow, how to do this? Thanks,

Angelo
 
A

Arne Vajhøj

I have a variable:

java.sql.Timestamp timeNow;

I'd like to get another Timestamp variable that is 6 hours from
timeNow, how to do this? Thanks,

Timestamp ts2 = new Timestamp(ts1.getTime() + 6 * 60 * 60 * 1000L);

Arne
 
M

Mark Thornton

Arne said:
Timestamp ts2 = new Timestamp(ts1.getTime() + 6 * 60 * 60 * 1000L);

Arne

Unless the OP wants a 6 hour wall clock difference in which case it
might be necessary to allow for standard/daylight time transitions.
Currently Calendar is the best way to solve that case.

Mark Thornton
 
A

Arne Vajhøj

Mark said:
Unless the OP wants a 6 hour wall clock difference in which case it
might be necessary to allow for standard/daylight time transitions.
Currently Calendar is the best way to solve that case.

The above +6 hours in time.

Calendar can be used to get +6 hours on the clock, but the code
would not be a simple add.

Arne
 
L

Lew

Arne said:
The above +6 hours in time.

Calendar can be used to get +6 hours on the clock, but the code
would not be a simple add.

Depends on how simple you demand that "simple" be.

I see nothing too complex about:

Calendar cal = Calendar.getInstance();
cal.setTime( timeNow );
cal.add( Calendar.HOUR_OF_DAY, 6 );
Timestamp futre = new Timestamp( cal.getTimeInMillis() );

Actually this is easier to read, I think, than the 'ts2' calculation above,
and has the virtue of accounting for DST.
 
A

Arne Vajhøj

Lew said:
Depends on how simple you demand that "simple" be.

I see nothing too complex about:

Calendar cal = Calendar.getInstance();
cal.setTime( timeNow );
cal.add( Calendar.HOUR_OF_DAY, 6 );
Timestamp futre = new Timestamp( cal.getTimeInMillis() );

Hm.

That code does +6 hours in time not the +6 hours on clock that
I called "would not be a simple add".
Actually this is easier to read, I think, than the 'ts2' calculation
above,

It is more readable.
and has the virtue of accounting for DST.

It does the exact same as my code regarding changes to and from DST.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top