Java Date to Time getTimeDeltaValue(Date t, Date baseDate, String tDelta)

Here you can find the source of getTimeDeltaValue(Date t, Date baseDate, String tDelta)

Description

Get time value - Time delta value of base date

License

Open Source License

Parameter

Parameter Description
t The time
baseDate Base date
tDelta Time delta type - days/hours/...

Return

The time delta value

Declaration

public static int getTimeDeltaValue(Date t, Date baseDate, String tDelta) 

Method Source Code

//package com.java2s;
/* Copyright 2012 Yaqiang Wang,
* yaqiang.wang@gmail.com/*w ww .ja  v a  2 s .  co m*/
* 
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or (at
* your option) any later version.
* 
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
* General Public License for more details.
*/

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

public class Main {
    /**
     * Get time value - Time delta value of base date
     *
     * @param t The time
     * @param baseDate Base date
     * @param tDelta Time delta type - days/hours/...
     * @return The time delta value
     */
    public static int getTimeDeltaValue(Date t, Date baseDate, String tDelta) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(baseDate);
        long sl = cal.getTimeInMillis();
        long el, delta;
        int value = 0;
        cal.setTime(t);
        el = cal.getTimeInMillis();
        delta = el - sl;
        if (tDelta.equalsIgnoreCase("hours")) {
            value = (int) (delta / (60 * 60 * 1000));
        } else if (tDelta.equalsIgnoreCase("days")) {
            value = (int) (delta / (24 * 60 * 60 * 1000));
        }

        return value;
    }
}

Related

  1. getTimeBoxValue(TimeZone zone, Date date)
  2. getTimeByCustomPattern(Date date, String pattern)
  3. getTimeByOffset(Date reqDate, int offset)
  4. getTimeDateByMinuteAndSeconds( Integer minutes, Integer seconds)
  5. getTimeDateStr(Date date)
  6. getTimeFromDate(Date date)
  7. getTimeFromDate(final Date date)
  8. getTimeImMillis(Date date)
  9. getTimeInSeconds(Date date)