Thursday, 9 August 2012

Java: Adding days to a date

package basics;

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

public class DateIncrementer
{

    public static Date addDays(Date date, int days)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
        return cal.getTime();
    }

    public static void main(String[] args)
    {
        Date currentDate = new Date();
        int days = 2;
        Date date = addDays(currentDate, days);

        System.out.println("Current Date = " + currentDate + "\nNew date = " + date + "\nDays added = " + days);
    }
}

No comments:

Post a Comment