djechlin djechlin - 2 months ago 5
SQL Question

Java SQL subtract an hour from date

I need a

java.sql.Timestamp
value corresponding to one hour ago. I have a few date related things happening here so it's important that the current time is sampled only once and the rest of the code uses that same time. As far as setting a
Timestamp
based on current time in ms.

Manipulating the
Calendar
object seems really klunky since I have to do things like add and subtract time to the calendar, which modifies it, so then I would have the lovely pleasure of either cloning myself a copy first or toggling its value around.

Answer

Simply subtract an hour off of the current time in milliseconds when making your timestamp (1 hour = 60 * 60 * 1000 ms)

Timestamp oneHourAgo = new Timestamp(System.currentTimeMillis() - (60 * 60 * 1000));
Comments