Java Program to add days to date


This Java example will show you how to add days to date. Add days, hours, minutes, seconds

import java.util.*;

public class Main {
   public static void main(String[] args) throws Exception {
      Date date = new Date();
      Calendar cal = Calendar. getInstance();
      cal.setTime(date);
      System.out.println("today is " + date.toString());
      cal. add(Calendar.MONTH, 2);
      System.out.println("date after 2 month will be " + cal.getTime().toString() );
      cal. add(Calendar.HOUR, 48);
      System.out.println("date after 48 hrs will be " + cal.getTime().toString() );
      cal. add(Calendar.YEAR, 4);
      System.out.println("date after 4 years will be " + cal.getTime().toString() );
   }
}

 

 

Output:

today is Wed May 05 13:39:37 UTC 2021
date after a month will be Mon Jul 05 13:39:37 UTC 2021
date after 48 hrs will be Wed Jul 07 13:39:37 UTC 2021
date after 3 years will be Mon Jul 07 13:39:37 UTC 2025 

 


JAVA File IO examples

JAVA Date & Time Progamming Examples