How can I return a Date object of 4 hours less than the current system time in Java?
7 Answers
If you're already on Java 8 or newer:
LocalDateTime fourHoursAgo = LocalDateTime.now().minusHours(4);
Or if you want to take DST (Daylight Saving Time) into account (just in case it coincidentally went into or out DST somewhere the last 4 hours):
ZonedDateTime fourHoursAgo = ZonedDateTime.now().minusHours(4);
Or if you're not on Java 8 yet:
Date fourHoursAgo = new Date(System.currentTimeMillis() - (4 * 60 * 60 * 1000));
And you want to take DST into account:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR_OF_DAY, -4);
Date fourHoursAgo = calendar.getTime();

- 1,082,665
- 372
- 3,610
- 3,555
The other answers are correct, but I would like to contribute the modern answer. The modern solution uses java.time
, the modern Java date and time API.
Instant fourHoursAgo = Instant.now().minus(Duration.ofHours(4));
System.out.println(fourHoursAgo);
This just printed:
2018-01-31T15:22:21.113710Z
The Z
in the end indicates that the time is printed in UTC — at UTC offset zero if you will. The Instant
class is the modern replacement for Date
, so I recommend you stick to it. The modern API is generally so much nicer to work with, so much cleaner, so much better designed.
Please note the advantages of letting the library class do the subtraction of 4 hours for you: the code is clearer to read and less error-prone. No funny constants, and no readers taking time to check if they are correct.
If you do need an old-fashioned Date
object, for example when using a legacy API that you cannot change or don’t want to change, convert like this:
Date oldfashionedDate = Date.from(fourHoursAgo);
Link: Oracle Tutorial trail: Date Time. Of course there are other resources on the internet too, please search.

- 4,023
- 4
- 26
- 43

- 81,772
- 15
- 137
- 161
-
Next time just vote for close as dupe instead of posting duplicate answers. – BalusC Feb 01 '18 at 07:37
-
See your point, @BalusC. I would’ve if I had considered them exact duplicates (there’s a substantial overlap between my two answers, but they are not identical). – Ole V.V. Feb 01 '18 at 07:45
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR_OF_DAY, -4);
calendar.getTime();

- 9,097
- 2
- 31
- 53
-
2`Calendar.HOUR` is 12-hour (AM/PM) based, not 24-hour based. You want `Calendar.HOUR_OF_DAY`. Also, using `Calendar#getInstance()` factory is preferred over constructing a region-specific calendar manually. – BalusC Dec 03 '10 at 18:24
Convert it to milliseconds, subtract the number of milliseconds in 4 hours, convert it back to a Date.

- 73,784
- 33
- 194
- 347
Calendar c = Calendar.getInstance();
c.add(Calendar.HOUR_OF_DAY, -4);
java.util.Date d = c.getTime();
System.out.println(d);

- 39,162
- 17
- 99
- 152
Calendar c =Calendar.getInstance() ;
c.add(Calendar.HOUR,-4);
Date d = c.getTime();

- 2,889
- 2
- 27
- 38
-
-
The roll method does only work between 4:00 (am) and 23:59. Otherwise you will get a wrong day. – Robert Dec 03 '10 at 18:24
Use a Calendar
object and the add
method.
calendar.add(Calendar.HOUR, -4);
See http://download.oracle.com/javase/6/docs/api/java/util/Calendar.html

- 4,967
- 2
- 28
- 35