Here you can find the source of getTimeDeltaValue(Date t, Date baseDate, String tDelta)
Parameter | Description |
---|---|
t | The time |
baseDate | Base date |
tDelta | Time delta type - days/hours/... |
public static int getTimeDeltaValue(Date t, Date baseDate, String tDelta)
//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; } }