Add or Subtract Hours, Minutes and Seconds in Java
Learn to add or subtract hours, minutes or seconds from a given date and time in Java using various date-time classes. If we require adding or subtracting the days and months, read the linked article.
1. Add or Subtract Time Since Java 8
Using the new Date API is the recommended approach if we use JDK 1.8 or later.
The following classes are part of the new API that can store and manipulate the time information for a given date.
The Duration class represents the amount of time in seconds and nanoseconds, and accessed using other duration-based units, such as minutes and hours. We can add or subtract a Duration from any class above.
1.2. Adding Hours, Minutes and Seconds
The LocalDateTime, ZoneDateTime and OffsetDateTime classes are an immutable representation of a date-time to a precision of nanoseconds. These classes support the plus methods to add the time to the date.
- plusHours(n) : returns a copy of given date-time object with the ‘n’ hours added.
- plusMinutes(n) : returns a copy of given date-time object with the ‘n’ minutes added.
- plusSeconds(n) : returns a copy of given date-time object with the ‘n’ seconds added.
- plusNanos(n) : returns a copy of given date-time object with the ‘n’ nano-seconds added.
- plus(duration) : returns a copy of given date-time object with the specified Duration added.
- plus(n, temporalUnit) : returns a copy of given date-time object with ‘n’ amount of specified unit added.
Java program to add hours and other time units to a given date-time. We are writing the examples using the LocalDateTime class, but all the statements are valid for ZoneDateTime and OffsetDateTime classes.
LocalDateTime updatedTime LocalDateTime now = LocalDateTime.now(); updatedTime = now.plusHours(2); updatedTime = now.plusMinutes(20); updatedTime = now.plusSeconds(300); updatedTime = now.plus(Duration.ofMillis(8000)); updatedTime = now.plus(20, ChronoUnit.HOURS);
The Instant class is meant to be representing a date. It is for representing a single instantaneous point on the timeline or epoch-seconds. It does not provide plusHours and plusMinutes methods.
Instant updatedInstant; Instant currentInstant = Instant.parse("2022-06-24T05:12:35Z"); updatedInstant = currentInstant.plus(2, ChronoUnit.HOURS); updatedInstant = currentInstant.plus(30, ChronoUnit.MINUTES); updatedInstant = currentInstant.plusSeconds(300); updatedInstant = currentInstant.plusMillis(8000 updatedInstant = currentInstant.plusNanos(60000
1.3. Subtracting Hours, Minutes and Seconds
Similar to plus methods, these classes provide a way to subtract any amount of time. We need to use the minus methods listed above.
LocalDateTime updatedTime LocalDateTime now = LocalDateTime.now(); updatedTime = now.minusHours(2); updatedTime = now.minusMinutes(20); updatedTime = now.minusSeconds(300); updatedTime = now.minus(Duration.ofMillis(8000)); updatedTime = now.minus(20, ChronoUnit.HOURS);
Similarly, for the Instant class we can use the minus methods.
Instant updatedInstant; Instant currentInstant = Instant.parse("2022-06-24T05:12:35Z"); updatedInstant = currentInstant.minus(2, ChronoUnit.HOURS); updatedInstant = currentInstant.minus(30, ChronoUnit.MINUTES); updatedInstant = currentInstant.minusSeconds(300); updatedInstant = currentInstant.minusMillis(8000 updatedInstant = currentInstant.minusNanos(600000);
2. Add or Subtract Time – Java 7
Adding and subtracting time was possible through the Calendar class. There was no direct support in the Date class.
We can use the cal.add(unit, amount) method for adding and subtracting time.
- If the amount was positive number then specified amount of specified unit of time is added to the calendar.
- If the amount was negative number then specified amount of specified unit of time is subtracted from the calendar.
Date date = new Date(); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.HOUR, 2); cal.add(Calendar.MINUTE, -15); cal.add(Calendar.SECOND, 10);
Note that Calendar is a mutable object so all changes are made to the given Calendar instance itself. No new Calendar instance is created.
In this tutorial, we learned to add and subtract the time (in hours, minutes and seconds) to a date in Java. We learned to use the new Java Date APIs as well as the old legacy Date and Calendar classes.