Java Day Add addDays(Integer dateAsInt, Integer daysToAdd)

Here you can find the source of addDays(Integer dateAsInt, Integer daysToAdd)

Description

addDays: returns the integer as date {talendTypes} String {Category} TimestampUtil {param} Integer(dateAsInt) dateAsInt : {param} Integer(daysToAdd) daysToAdd : {example} addDays(dateAsInt, daysToAdd) # 20150403

License

Apache License

Declaration

public static Integer addDays(Integer dateAsInt, Integer daysToAdd) 

Method Source Code

//package com.java2s;
/**//from  w  w  w.  ja v a2 s.  com
 * Copyright 2015 Jan Lolling jan.lolling@gmail.com
 * 
 * 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.
 */

import java.util.Calendar;

public class Main {
    /**
     * addDays: returns date of yesterday
     * @param today any day
     * @param daysToAdd difference in days
     * @return Date with UTC time
     * 
     * {talendTypes} Date
     * 
     * {Category} TimestampUtil
     * 
     * {param} date today: any day.
     * 
     * {param} long day: days to be added. (-1 for yesterday)
     * 
     * {example} addDays(today, daysToAdd).
     */
    public static java.util.Date addDays(java.util.Date today,
            long daysToAdd) {
        if (today != null) {
            return new java.util.Date(today.getTime()
                    + (1000 * 60 * 60 * 24 * daysToAdd));
        } else {
            return null;
        }
    }

    /**
     * addDays: returns the integer as date
     * 
     * {talendTypes} String
     * 
     * {Category} TimestampUtil
     * 
     * {param} Integer(dateAsInt) dateAsInt : 
     * {param} Integer(daysToAdd) daysToAdd : 
     * 
     * {example} addDays(dateAsInt, daysToAdd) # 20150403
     */
    public static Integer addDays(Integer dateAsInt, Integer daysToAdd) {
        if (dateAsInt != null) {
            java.util.Calendar c = java.util.Calendar.getInstance();
            // cut time
            c.set(java.util.Calendar.MINUTE, 0);
            c.set(java.util.Calendar.SECOND, 0);
            c.set(java.util.Calendar.MILLISECOND, 0);
            c.set(java.util.Calendar.HOUR_OF_DAY, 0);
            int year = dateAsInt / 10000;
            int monthDay = (dateAsInt - (year * 10000));
            int month = monthDay / 100;
            int day = monthDay % 100;
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, month - 1);
            c.set(Calendar.DAY_OF_MONTH, day);
            c.add(Calendar.DAY_OF_YEAR, daysToAdd);
            return getDateAsInt(c.getTime());
        } else {
            return null;
        }
    }

    /**
     * getDateAsId: returns the current day as integer
     * 
     * {talendTypes} String
     * 
     * {Category} TimestampUtil
     * 
     * {param} Date(date) date : date to format
     * 
     * {example} getDateAsInt(date) # 20150403
     */
    public static Integer getDateAsInt(java.util.Date date) {
        if (date != null) {
            java.util.Calendar c = java.util.Calendar.getInstance();
            c.setTime(date);
            // cut time
            c.set(java.util.Calendar.MINUTE, 0);
            c.set(java.util.Calendar.SECOND, 0);
            c.set(java.util.Calendar.MILLISECOND, 0);
            c.set(java.util.Calendar.HOUR_OF_DAY, 0);
            int id = c.get(Calendar.YEAR) * 10000;
            id = id + (c.get(Calendar.MONTH) + 1) * 100;
            id = id + c.get(Calendar.DAY_OF_MONTH);
            return id;
        } else {
            return null;
        }
    }
}

Related

  1. addDays(final Date date, final int addDays)
  2. addDays(final Date date, final int days)
  3. addDays(final Date date, final int days)
  4. addDays(int days)
  5. addDays(int days, Date date)
  6. addDays(java.util.Date value, final int days)
  7. AddDays(long initialDateMilliSeconds, int dayNumber)
  8. addDays(String dateStr, int days)
  9. addDays(String dateStr, int nDays, String inputDateFormat, String outputDateFormat)