Java TimeUnit Usage daysFromToday(long toDate)

Here you can find the source of daysFromToday(long toDate)

Description

number of days the supplied date is from today in the future.

License

Open Source License

Parameter

Parameter Description
toDate note that this is long - date in milliseconds (date.getTime())

Return

number of days as counted from today forward into toDate. If toDate is the past, returned value is -ve.

Declaration

public static int daysFromToday(long toDate) 

Method Source Code

//package com.java2s;
/*/*from   w w  w . j  a  v a 2 s  .c  om*/
 * Copyright (c) 2016 simplity.org
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

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

import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * number of days the supplied date is from today in the future. same as
     * date - today.
     *
     * @param toDate
     *            note that this is long - date in milliseconds (date.getTime())
     * @return number of days as counted from today forward into toDate. If
     *         toDate is the past, returned value is -ve.
     */
    public static int daysFromToday(long toDate) {
        return daysBetweenDates(toDate, getToday().getTime());
    }

    /**
     * number of days between two dates. time part is ignored from both dates.
     * If you want to consider time as well, then use elapsedDaysBetweenDates()
     *
     * @param toDate
     *            milli-second representation of to-date. By convention, this
     *            should be a UTC date for 0:00 AM
     * @param fromDate
     *            milli-second representation of from-date. By convention, this
     *            should be a UTC date for 0:00 AM
     * @return number of days as counted from date to date, and not based on 24
     *         hours of elapsed time time.
     */
    public static int daysBetweenDates(long toDate, long fromDate) {
        return (int) TimeUnit.DAYS.convert(toDate - fromDate, TimeUnit.MILLISECONDS);
    }

    /**
     * Get today's date as per local calendar.
     *
     * @return a date object that represents a UTC date that is equal to today
     *         in local calendar. for example, if local calendar says
     *         20-Aug-2016, but UTC would say 21-Aug-2016, this function returns
     *         date that would print in UTC as '2016-08-20T00:00:00.000Z'
     */
    public static Date getToday() {
        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        /*
         * set date to this millis
         */
        return cal.getTime();
    }
}

Related

  1. dateDiff(Date d1, Date d2)
  2. dateStart(final Calendar c)
  3. daysBetween(Calendar startDate, Calendar endDate)
  4. daysBetween(Date initDate, Date endDate)
  5. daysBetweenDates(long toDate, long fromDate)
  6. daysToMillis(int days)
  7. durationInSecs(long startNanos, long endNanos)
  8. durationIsValid(long seconds, int nanos)
  9. durationToString(long duration)