Java Day Add addDays(String dateStr, int nDays, String inputDateFormat, String outputDateFormat)

Here you can find the source of addDays(String dateStr, int nDays, String inputDateFormat, String outputDateFormat)

Description

add days to a given date

License

Open Source License

Parameter

Parameter Description
dateStr (in inputDateFormt)
nDays a parameter

Return

modified date (in outputDatFormat) e.g., addDays("2008-02-28", 1, "yyyy-MM-dd", "MM/yy/dd") returns "02/08/29" addDays("2008-02-28", -29, "yyyy-MM-dd", "MM/yy/dd") returns "01/08/31" addDays("2008-02-28", 0, "yyyy-MM-dd", "MM/yy/dd") returns "02/08/28" (simply converts the date format)

Declaration

public static String addDays(String dateStr, int nDays, String inputDateFormat, String outputDateFormat) 

Method Source Code

//package com.java2s;
/**/* w w w .ja  v  a  2 s .c om*/
 * Copyright (C) 2010-2014 Think Big Analytics, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. See accompanying LICENSE file.
 */

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

public class Main {
    /**
      * add days to a given date
      * @param dateStr (in inputDateFormt)
      * @param nDays
      * @return modified date (in outputDatFormat)
      * e.g., addDays("2008-02-28", 1, "yyyy-MM-dd", "MM/yy/dd") returns "02/08/29"
      *        addDays("2008-02-28", -29, "yyyy-MM-dd", "MM/yy/dd") returns "01/08/31"
      *        addDays("2008-02-28", 0, "yyyy-MM-dd", "MM/yy/dd") returns "02/08/28" (simply converts the date format)
      */
    public static String addDays(String dateStr, int nDays, String inputDateFormat, String outputDateFormat) {

        try {

            DateFormat inputFormatter = new SimpleDateFormat(inputDateFormat);
            DateFormat outputFormatter = inputDateFormat.equals(outputDateFormat) ? inputFormatter
                    : new SimpleDateFormat(outputDateFormat);

            Date date = (Date) inputFormatter.parse(dateStr);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE, nDays);

            return outputFormatter.format(calendar.getTime());

        } catch (ParseException e) {

            return null;
        }
    }

    /**
     * add days to a given date
     * InputDateFormat = OutputDateFormat
     * @param dateStr
     * @param nDays
     * @param dateFormat
     * @return modifed date
     * e.g., addDays("2008-02-28", 1, "yyyy-MM-dd") returns 2008-02-29
     */
    public static String addDays(String dateStr, int nDays, String dateFormat) {
        return addDays(dateStr, nDays, dateFormat, dateFormat);
    }

    /**
     * add days to a given date
     * InputDateFormat = OutputDateFormat = yyyy-MM-dd
     * @param dateStr
     * @param nDays
     * @return modified date
     * e.g., addDays("2008-02-28", 1, "yyyy-MM-dd") returns 2008-02-29
     */
    public static String addDays(String dateStr, int nDays) {
        return addDays(dateStr, nDays, "yyyy-MM-dd");
    }
}

Related

  1. addDays(int days, Date date)
  2. addDays(Integer dateAsInt, Integer daysToAdd)
  3. addDays(java.util.Date value, final int days)
  4. AddDays(long initialDateMilliSeconds, int dayNumber)
  5. addDays(String dateStr, int days)
  6. addDays(String s, int day)
  7. addDays(String src, int day, String format)
  8. addDays(String startDate, int amount)
  9. addDays2Date(Date d, int days)