Adding and subtracting days, month, year, hour, second, minutes from Date (using GregorianCalendar) | Adding days to manually created Date in java


Contents of page :
  • Program 1.1) Adding days in Date (using GregorianCalendar)
  • Program 1.2) Adding hours in Date (using GregorianCalendar)
  • Program 1.3) Adding days and hours in Date (using GregorianCalendar)

  • Program 2.1) Subtracting days from Date (using GregorianCalendar)
  • Program 2.2) Subtracting days and hours from Date (using GregorianCalendar)

  • Program 3) Adding days and hours to manually created Date (using GregorianCalendar)

Program 1.1) Adding days in Date (using GregorianCalendar)
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... args) {
          GregorianCalendar currentDate = new GregorianCalendar();
          Date date = currentDate.getTime();
          System.out.println("currentDate                = " + date);
         
          currentDate.add(Calendar.DATE, 2); //Add 2 days to currentDate
         
          date = currentDate.getTime();
          System.out.println("currentDate after adding 2 day = " + date);
   }
}
/*OUTPUT
currentDate                = Wed Jul 08 17:31:15 IST 2015
currentDate after adding 2 day = Fri Jul 10 17:31:15 IST 2015
*/



Program 1.2) Adding hours in Date (using GregorianCalendar)
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... args) {
          GregorianCalendar currentDate = new GregorianCalendar();
          Date date = currentDate.getTime();
          System.out.println("currentDate                  = " + date);
         
          currentDate.add(Calendar.HOUR, 2); //Add 2 hours to currentDate
         
          date = currentDate.getTime();
          System.out.println("currentDate after adding 2 hours = " + date);
   }
}
/*OUTPUT
currentDate                  = Wed Jul 08 17:31:35 IST 2015
currentDate after adding 2 hours = Wed Jul 08 19:31:35 IST 2015
*/


Program 1.3) Adding days and hours in Date (using GregorianCalendar)
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... args) {
          GregorianCalendar currentDate = new GregorianCalendar();
          Date date = currentDate.getTime();
          System.out.println("currentDate                              = " + date);
          currentDate.add(Calendar.DATE, 2); //Add 2 days to currentDate
          currentDate.add(Calendar.HOUR, 2); //Add 2 hours to currentDate
         
          date = currentDate.getTime();
          System.out.println("currentDate after adding 2 days and 2 hours = " + date);
   }
}
/*OUTPUT
currentDate                              = Wed Jul 08 17:31:45 IST 2015
currentDate after adding 2 days and 2 hours = Fri Jul 10 19:31:45 IST 2015
*/




Program 2.1) Subtracting days from Date (using GregorianCalendar)
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... args) {
          GregorianCalendar currentDate = new GregorianCalendar();
          Date date = currentDate.getTime();
          System.out.println("currentDate                   = " + date);
         
          //Add -2 hours to currentDate OR subtract 2 days from currentDate
          currentDate.add(Calendar.HOUR, -2);
         
          date = currentDate.getTime();
          System.out.println("currentDate after adding -2 hours = " + date);
   }
}
/*OUTPUT
currentDate                    = Wed Jul 08 17:31:59 IST 2015
currentDate after adding -2 hours = Wed Jul 08 15:31:59 IST 2015
*/



Program 2.2) Subtracting days and hours from Date (using GregorianCalendar)
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... args) {
          GregorianCalendar currentDate = new GregorianCalendar();
          Date date = currentDate.getTime();
          System.out.println("currentDate                                = " + date);
          //Add -2 days to currentDate OR subtract 2 hours from currentDate
          currentDate.add(Calendar.DATE, -2);
         
          //Add -2 hours to currentDate OR subtract 2 days from currentDate
          currentDate.add(Calendar.HOUR, -2);
         
          date = currentDate.getTime();
          System.out.println("currentDate after adding -2 days and -2 hours = " + date);
   }
}
/*OUTPUT
currentDate                                = Wed Jul 08 17:32:12 IST 2015
currentDate after adding -2 days and -2 hours = Mon Jul 06 15:32:12 IST 2015
*/






Program 3) Adding days and hours to manually created Date (using GregorianCalendar)

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... args) {
          try {
                 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                 Date manualDate = sdf.parse("21-04-2015");
                 System.out.println("manualDate                            = " + manualDate);
                 GregorianCalendar cal = new GregorianCalendar();
                
                 cal.setTime(manualDate); // set date to custom date,then add days and hours to it...
                                           
                 cal.add(Calendar.DATE, 2); //Add 2 days to manualDate
                 cal.add(Calendar.HOUR, 2); //Add 2 hours to manualDate
                
                 manualDate = cal.getTime();
                 System.out.println("manualDate after adding 2 hours & 2 days = " + manualDate);
          } catch (ParseException pe) {
                 pe.printStackTrace();
          }
   }
}
/*OUTPUT
manualDate                            = Tue Apr 21 00:00:00 IST 2015
manualDate after adding 2 hours & 2 days = Thu Apr 23 02:00:00 IST 2015
*/


RELATED LINKS>

Convert String to Date>

Using SimpleDateFormat to convert String to Date - pass date, month, year, hour, minute, second, day, Am/Pm(E), time zone(z) || Enabling strict parsing of String by setting SimpleDateFormat’s setLenient(false)



Convert Date to String>

Using SimpleDateFormat to convert Date to String - use dd-MM-yyyy and dd-MM-yyyy hh:mm:ss format



Difference between two Dates>

Difference between two dates in days, hours, minutes, seconds, milliSeconds- where dates are manually created using dd-MM-yyyy and dd-MM-yyyy hh:mm:ss format


2 approaches to check whether difference between given two dates is greater than specified number of days or not


No comments:

Post a Comment