How do you say, add 1 hour to a given result from calendar.getTime?
-
1trying to figure out why this got down-voted... – codingbear Jul 22 '09 at 21:23
-
probably because its a one liner.. it wasn't exactly a complex question though. just new to java. Oh some people *do* suck, I think that could be it. – lsl Jul 22 '09 at 21:26
-
3Actually, it's a remarkably complex question when you get into DST changes. Do you always expect 01:30 + 1 hour to be 02:30, for instance? – Jon Skeet Jul 22 '09 at 21:34
-
It's a one liner *and* it's not terribly clear. `getTime` gives you a `Date`; is that what you really want to modify? – Michael Myers Jul 22 '09 at 21:34
-
1+1 to undo the downvote. Stack Overflow is supposed to be a place where people can ask legitimate questions and not be made to feel stupid about them. I don't think downvoting simple questions or answering them with "have you tried Googling for the answer?" is necessarily appropriate on SO. – Grant Wagner Jul 22 '09 at 21:37
-
Thankyou grant. Also, Jon, I don't think it is much of a complex question, but it sure is a complex answer. I have a server in new zealand and a server in the US that both require time expiring authentications. One other problem is the US box needs to talk to another US server which happens to be in a different time zone. :( – lsl Jul 22 '09 at 23:11
-
ps of course I tried to google the answer.. its one of those questions that just give very varied and broken search results. Especially when you need to get into timezone differences. Its the type of question SO was built to solve. – lsl Jul 22 '09 at 23:15
-
1@jim: For this sort of thing, I wouldn't use a calendar at all. Just use the UTC time everywhere - you don't even need to use a human-readable format for it most of the time, just use the number of milliseconds since the epoch. Then you can just add the number of milliseconds you want. Only format it to a human-readable form when you need to display it to someone. – Jon Skeet Jul 23 '09 at 06:38
3 Answers
Well, you can add 1000 * 60 * 60 to the millisecond value of the Date
. That's probably the simplest way, if you don't want to mutate the Calendar
instance itself. (I wouldn't like to guess exactly what Calendar
will do around DST changes, by the way. It may well not be adding an hour of UTC time, if you see what I mean.)
So if you do want to go with the Date approach:
date.setTime(date.getTime() + 1000 * 60 * 60);
This will always add an actual hour, regardless of time zones, because a Date
instance doesn't have a time zone - it's just a wrapper around a number of milliseconds since midnight on Jan 1st 1970 UTC.
However, I'd strongly advise (as I always do with Java date/time questions) that you use Joda Time instead. It makes this and myriad other tasks a lot easier and more reliable. I know I sound like a broken record on this front, but the very fact that it forces you to think about whether you're actually talking about a local date/time, just a date, a local midnight etc makes a big difference.

- 1,421,763
- 867
- 9,128
- 9,194
-
+1 for DST - incredibly easy to miss, next to impossible to reproduce and fix when it blows up in production 4 months later. BTW, this is the 2nd question I'm seeing today where Jon's answer is not the top one. Is the world as we know it coming to an abrupt end? :-) – ChssPly76 Jul 22 '09 at 21:40
-
as in depth and as helpful as your answer was, rich needs some noobie love and his answer really was exactly what I needed. Something I should have grabbed from the javadocs but missed :) – lsl Aug 04 '09 at 00:05
Calendar cal = Calendar.getInstance();
//cal.setTime(date); //if you need to pass in a date
cal.add(Calendar.HOUR, 1);

- 3,995
- 3
- 23
- 28
tl;dr
Instant.now().plusHours( 1 )
…or…
ZonedDateTime.now( ZoneId.of( "America/Montreal" ).plusHours( 1 )
Using java.time
The accepted Answer is correct but outdated.
The troublesome old legacy date-time classes have been supplanted by the java.time classes.
Instead of Calendar
, use ZonedDateTime
.
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.now( z );
ZonedDateTime zdtOneHourLater = zdt.plusHours( 1 );
Note that the wall-clock time may not be an hour later. Anomalies such as Daylight Saving Time (DST) means an hour later may appear to be two hours later jumping from 1 AM to 3 AM for DST switch-over.
For converting from old Calendar
to modern java.time types, see this Question.
Most of your work should be in UTC. For that, use the Instant
class.
Instant hourLater = Instant.now().plusHours( 1 );
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.

- 1
- 1

- 303,325
- 100
- 852
- 1,154