Java Date Add addDate(String dt, long day)

Here you can find the source of addDate(String dt, long day)

Description

add Date

License

Open Source License

Declaration

public static java.util.Date addDate(String dt, long day) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;

public class Main {
    private final static int MILLIS_PER_HOUR = 3600000;

    public static java.util.Date addDate(String dt, long day) {
        if (dt == null) {
            throw new IllegalArgumentException("dt can not be null");
        }/*w w w  .j  a  va 2 s .  co  m*/

        int len = dt.length();
        if (!(len == 8 || len == 14)) {
            throw new IllegalArgumentException("dt length must be 8 or 14 (yyyyMMdd or yyyyMMddHHmmss)");
        }

        if (len == 8) {
            dt += "090000";
        }

        return addDate(getDate(dt), day);
    }

    public static java.util.Date addDate(java.util.Date dt, long day) {
        return new java.util.Date(dt.getTime() + (MILLIS_PER_HOUR * 24L * day));
    }

    /**
    * yyyyMMddHHmmss --> java.util.Date
    * @param String dt
    * @return java.util.Date
    */
    private static java.util.Date getDate(String dt) {
        Calendar cal = Calendar.getInstance();
        cal.set(Integer.valueOf(dt.substring(0, 4)).intValue(), Integer.valueOf(dt.substring(4, 6)).intValue() - 1,
                Integer.valueOf(dt.substring(6, 8)).intValue(), Integer.valueOf(dt.substring(8, 10)).intValue(),
                Integer.valueOf(dt.substring(10, 12)).intValue(), Integer.valueOf(dt.substring(12, 14)).intValue());

        return cal.getTime();
    }
}

Related

  1. addDate(java.util.Date date, int day)
  2. addDate(String date)
  3. addDate(String date, int amount)
  4. addDate(String date, String type, int into)
  5. addDate(String date1, String addpart, int num)
  6. addDateByDays(int days)
  7. addDateDay(int day)
  8. addDateParam(String dtstr, boolean start, List where)
  9. addDates(Date startDate, int days)