Java TimeUnit Calculate minus(Date date, int interval, TimeUnit unit)

Here you can find the source of minus(Date date, int interval, TimeUnit unit)

Description

minus

License

Open Source License

Declaration

public static Date minus(Date date, int interval, TimeUnit unit) 

Method Source Code


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

import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.joda.time.DateTime;

public class Main {
    public static Date minus(Date date, int interval, TimeUnit unit) {
        switch (unit) {
        case DAYS:
            return minusDays(date, interval);
        case HOURS:
            return minusHours(date, interval);
        case MINUTES:
            return minusMinutes(date, interval);
        case SECONDS:
            return minusSeconds(date, interval);
        case MILLISECONDS:
            return minusMillis(date, interval);
        default:/*from w w  w. j  ava  2s.  c  om*/
            break;
        }

        return date;
    }

    public static Date minusDays(Date date, int days) {
        return new DateTime(date).minusDays(days).toDate();
    }

    public static Date minusHours(Date date, int hours) {
        return new DateTime(date).minusHours(hours).toDate();
    }

    public static Date minusMinutes(Date date, int minutes) {
        return new DateTime(date).minusMinutes(minutes).toDate();
    }

    public static Date minusSeconds(Date date, int seconds) {
        return new DateTime(date).minusSeconds(seconds).toDate();
    }

    public static Date minusMillis(Date date, int millis) {
        return new DateTime(date).minusMillis(millis).toDate();
    }
}

Related

  1. getWindowFlooredBinaryTime(TimeUnit unit, long timestamp, int windowSizeInSeconds)
  2. humanReadableTime(long time, TimeUnit unit)
  3. isValidTimeUnit(String timeUnit)
  4. logTiming(String description, int n, long time, TimeUnit unit)
  5. mapByName(TimeUnit... units)
  6. parseTimeUnit(final String unit)
  7. parseTimeUnit(Object property, TimeUnit defaultValue)
  8. pause(long duration, TimeUnit unit)
  9. printTime(long time, TimeUnit unit)