Java — Add/subtract years, months, days, hours, minutes, or seconds to a Date & Time | Code Factory

Add/Subtract years, months, days, hours, minutes, seconds to a Date using Calendar class

package com.example.java.programming.datetime;import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @author code.factory
*
*/
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));
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.YEAR, 2);
c.add(Calendar.MONTH, 1);
c.add(Calendar.DATE, -2);
c.add(Calendar.HOUR, -1);
c.add(Calendar.MINUTE, 20);
c.add(Calendar.SECOND, 10);
Date currentDatePlusOne = c.getTime(); System.out.println("Updated Date : " + dateFormat.format(currentDatePlusOne));
}
}
Current Date : 2020-07-01 15:59:55
Updated Date : 2022-07-30 15:20:05

Add/Subtract years, months, weeks, days, hours, minutes, seconds to LocalDateTime

package com.example.java.programming.datetime;import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
/**
* @author code.factory
*
*/
public class LocalDateTimeAddSubtractExample {
public static void main(String... args) {
LocalDateTime dateTime = LocalDateTime.of(2020, 7, 25, 10, 30, 50);
LocalDateTime newDateTime = dateTime.minusYears(3).plusMonths(1).minusWeeks(4).plusDays(2).minusHours(2);
System.out.println(newDateTime);
newDateTime = dateTime.plusYears(3).minusMonths(1).plusWeeks(4).minusDays(2).plusMinutes(30);
System.out.println(newDateTime);
System.out.println(dateTime.plus(2, ChronoUnit.DAYS));
}
}
2017-07-30T08:30:50
2023-07-21T11:00:50
2020-07-27T10:30:50

Add/Subtract days, weeks, months, years to LocalDate

package com.example.java.programming.datetime;import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
/**
* @author code.factory
*
*/
public class LocalDateAddSubtractExample {
public static void main(String... args) {
LocalDate date = LocalDate.of(2020, 07, 01);
LocalDate newDate = date.plusYears(1).minusMonths(2).plusWeeks(3).minusDays(4);
System.out.println(newDate);
newDate = date.minusYears(4).plusMonths(3).minusWeeks(2).plusDays(1);
System.out.println(newDate);
System.out.println(date.plus(2, ChronoUnit.DAYS));
}
}
2021-05-18
2016-09-18
2020-07-03

Add/Subtract hours, minutes, seconds to LocalTime

package com.example.java.programming.datetime;import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
/**
* @author code.factory
*
*/
public class LocalTimeAddSubtractExample {
public static void main(String... args) {
LocalTime time = LocalTime.of(11, 22, 33);
LocalTime newTime = time.plusHours(1).plusMinutes(2).plusSeconds(3);
System.out.println(newTime);
LocalTime updatedTime = time.minusHours(10).minusMinutes(30).minusSeconds(50);
System.out.println(updatedTime);
System.out.println(time.plus(1, ChronoUnit.HOURS));
}
}
12:24:36
00:51:43
12:22:33

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Coding is the future. Are you ready for it?

Putting customers first with SLIs and SLOs (Part 2)

Node.js Best Practices — Monitoring and Testing

Project 6 — Curated event microsite

Initial Results for IOST 2020 Q2 Contribution Rankings and Rewards

Two (very different) Algorithms for Generating Permutations

Db2 Event Store Architectural Series: Indexing

Write Simple BTC Quantitative Trading System Using Python in One Hour

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Major Features of Java 17

java internship in Kochi

Java Coding Standards

Java as a first programming language?

Java Coding Standards