#JavaInspires
Hi Guys,Welcome to Java Inspires.
Output:
Docs: https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
In this post, we will see java program to add days to Date object in Java using Calendar Object.
Here, we create a date object then we convert date to calendar object. Then we add days using add method with Calendar.DATE parameter.
calendar.add(Calendar.DATE, n); // n is number of days.
Here is the code.
package com.javainspires; import java.util.Calendar; import java.util.Date; /** * * @author #JavaInspires * */ public class JavaBasicMain { public static void main(String[] args) { // create a date object Date today = new Date(); System.out.println("Today :" + today); // create calendar instance Calendar calendar = Calendar.getInstance(); // Convert date object to calender object calendar.setTime(today); // to add one day to today calendar.add(Calendar.DATE, 1); // convert calendar to date Date tomorrow = calendar.getTime(); System.out.println("Tomorrow :" + tomorrow); // to add two days calendar.setTime(today); // resetting to today calendar.add(Calendar.DATE, 2); Date dayAfterTomorrow = calendar.getTime(); System.out.println("Day After Tomorrow :" + dayAfterTomorrow); // to add 10 days calendar.setTime(today); // resetting to today calendar.add(Calendar.DATE, 10); System.out.println("After 10 Days :" + calendar.getTime()); } }
Output:
Today :Fri May 14 12:08:23 EDT 2021 Tomorrow :Sat May 15 12:08:23 EDT 2021 Day After Tomorrow :Sun May 16 12:08:23 EDT 2021 After 10 Days :Mon May 24 12:08:23 EDT 2021
Docs: https://docs.oracle.com/javase/8/docs/api/java/util/Date.html
Search Terms:
add days to date java, add days to date java 8, add days to date java calendar, add days to java date object, add 30 days to date java, add 10 days to date java, get tomorrow date in java, get next day date java, convert date to calendar in java, convert calendar to date in java