1

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));
genie
  • 23
  • 1
  • 1
  • 4

2 Answers2

6

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.

Jason C
  • 38,729
  • 14
  • 126
  • 182
5

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());
Leos Literak
  • 8,805
  • 19
  • 81
  • 156