Java Date Add addDate(String date, String type, int into)

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

Description

add Date

License

Apache License

Declaration

public static String addDate(String date, String type, int into) throws Exception 

Method Source Code

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

import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    public static String addDate(String date, String type, int into) throws Exception {
        String Sdate = "";
        try {/*from   w w  w. ja v  a2  s  . com*/
            GregorianCalendar grc = new GregorianCalendar();
            grc.setTime(new Date(date));
            if (type.equals("D")) {
                grc.add(GregorianCalendar.DATE, into);
            } else if (type.equals("M")) {
                grc.add(GregorianCalendar.MONTH, into);
            } else if (type.equals("Y")) {
                grc.add(GregorianCalendar.YEAR, into);
            }
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Sdate = new String(formatter.format(grc.getTime()));
        } catch (Exception e) {
            throw e;
        }
        return Sdate;
    }

    public static String addDate(String date, String into) throws Exception {
        String Sdate = "";
        try {
            date = date.replaceAll("-", "/");
            date = date.substring(0, date.length() - 2);
            GregorianCalendar grc = new GregorianCalendar();
            grc.setTime(new Date(date));
            grc.add(GregorianCalendar.DATE, Integer.parseInt(into));
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Sdate = new String(formatter.format(grc.getTime()));
        } catch (Exception e) {
            throw e;
        }
        return Sdate;
    }

    /**
     * Returns a string the represents the passed-in date parsed according to
     * the passed-in format. Returns an empty string if the date or the format
     * is null.
     **/
    public static String format(Date aDate, SimpleDateFormat aFormat) {
        if (aDate == null || aFormat == null) {
            return "";
        }
        synchronized (aFormat) {
            return aFormat.format(aDate);
        }
    }
}

Related

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