Currently I have a Date object representing a time. How would I add 5 minutes to this object?
6 Answers
You could use Calendar
, which will make it easy to add any length of time:
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MINUTE, 5);
Date newDate = cal.getTime();
For your case you could just add the time in milliseconds like this:
Date newDate = new Date(date.getTime() + 5 * 60 * 1000L);

- 70,765
- 18
- 106
- 111
Consider using Joda-Time. It is a library that makes date and time handling far more pleasant than the built-in ones.
The DateTime
class offers a plusMinutes
method.
DateTime now = DateTime.now( DateTimeZone.forID( "America/Montreal" ) );
DateTime inFiveMinutes = now.plusMinutes( 5 );

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

- 99
- 1
-
4And is completely overkill for such a simple task. – Heiko Rupp May 05 '11 at 08:44
-
5@Heiko: not if you do a lot of such simple tasks. – Michael Borgwardt May 05 '11 at 08:49
-
2@HeikoRupp If someone is manipulating date-time values by adding minutes, they are probably doing many other date-time tasks. Those seemingly simple tasks quickly become complicated, confusing, error-prone, and difficult to debug without a decent date-time library such as [Joda-Time](http://www.joda.org/joda-time/) or the [java.time package](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) in Java 8 (inspired by Joda-Time, defined by [JSR 310](https://jcp.org/en/jsr/detail?id=310)). – Basil Bourque Sep 19 '14 at 09:01
-
2FYI, the [*Joda-Time*](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), advising migration to the [*java.time*](http://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jul 03 '19 at 22:27
With Java 8 there are new options. In fact Joda Time recommends moving to Java 8 Date and Time APIs as soon as possible. Here is an example of adding time from this link:
https://docs.oracle.com/javase/tutorial/datetime/iso/period.html
The following code adds 5 minutes to the current time:
LocalDateTime now = LocalDateTime.now();
now = now.plusMinutes(5);
System.out.println(now);

- 641
- 6
- 17
-
1`LocalDateTime` is the wrong class for this purpose as it purposely has no concept of time zone. – Basil Bourque Oct 27 '17 at 08:11
-
-
@JeremyBunn The Question asks about a `java.util.Date`. That class represents a moment in UTC. In contrast, the `LocalDateTime` class does not. `LocalDateTime` purposely lacks any concept of time zone of offset-from-UTC, therefore **`LocalDateTime` cannot represent a moment**. In *java.time*, the equivalent to `java.util.Date` is `java.time.Instant`. Thus the presence of the new `Date::toInstant()` conversion method added to that old class. I cannot imagine any situation in which calling `LocalDateTime.now()` is the right thing to do. – Basil Bourque Jul 03 '19 at 22:25
Date has the time in milli-seconds. However, you might find using a long
is simpler for this type of calculations.
Date date1 = new Date();
long time1 = date1.getTime();
long time2 = time1 + 5 * 60 * 1000;
Date date2 = new Date(time2);
If you use plain long
you can drop the lines with Date objects.

- 17,658
- 5
- 50
- 82

- 525,659
- 79
- 751
- 1,130
tl;dr
myDate.toInstant()
.plus( 5 , ChronoUnit.MINUTES )
You don’t
The Date
class is part of the troublesome old date-classes that should no longer be used. They are supplanted by the java.time classes.
Instant
Convert your Date
to an Instant
using new method added to the old class. Both represent a moment on the timeline in UTC but the newer class has a finer resolution of nanoseconds.
Instant instant = myDate.toInstant() ;
Adding five minutes is easy.
Instant later = instant.plus( 5 , ChronoUnit.MINUTES ) ;
Or use a Duration
.
Duration d = Duration.ofMinutes( 5 ) ;
Instant later = instant.plus( d ) ;

- 303,325
- 100
- 852
- 1,154
In Java 7 you might use DateUtils class from Apache Commons library:
Date dateWithFiveMinutesAdded = DateUtils.addMinutes(date, 5);
or Joda-Time Library:
Date dateWithFiveMinutesAdded = new DateTime(date).plusMinutes(5).toDate();
If you use Java 8 and you don't want to use any external libraries consider new built in Java 8 Date/Time API:
Date dateWithFiveMinutesAdded = Date.from(date.toInstant().plus(5, ChronoUnit.MINUTES));
or alternatively:
Date dateWithFiveMinutesAdded = Date.from(date.toInstant().plusSeconds(5 * 60));
However with Java 8 you should use rather Instant, ZonedDateTime or LocalDateTime instead of Date.
Obviously there are many more ways to do it, but in my opinion those are quite clean and compact when it comes to code clearness.

- 3,435
- 33
- 41
-
`LocalDateTime` is the wrong class for this purpose as it purposely has no concept of time zone. – Basil Bourque Oct 27 '17 at 08:11
-
`Date` also has no concept of time zone. Converting it to `LocalDateTime` and backwards with `ZoneOffset.UTC (+0)` doesn't change time zone so everything looks fine for me. Can you give an example of situation in which something will go wrong with this conversion? – luke Oct 28 '17 at 13:53
-
Incorrect, `java.util.Date` *does* have a concept of time zone: UTC. A `Date` is a count from the first moment of 1970 in UTC (ignoring leap seconds). So **a `Date` is always in UTC**, and represents a specific point on the timeline. A `LocalDateTime` has no association with any zone or offset, and does *not* represent a moment on the timeline. So while the numerical representation of the date-time might work out, the semantics of your solution is wrong. The replacement in java.time for `java.util.Date` is `Instant`, not `LocalDateTime` nor `ZonedDateTime`. – Basil Bourque Oct 28 '17 at 15:36
-
For more discussion, see: [Java 8: What's the difference between Instant and LocalDateTime?](https://stackoverflow.com/q/32437550/642706) – Basil Bourque Oct 28 '17 at 16:12
-