Java TimeUnit Calculate getDate(final TimeUnit unit, final int offset)

Here you can find the source of getDate(final TimeUnit unit, final int offset)

Description

get Date

License

Open Source License

Return

The date that results after appling the offset in the specified unit to the current date

Declaration

public static Date getDate(final TimeUnit unit, final int offset) 

Method Source Code

//package com.java2s;
/*/*from  www  . j  a  v  a 2 s . c o m*/
 * Copyright (c) 2014 CCM modding crew.
 * View members of the CCM modding crew on https://github.com/orgs/CCM-Modding/members
 *
 * 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, TORT 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 {
    /**
     * @return The date that results after appling the offset in the specified unit to the current date
     */
    public static Date getDate(final TimeUnit unit, final int offset) {
        // Get the default representation of "now".
        final Calendar when = Calendar.getInstance();
        // Scale the offset according to its unit.
        // Note that this defines a day as exactly 60*60*24 seconds,
        // and ignores things like Daylight Savings Time.
        long seconds = unit.toSeconds(offset);
        // We do not support time increments longer than 24 millenia.
        if (seconds > Integer.MAX_VALUE) {
            seconds = Integer.MAX_VALUE;
        } else if (seconds < Integer.MIN_VALUE) {
            seconds = Integer.MIN_VALUE;
        }
        when.add(Calendar.SECOND, (int) seconds);
        // Convert to a Date object.
        return when.getTime();
    }
}

Related

  1. diff(Date earlier, Date later, TimeUnit timeUnit)
  2. differenceBetween(Date initDate, Date endDate, TimeUnit units)
  3. getBase(final TimeUnit unit)
  4. getBucketInMillis(int bucketSize, TimeUnit bucketUnit)
  5. getComingTime(Integer delta, TimeUnit unit)
  6. getDateDiff(Date date1, Date date2, TimeUnit timeUnit)
  7. getDateDiff(final Date d1, final Date d2, final TimeUnit timeUnit)
  8. getDifference(Calendar initDate, Calendar endDate, TimeUnit units)
  9. getExpiringMap(long time, TimeUnit unit)