Increment timestamp by one day

S

sconeek

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

JScoobyCed

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.

// 84600000 milliseconds in a day
long oneDay = 1 * 24 * 60 * 60 * 1000;
// to add to the timestamp
timestampTo.setTime(timestampTo.getTime() + oneDay);
 
O

Oliver Wong

JScoobyCed said:
// 84600000 milliseconds in a day
long oneDay = 1 * 24 * 60 * 60 * 1000;
// to add to the timestamp
timestampTo.setTime(timestampTo.getTime() + oneDay);

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
 
Joined
Jul 24, 2009
Messages
1
Reaction score
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:
Joined
Mar 3, 2012
Messages
1
Reaction score
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);
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top