Timestamp

Discussion in 'Java' started by angelochen960@gmail.com, Mar 1, 2009.

  1. Guest

    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
    , Mar 1, 2009
    #1
    1. Advertising

  2. wrote:
    > 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,
    >


    You could use java.util.Calendar,
    http://blogs.techrepublic.com.com/howdoi/?p=116.

    Obviously, you'll have to convert from java.sql.Timestamp to
    java.util.Calendar and back again.

    Or wait for JSR 310?

    --
    RGB
    RedGrittyBrick, Mar 1, 2009
    #2
    1. Advertising

  3. Lew Guest

    wrote:
    >> 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,


    RedGrittyBrick wrote:
    > You could use java.util.Calendar,
    > http://blogs.techrepublic.com.com/howdoi/?p=116.
    >
    > Obviously, you'll have to convert from java.sql.Timestamp to
    > java.util.Calendar and back again.
    >
    > Or wait for JSR 310?


    More information about Calendar:
    <http://java.sun.com/javase/6/docs/api/java/util/Calendar.html>

    --
    Lew
    Lew, Mar 1, 2009
    #3
  4. Arne Vajhøj Guest

    wrote:
    > 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
    Arne Vajhøj, Mar 1, 2009
    #4
  5. Arne Vajhøj wrote:
    > wrote:
    >> 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


    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
    Mark Thornton, Mar 1, 2009
    #5
  6. Arne Vajhøj Guest

    Mark Thornton wrote:
    > Arne Vajhøj wrote:
    >> wrote:
    >>> 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);

    >
    > 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
    Arne Vajhøj, Mar 1, 2009
    #6
  7. Lew Guest

    Arne Vajhøj wrote:
    > Mark Thornton wrote:
    >> Arne Vajhøj wrote:
    >>> wrote:
    >>>> 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);

    >>
    >> 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.


    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.

    --
    Lew
    Lew, Mar 1, 2009
    #7
  8. Lew wrote:
    > Arne Vajhøj wrote:
    >> Mark Thornton wrote:
    >>> Arne Vajhøj wrote:
    >>>> wrote:
    >>>>> 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);
    >>>
    >>> 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.

    >
    > 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
    Arne Vajhøj, Mar 1, 2009
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manzoorul Hassan

    Convert to NTP Timestamp

    Manzoorul Hassan, Mar 11, 2005, in forum: Perl
    Replies:
    1
    Views:
    2,706
    Jim Gibson
    Mar 11, 2005
  2. Shawn

    displaying sybase timestamp

    Shawn, Jan 27, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    3,146
    Shawn
    Jan 27, 2004
  3. Greg Collins [MVP]

    File Timestamp and Saving Transformed XML to Server

    Greg Collins [MVP], Jul 8, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    462
    Greg Collins [MVP]
    Jul 12, 2004
  4. Roger Twomey

    Using SQL Server Timestamp

    Roger Twomey, Sep 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    23,757
    Greg Burns
    Sep 10, 2004
  5. =?Utf-8?B?bXliYXBweQ==?=
    Replies:
    7
    Views:
    13,062
    =?Utf-8?B?Zng=?=
    Oct 20, 2004
Loading...

Share This Page