In this article, you’ll find several ways of adding or subtracting years, months, days, hours, minutes, or seconds to a Date in Java.
Add/Subtract years, months, days, hours, minutes, seconds to a Date using Calendar class
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CalendarAddSubtractExample {
public static void main(String[] args) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
System.out.println("Current Date " + dateFormat.format(date));
// Convert Date to Calendar
Calendar c = Calendar.getInstance();
c.setTime(date);
// Perform addition/subtraction
c.add(Calendar.YEAR, 2);
c.add(Calendar.MONTH, 1);
c.add(Calendar.DATE, -10);
c.add(Calendar.HOUR, -4);
c.add(Calendar.MINUTE, 30);
c.add(Calendar.SECOND, 50);
// Convert calendar back to Date
Date currentDatePlusOne = c.getTime();
System.out.println("Updated Date " + dateFormat.format(currentDatePlusOne));
}
}
# Output
Current Date 2020-02-24 19:35:00
Updated Date 2022-03-14 16:05:50
Add/Subtract years, months, weeks, days, hours, minutes, seconds to LocalDateTime
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
public class LocalDateTimeAddSubtractExample {
public static void main(String[] args) {
LocalDateTime dateTime = LocalDateTime.of(2020, 1, 26, 10, 30, 45);
// Add years, months, weeks, days, hours, minutes, seconds to LocalDateTime
LocalDateTime newDateTime = dateTime.plusYears(3).plusMonths(1).plusWeeks(4).plusDays(2).plusHours(2);
System.out.println(newDateTime);
// Subtract years, months, weeks, days, hours, minutes, seconds to LocalDateTime
newDateTime = dateTime.minusYears(3).minusMonths(1).minusWeeks(4).minusDays(2).minusMinutes(30);
System.out.println(newDateTime);
// Add/Subtract using the generic plus/minus method
System.out.println(dateTime.plus(2, ChronoUnit.DAYS));
}
}
# Output
2023-03-28T12:30:45
2016-11-26T10:00:45
2020-01-28T10:30:45
Add/Subtract days, weeks, months, years to LocalDate
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class LocalDateAddSubtractExample {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2020, 1, 26);
// Add years, months, weeks, days to LocalDate
LocalDate newDate = date.plusYears(3).plusMonths(1).plusWeeks(4).plusDays(2);
System.out.println(newDate);
// Subtract years, months, weeks, days to LocalDate
newDate = date.minusYears(3).minusMonths(1).minusWeeks(4).minusDays(2);
System.out.println(newDate);
// Add/Subtract using the generic plus/minus method
System.out.println(date.plus(2, ChronoUnit.DAYS));
}
}
2023-03-28
2016-11-26
2020-01-28
Add/Subtract hours, minutes, seconds to LocalTime
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
public class LocalTimeAddSubtractExample {
public static void main(String[] args) {
LocalTime time = LocalTime.of(11, 45, 20);
// Add hours, minutes, or seconds
LocalTime newTime = time.plusHours(2).plusMinutes(30).plusSeconds(80);
System.out.println(newTime);
// Subtract hours, minutes, or seconds
LocalTime updatedTime = time.minusHours(2).minusMinutes(30).minusSeconds(30);
System.out.println(updatedTime);
// Add/Subtract using the generic plus/minus method
System.out.println(time.plus(1, ChronoUnit.HOURS));
}
}
# Output
14:16:40
09:14:50
12:45:20