Java Date to Time getTimeBackInMillis(final int granularity, final int numberToBack, final Date date)

Here you can find the source of getTimeBackInMillis(final int granularity, final int numberToBack, final Date date)

Description

get Time Back In Millis

License

Apache License

Declaration

private static long getTimeBackInMillis(final int granularity, final int numberToBack, final Date date) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    private static final ThreadLocal<Calendar> calendarCache = new ThreadLocal<Calendar>();

    private static long getTimeBackInMillis(final int granularity, final int numberToBack, final Date date) {

        Calendar calendar = buildCalendar(date);
        calendar.add(granularity, -numberToBack);
        long timeBackInMillis = calendar.getTimeInMillis();
        return timeBackInMillis;
    }/*from   w ww.  j av  a  2 s .c om*/

    /**
     * Gets a calendar using the default time zone and locale. The Calendar
     * returned is based on the current time in the default time zone with the
     * default locale.
     * 
     * @return a Calendar object.
     */
    private static Calendar buildCalendar() {

        Calendar calendar = calendarCache.get();
        if (calendar == null) {
            calendar = GregorianCalendar.getInstance();
            calendarCache.set(calendar);
        }
        return calendar;
    }

    /**
     * Gets a calendar using the default time zone and locale. The Calendar
     * returned is based on the given time in the default time zone with the
     * default locale.
     * 
     * @return a Calendar object use given date.
     */
    private static Calendar buildCalendar(final Date date) {

        Calendar calendar = buildCalendar();
        calendar.setTime(date);
        return calendar;
    }
}

Related

  1. getTime(java.util.Date date)
  2. getTime(java.util.Date date)
  3. getTimeAsMilliSeconds(Date dateTime)
  4. getTimeAsString(Date date)
  5. getTimeAsXsdDateTime(Date d)
  6. getTimeBoxValue(TimeZone zone, Date date)
  7. getTimeByCustomPattern(Date date, String pattern)
  8. getTimeByOffset(Date reqDate, int offset)
  9. getTimeDateByMinuteAndSeconds( Integer minutes, Integer seconds)