Java Day Add getAddDay(int day)

Here you can find the source of getAddDay(int day)

Description

get Add Day

License

Open Source License

Declaration

public static String getAddDay(int day) 

Method Source Code


//package com.java2s;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {

    public static String getAddDay(int day) {
        String orgDate = null;/*  www.j  av a2  s.c  om*/
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, day);
        Date newDate = calendar.getTime();
        orgDate = formatDate(newDate);
        return orgDate;
    }

    /**
     * Adds to a date returning a new object. The original date object is
     * unchanged.
     * 
     * @param date
     *            the date, not null
     * @param calendarField
     *            the calendar field to add to
     * @param amount
     *            the amount to add, may be negative
     * @return the new date object with the amount added
     * @throws IllegalArgumentException
     *             if the date is null
     */
    public static Date add(Date date, int calendarField, int amount) {
        if (date == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(calendarField, amount);
        return c.getTime();
    }

    public static String formatDate(Date date) {
        if (date == null) {
            return null;
        }
        return new SimpleDateFormat("yyyy-MM-dd").format(date);
    }
}

Related

  1. addDayString5(Date date, int day)
  2. addDayToString(String strDate, int days)
  3. addOneDay(String strDate)
  4. addOneDayForamt(String date, String format, Integer addDate)
  5. AddUtilDate(String strDate, int iDays)
  6. getFutrueDate(Date oldDate, int addDay)