How to add 3 hours to the time that store in java.sql.Timestamp
by not using the deprecated API?
I use the below code, but it doesn't work.
Timestamp later = new Timestamp(old + (1000 * 60 * 60 * 3));
Assuming old
is a Timestamp
; your code is close. You just need to convert the old
timestamp to a millisecond value first. Do:
Timestamp later = new Timestamp(old.getTime() + (1000 * 60 * 60 * 3));
Neither getTime()
nor the Timestamp(long)
constructor are deprecated.
Note that all of this information is readily available in the Timestamp
documentation.
You can use Calendar for date manipulation:
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.HOUR, 3);
Timestamp timestamp = new Timestamp(calendar.getTimeInMillis());