Java Day Add add(int date, int days)

Here you can find the source of add(int date, int days)

Description

add

License

Apache License

Declaration

public static String add(int date, int days) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {

    public static Date add(int days) {
        Calendar now = Calendar.getInstance();
        now.add(Calendar.DAY_OF_YEAR, days);
        return now.getTime();
    }/*from w w w.j av a  2  s .  co m*/

    public static String add(int days, String pattern) {
        Date date = add(days);
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    public static String add(int date, int days) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            Calendar cd = Calendar.getInstance();
            cd.setTime(format.parse(String.valueOf(date)));
            cd.add(Calendar.DAY_OF_YEAR, days);
            return format.format(cd.getTime());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static String add(String date, int days, String pattern) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cd = Calendar.getInstance();
            cd.setTime(format.parse(date));
            cd.add(Calendar.DAY_OF_YEAR, days);
            return new SimpleDateFormat(pattern).format(cd.getTime());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. add(String date, int day)
  2. addDate(Date d, long day)
  3. addDate(Date date, int day)
  4. addDate(java.util.Date date, int day)