Java Date add days, subtract days, Calendar

Filed Under: Java

Sometimes while working on Java Date, we need to add or subtract some days from it. Here I am providing java example to add days to java date or subtract days to java date object.

Java Date Add Days and Subtract Days

java date add days, java date subtract days

Below is a simple program for Java Date add days and subtract days example.


package com.journaldev.util;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class JavaDateAddSubtract {

	public static void main(String[] args) {
		Date date = new Date();
		System.out.println("Today's date: "+date.toString());
		//test addDays method
		int i=10;
		while(i<=50){
			Date newDate = addDays(date, i);
			System.out.println("Java Date after adding "+i+" days: "+newDate.toString());
			i+=10;
		}
		System.out.println("\n\n");
		//test subtractDays method
		i=10;
		while(i<=50){
			Date newDate = subtractDays(date, i);
			System.out.println("Java Date after subtracting "+i+" days: "+newDate.toString());
			i+=10;
		}

	}
	/**
	 * add days to date in java
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date addDays(Date date, int days) {
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(Calendar.DATE, days);
				
		return cal.getTime();
	}
	
	/**
	 * subtract days to date in java
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date subtractDays(Date date, int days) {
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(Calendar.DATE, -days);
				
		return cal.getTime();
	}

}

The output of the above program is:


Today's date: Thu Nov 15 11:45:05 PST 2012
Java Date after adding 10 days: Sun Nov 25 11:45:05 PST 2012
Java Date after adding 20 days: Wed Dec 05 11:45:05 PST 2012
Java Date after adding 30 days: Sat Dec 15 11:45:05 PST 2012
Java Date after adding 40 days: Tue Dec 25 11:45:05 PST 2012
Java Date after adding 50 days: Fri Jan 04 11:45:05 PST 2013

Java Date after subtracting 10 days: Mon Nov 05 11:45:05 PST 2012
Java Date after subtracting 20 days: Fri Oct 26 11:45:05 PDT 2012
Java Date after subtracting 30 days: Tue Oct 16 11:45:05 PDT 2012
Java Date after subtracting 40 days: Sat Oct 06 11:45:05 PDT 2012
Java Date after subtracting 50 days: Wed Sep 26 11:45:05 PDT 2012

You can use these utility functions to add days or subtract days to a Java Date object.

Update: Java 8 has introduced Date and Time API that provides utility method to add and subtract days, weeks, months etc. from a given date. You should check it out at Java Date API Tutorial.

Comments

  1. Manish Kathuria says:

    Dear Sir,

    I am getting a date input from the user in DATE FIELD in HTML5, and an option selected by the user as Daily, Weekly or Monthly in another SELECT Field. I would like to have a javascript code to give output to another DATE/TEXT field to update my SQL Database in PHP mySQLI. for example if date input is 2014-11-25(Y-m-d) and option selected is Daily the output should be 2014-11-26 & if option selected is Weekly then the output should be 2014-12-02. Can you help?

    Thanks & Regards

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages