Java Day Add addDays(Date d, double count)

Here you can find the source of addDays(Date d, double count)

Description

add count days days is truncated to second and can be negative

License

Open Source License

Declaration

public static Date addDays(Date d, double count) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  w w w. j  a va 2 s. c  om*/
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**
     * add count days days is truncated to second and can be negative
     */
    public static Date addDays(Date d, double count) {
        if (d == null) {
            return null;
        }
        int sec = (int) (count * 3600 * 24);
        int sign = 1;
        if (sec < 0) {
            sec = -sec;
            sign = -1;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(Calendar.DATE, sign * (sec / 3600 / 24));
        cal.add(Calendar.HOUR_OF_DAY, sign * ((sec / 3600) % 24));
        cal.add(Calendar.MINUTE, sign * ((sec / 60) % 60));
        cal.add(Calendar.SECOND, sign * (sec % 60));
        return cal.getTime();
    }
}

Related

  1. addDay(String datetime, int day)
  2. addDayInterval(long millis, int dayInterval)
  3. addDays(Date aDate, int days)
  4. addDays(Date baseDate, int amount, boolean endOfDay)
  5. addDays(Date beginDate, int days)
  6. addDays(Date d, int days)
  7. addDays(Date d, int n)
  8. addDays(Date date, int amount)
  9. addDays(Date date, int count)