27

How can I return a Date object of 4 hours less than the current system time in Java?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
ʞɔıu
  • 47,148
  • 35
  • 106
  • 149

7 Answers7

66

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();
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
17

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.

Jonny Henly
  • 4,023
  • 4
  • 26
  • 43
Ole V.V.
  • 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
6
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR_OF_DAY, -4);
calendar.getTime();
Nicolas Repiquet
  • 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
2

Convert it to milliseconds, subtract the number of milliseconds in 4 hours, convert it back to a Date.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
2
Calendar c = Calendar.getInstance();
c.add(Calendar.HOUR_OF_DAY, -4);
java.util.Date d = c.getTime();
System.out.println(d);
Robert
  • 39,162
  • 17
  • 99
  • 152
1
Calendar c =Calendar.getInstance() ;
c.add(Calendar.HOUR,-4);
Date d = c.getTime();
Allan
  • 2,889
  • 2
  • 27
  • 38
1

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

Avrom
  • 4,967
  • 2
  • 28
  • 35