I fail to add seconds to Java Timestamp.
I have this, but, it gives me the same date:
int sec = 600;
java.sql.Timestamp ts_from_ws = new java.sql.Timestamp(retry_date);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(ts_from_ws.getTime());
cal.add(Calendar.SECOND,sec);
java.sql.Timestamp ts_new_date_ws = new java.sql.Timestamp(cal.getTime().getTime());
I've always favoured brevity:
or if you want relative to "now":
In java 8, you can leverage the new
java.time.Instant
. Injava.sql.TimeStamp
, two new method are added from java 1.8:public static Timestamp from(Instant instant)
andpublic Instant toInstant()
.So to add sec, we can convert the
TimeStamp
to Instant and then callplusSeconds
and then back to TimeStamp:Or one line:
The code you've got works for me. As a short but complete program:
Output:
Note the difference of 10 minutes, i.e. 600 seconds.
Of course you lose the sub-millisecond precision this way, which may well not be ideal - and it goes against what I'm normally use a timestamp for in the first place - but it does add the seconds...
Another option would be to just use
Timestamp
directly: