Increment timestamp by one day

Discussion in 'Java' started by sconeek, Mar 15, 2006.

  1. sconeek

    sconeek Guest

    hi all,
    how would i increment the timestamp by one day. i currently have,
    java.sql.Timestamp timestampTo = new
    java.sql.Timestamp(localDateTo.getTime());

    now i want to increment the timestamp by one day.
    any help please.
    thanks.
     
    sconeek, Mar 15, 2006
    #1
    1. Advertisements

  2. sconeek

    JScoobyCed Guest

    // 84600000 milliseconds in a day
    long oneDay = 1 * 24 * 60 * 60 * 1000;
    // to add to the timestamp
    timestampTo.setTime(timestampTo.getTime() + oneDay);
     
    JScoobyCed, Mar 15, 2006
    #2
    1. Advertisements

  3. sconeek

    Oliver Wong Guest

    Does the OP need to take into account leap seconds and perhaps
    non-gregorian calendars? If so, it might be safer (though more work) to
    parse the timestamp into a Date Object of the appropriate calendar type,
    increment the date by one day, and then convert back to a timestamp.

    - Oliver
     
    Oliver Wong, Mar 15, 2006
    #3
  4. sconeek

    jakeScript

    Joined:
    Jul 24, 2009
    Messages:
    1
    Likes Received:
    0
    1 day == 86400000 not 84600000

    I know this is old but it is what came up when I searched for a one day timestamp.

    Here is a little JavaScript Function I wrote as well if it helps anyone.

    var changeDate = function(dayInterval) {
    var lengthOfInterval = dayInterval * 86400000;
    var currentDate = new Date().getTime();
    var newDate = currentDate + lengthOfInterval;
    return new Date(newDate).toGMTString();
    }

    Ex.

    var policy60Days = changeDate(60);

    If the current time is:
    Fri, 24 Jul 2009 17:47:08 GMT

    policy60Days is:
    Tue, 22 Sep 2009 17:47:08 GMT
     
    Last edited: Jul 24, 2009
    jakeScript, Jul 24, 2009
    #4
  5. sconeek

    sangeeta_lal

    Joined:
    Mar 3, 2012
    Messages:
    1
    Likes Received:
    0
    nice way suggested on stack overflow

    java.sql.Timestamp ts = ...

    Calendar cal = Calendar.getInstance();
    cal.setTime(min_date);
    cal.add(Calendar.DAY_OF_WEEK, 1);
    java.sql.Timestamp new_date = new java.sql.Timestamp(cal.getTime().getTime());

    System.out.println("new date Cal="+new_date);
     
    sangeeta_lal, Mar 3, 2012
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.