Java Date Add addDate(String date)

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

Description

add Date

License

Apache License

Declaration

public static Date addDate(String date) 

Method Source Code

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

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.GregorianCalendar;

public class Main {

    public static Date addDate(String date) {
        if (date == null) {
            return null;
        }//ww w .  ja va 2s. co m

        Date tempDate = parseDate("yyyy-MM-dd", date);
        String year = format(tempDate, "yyyy");
        String month = format(tempDate, "MM");
        String day = format(tempDate, "dd");
        GregorianCalendar calendar = new GregorianCalendar(Integer.parseInt(year), Integer.parseInt(month) - 1,
                Integer.parseInt(day));

        calendar.add(GregorianCalendar.DATE, 1);
        return calendar.getTime();
    }

    /**
     * Parse a string and return the date value in the specified format
     * 
     * @param strFormat
     * @param dateValue
     * @return
     * @throws ParseException
     * @throws Exception
     */
    public static Date parseDate(String strFormat, String dateValue) {
        if (dateValue == null)
            return null;

        if (strFormat == null) {
            strFormat = "yyyy-MM-dd HH:mm:ss";
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);
        Date newDate = null;

        try {
            newDate = dateFormat.parse(dateValue);
        } catch (ParseException pe) {
            newDate = null;
        }

        return newDate;
    }

    public static String format(Date aTs_Datetime) {
        return format(aTs_Datetime, "yyyy-MM-dd");
    }

    public static String format(Date aTs_Datetime, String as_Pattern) {
        if (aTs_Datetime == null || as_Pattern == null)
            return null;

        SimpleDateFormat dateFromat = new SimpleDateFormat();
        dateFromat.applyPattern(as_Pattern);

        return dateFromat.format(aTs_Datetime);
    }

    public static String getTime(Date date) {
        SimpleDateFormat formatter_f = new SimpleDateFormat("yyyy-MM-dd");
        String str = formatter_f.format(date);
        return str;
    }
}

Related

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