Java Date Add addDate(String date, int amount)

Here you can find the source of addDate(String date, int amount)

Description

add Date

License

Apache License

Declaration

public static Date addDate(String date, int amount) 

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 addDate(String date, int amount) {
        if (date == null || "".equals(date.trim())) {
            return null;
        }//from  w  w  w .  j av  a  2  s .c  o m
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getDate(date));
        calendar.add(Calendar.DATE, amount);
        return calendar.getTime();
    }

    public static Date getDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date getDate(Date date, String dateFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        try {
            return sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date getDate(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date getDate(String date, String dateFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date add(Date d, int field, int amount) {
        if (d == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.add(field, amount);
        return calendar.getTime();
    }

    public static Date getTime(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.parse(date);
    }
}

Related

  1. addDate(Date date, int years, int months, int days)
  2. addDate(Date date, String unit, int quantity)
  3. addDate(int field, int amount)
  4. addDate(java.util.Date date, int day)
  5. addDate(String date)
  6. addDate(String date, String type, int into)
  7. addDate(String date1, String addpart, int num)
  8. addDate(String dt, long day)
  9. addDateByDays(int days)