lsl
lsl

Reputation: 4419

Adding java time objects

How do you say, add 1 hour to a given result from calendar.getTime?

Upvotes: 4

Views: 657

Answers (3)

Basil Bourque
Basil Bourque

Reputation: 339342

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.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1502066

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.

Upvotes: 7

Rich Kroll
Rich Kroll

Reputation: 4005

Calendar cal = Calendar.getInstance();
//cal.setTime(date); //if you need to pass in a date
cal.add(Calendar.HOUR, 1);

Upvotes: 4

Related Questions