How to increment and decrement date using Java?
Before understanding how to increment and decrement the date, one must know about the Calendar class in Java.
The Java calendar class offers methods for converting dates between a given moment in time and a variety of calendar fields, including MONTH, YEAR, HOUR, etc. It implements the Comparable, Serializable, and Cloneable interfaces and derives from the Object class.
We are unable to create an instance of it using a constructor because it is an abstract class. To create and implement a sub-class, we must instead utilize the static method Calendar.getInstance().
Calendar.getInstance(): return a Calendar instance based on the current time in the default time zone with the default locale.
Calendar.getInstance(TimeZone zone)
Calendar.getInstance(Locale aLocale)
Calendar.getInstance(TimeZone zone, Locale aLocale)
Decrementing the date in Java
To decrement the date in Java follow the given steps
Import the following package for Calendar class in Java
import java.util.Calendar ;
Firstly, create a Calendar object and display the current date
Calendar calendar = Calendar.getInstance() ;
System.out.println("Current Date = " + calendar.getTime()) ;
Now, let us decrement the date using the add() method and Calendar.DATE constant. Set a negative value since we are decrementing the date
calendar.add(Calendar.DATE, -3) ;
Example
Filename: Example.java
import java.util.Calendar;
public class Example {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance() ;
System.out.println("Current Date = " + calendar.getTime()) ;
// Decrementing date by 3
calendar.add(Calendar.DATE, -3) ;
System.out.println("Updated Date = " + calendar.getTime()) ;
}
}
Output
Incrementing the date in Java
import java.util.Calendar ;
First, create a Calendar object and display the current date
Calendar calendar = Calendar.getInstance() ;
System.out.println("Current Date = " + calendar.getTime()) ;
Now, let us increment the date using the add() method and Calendar.DATE constant
calendar.add(Calendar.DATE, 2) ;
Example
Filename: Example.java
import java.util.Calendar ;
public class Example {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance() ;
System.out.println("Current Date = " + calendar.getTime()) ;
// Incrementing date by 2
calendar.add(Calendar.DATE, 2) ;
System.out.println("Updated Date = " + calendar.getTime()) ;
}
}
Output
Filename: Example.java
import java.util.Calendar;
public class Example {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance() ;
System.out.println("Current Date = " + calendar.getTime()) ;
// Decrementing date by 5
calendar.add(Calendar.DATE, -5) ;
System.out.println("Date before 5 days= " + calendar.getTime()) ;
// Incrementing date by 5
calendar.add(Calendar.DATE, 10) ;
System.out.println("Date after 5 days = " + calendar.getTime()) ;
}
}
Output
In this quick tutorial, you'll learn how to increment the days to the current date and time in Java.
Java Calendar api has a special add() method that increment the given number for the given unit.
Look at the below example that adds the 2 years 2 months 2 days to the current date.
Filename: AddDaysExample.java
import java.text.SimpleDateFormat ;
import java.util.Calendar ;
import java.util.Date ;
public class AddDaysExample {
public static void main(String[] args) {
// creating date in java
Date date = new Date() ;
// printing the date in readable format.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd") ;
String dateStr = dateFormat.format(date) ;
System.out.println("Current date : "+dateStr) ;
//creating calender instance
Calendar calendar = Calendar.getInstance();
calendar.setTime(date) ;
// adding 2 years, months and date
calendar.add(Calendar.YEAR, 2) ;
calendar.add(Calendar.MONTH, 2) ;
calendar.add(Calendar.DATE, 2) ;
// getting the new date from the calendar
Date addedDate = calendar.getTime() ;
// printing the new date
String newDateinStr = dateFormat.format(addedDate) ;
System.out.println("New date after adding 2 years : "+newDateinStr) ;
}
}
Output
Look at the below example that goes back to the 2 years 2 months 2 days to the current date.
Filename: Example.java
import java.text.SimpleDateFormat ;
import java.time.LocalDateTime ;
import java.time.ZoneId ;
import java.time.format.DateTimeFormatter ;
import java.util.Date ;
public class Example {
public static void main(String[] args) {
// date formats
String format = "yyyy-MM-dd hh:mm:ss" ;
SimpleDateFormat dateFormat = new SimpleDateFormat(format) ;
// Date format in java 8
DateTimeFormatter dateFormat8 = DateTimeFormatter.ofPattern(format) ;
// creating date in java
Date date = new Date() ;
// printing the date in readable format.
String dateStr = dateFormat.format(date) ;
System.out.println("Current date : " + dateStr) ;
// converting date to LocalDateTime
LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime() ;
System.out.println("localDateTime : " + dateFormat8.format(localDateTime)) ;
localDateTime = localDateTime.minusYears(2) ;
localDateTime = localDateTime.minusMonths(2) ;
localDateTime = localDateTime.minusDays(2) ;
localDateTime = localDateTime.minusHours(2).minusMinutes(2).minusSeconds(2) ;
// converting LocalDateTime to Date
Date currentDateminus = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()) ;
// printing the new date
String newDateinStr = dateFormat.format(currentDateminus);
System.out.println("New date after going back 2 years : " + newDateinStr);
}
}
Output