How to Change the Day in the Date using Java?
To operate with the date and time in Java, we need the Calendar abstract class. It provides a number of helpful interfaces that enable us to convert dates between a certain point in time and a collection of calendar fields like DAY, MONTH, YEAR, HOUR, etc. Calendar, a class that also descends from an Object, implements the Comparable interface.
The Calendar class's declaration
public abstract class Calendar extends Object
implements Serializable, Cloneable, Comparable <Calendar>
Using Java's Calendar class, we may modify a specified date or the current date by adding either a single day or a number of days. Let's examine how we might extend the date by days.
1. Using the Calendar class, add Days to the specified Date
The following procedures are used to extend the specified date by days using the Calendar class:
- Including SimpleDateFormat and Calendar, we import all required classes and packages.
- Instances of SimpleDateFormat and Calendar should be created. These instances allow us to parse the date to which we need to add days.
- Use the setTime() function of the calendar class to add the specified date to the calendar.
- Use the add() function of the calendar class to extend the date by a day. The add method's two parameters are the calendar element and the amount of time that must be added ().
- Take the newly calculated date from the calendar as well as change the SimpleDateFormat class's default format to display it on the screen.
Example program
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.text.ParseException;
class Date {
public static void main(String args[]){
String dateBefore = "2022-08-11";
System.out.println(dateBefore+" is the date before adding the days");
SimpleDateFormatsdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
try{
cal.setTime(sdf.parse(dateBefore));
}catch(ParseException e){
e.printStackTrace();
}
cal.add(Calendar.DAY_OF_MONTH, 3);
String dateAfter = sdf.format(cal.getTime());
System.out.println(dateAfter+" is the date after adding the 3 days.");
}
}
Output
2022-08-11 is the date before adding 4 the days
2022-08-14 is the date after adding the 4 days.
2. Using the Calendar class, add days to the current date.
The procedures for adding days to a current date and a given date are identical. In this instance, defining a date is not necessary. Here, we take a date from the calendar and then add days to it.
Example program
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.text.ParseException;
class Date {
public static void main(String args[]) {
SimpleDateFormatsdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
System.out.println(sdf.format(cal.getTime())+" is the date before the adding days");
cal.add(Calendar.DAY_OF_MONTH, -4);
String dateAfter = sdf.format(cal.getTime());
System.out.println(dateAfter+" is the date after adding the 3 days.");
}
}
The output of the above program
2022-11-25 is the date before the adding days
2022-11-21 is the date after adding the 3 days.
3. Without utilizing the Calendar class, date addition
Including days in a date without using the Calendar class is a rather simple process.
We make use of the Java.time package's LocalDate class. The LocalDate class offers a number of ways to determine and alter the current date.
Example program
class Date
{
public static void main(String args[])
{
LocalDate newDate1 = LocalDate.now().plusDays(2);
System.out.println(newDate1+" is a day after adding a 4 day to the current date");
LocalDate newDate2 = LocalDate.now().plusDays(3);
System.out.println(newDate2+" is a day after adding a 8 day to the current date");
LocalDate newDate3 = LocalDate.of(2020, 11, 15).plusDays(1);
System.out.println(newDate3+" is a day after adding 1 day to the defined date");
LocalDate newDate4 = LocalDate.of(2020, 11, 15).plusDays(7);
System.out.println(newDate4+" is a day after adding 7 a day to the defined date");
}
}
Output
2022-11-27 is a day after adding a 4-day to the current date
2022-11-28 is a day after adding an 8 day to the current date
2020-11-16 is a day after adding 1 day to the defined date
2020-11-22 is a day after adding 7 a day to the defined date
Example 2:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Date {
private static final DateFormatdateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
public static void main(String[] args) {
Date currentDate = new Date();
System.out.println(dateFormat.format(currentDate));
Calendar c = Calendar.getInstance();
c.setTime(currentDate);
c.add(Calendar.YEAR, 2);
c.add(Calendar.MONTH, 3);
c.add(Calendar.DATE, 2);
c.add(Calendar.HOUR, 3);
c.add(Calendar.MINUTE, 2);
c.add(Calendar.SECOND, 2);
Date currentDatePlusOne = c.getTime();
System.out.println(dateFormat.format(currentDatePlusOne));
}
}
Output
2022/11/25 09:31:012023/12/26 10:32:02