/* XBN Java: Generically useful, non-GUI Java code. http://sourceforge.net/projects/xbnjava Copyright (C) 1997-2003, Jeff Epstein All rights reserved. Modifications: No Redistribution in binary form, with or without modifications, are permitted provided that the following conditions are met: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * If modifications are made to source code then this license should indicate that fact in the "Modifications" section above. * Neither the author, nor the contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. [NOTE: This license contains NO advertising clause.] */ package xbn.util; import xbn.XBNObject; import java.util.Date; /**
Random functions for analyzing and manipulating java.util.Dates. See java.util.Date.
Source code: UtilDate.java.
Create a UtilDate. This constructor does nothing.
**/ public UtilDate() { } /**Get a Date that is X seconds before now.
@returngetDateXBeforeNow(getMillsInASecond(), i_unitsAgo)
**/
public final Date getDateXBNSeconds(int i_unitsAgo) {
return getDateXBeforeNow(getMillsInASecond(), i_unitsAgo);
}
/**
Get a Date that is X minutes before now.
@returngetDateXBeforeNow(getMillsInAMinute(), i_unitsAgo)
**/
public final Date getDateXBNMinutes(int i_unitsAgo) {
return getDateXBeforeNow(getMillsInAMinute(), i_unitsAgo);
}
/**
Get a Date that is X hours before now.
@returngetDateXBeforeNow(getMillsInAMinute(), i_unitsAgo)
**/
public final Date getDateXBNHours(int i_unitsAgo) {
return getDateXBeforeNow(getMillsInAMinute(), i_unitsAgo);
}
/**
Get a Date that is X days before now.
@returngetDateXBeforeNow(getMillsInADay(), i_unitsAgo)
**/
public final Date getDateXBNDays(int i_unitsAgo) {
return getDateXBeforeNow(getMillsInADay(), i_unitsAgo);
}
/**
Get a Date that is X weeks before now.
@returngetDateXBeforeNow(getMillsInAWeek(), i_unitsAgo)
**/
public final Date getDateXBNWeeks(int i_unitsAgo) {
return getDateXBeforeNow(getMillsInAWeek(), i_unitsAgo);
}
/**
Get a Date that is X units before now.
@returngetDateXAgo(l_multiplier, i_unitsAgo, (new Date()))
**/
public final Date getDateXBeforeNow(long l_multiplier, int i_unitsAgo) {
return getDateXAgo(l_multiplier, i_unitsAgo, (new Date()));
}
/**
Get a Date that is X seconds before the provided date.
@returngetDateXAgo(getMillsInASecond(), i_unitsAgo, d_from)
**/
public final Date getDateXAgoSeconds(int i_unitsAgo, Date d_from) {
return getDateXAgo(getMillsInASecond(), i_unitsAgo, d_from);
}
/**
Get a Date that is X minutes before the provided date.
@returngetDateXAgo(getMillsInAMinute(), i_unitsAgo, d_from)
**/
public final Date getDateXAgoMinutes(int i_unitsAgo, Date d_from) {
return getDateXAgo(getMillsInAMinute(), i_unitsAgo, d_from);
}
/**
Get a Date that is X hours before the provided date.
@returngetDateXAgo(getMillsInAMinute(), i_unitsAgo, d_from)
**/
public final Date getDateXAgoHours(int i_unitsAgo, Date d_from) {
return getDateXAgo(getMillsInAMinute(), i_unitsAgo, d_from);
}
/**
Get a Date that is X days before the provided date.
@returngetDateXAgo(getMillsInADay(), i_unitsAgo, d_from)
**/
public final Date getDateXAgoDays(int i_unitsAgo, Date d_from) {
return getDateXAgo(getMillsInADay(), i_unitsAgo, d_from);
}
/**
Get a Date that is X weeks before the provided date.
@returngetDateXAgo(getMillsInAWeek(), i_unitsAgo, d_from)
**/
public final Date getDateXAgoWeeks(int i_unitsAgo, Date d_from) {
return getDateXAgo(getMillsInAWeek(), i_unitsAgo, d_from);
}
/**
Get a Date that is X units before the provided date.
@param l_multiplier The number used to translate a Date's milleseconds to some other unit. May not be zero. @param i_unitsAgo The number of "units ago" you want the returned date to be, in relation to d_from. @param d_from The date that you want to count back from. May not be null. @returnnew Date(d_from.getTime() - (i_unitsAgo / l_multiplier))
**/
public final Date getDateXAgo(long l_multiplier, int i_unitsAgo, Date d_from) {
throwAXIfNull(d_from, "d_from", "getDateXAgo");
if(l_multiplier == 0) {
throwAX("getXMinusYUnits: l_multiplier is zero.");
}
return new Date(d_from.getTime() - (i_unitsAgo / l_multiplier));
}
/**
How many seconds before now is the provided date?
@returngetNowMinusYUnits(d_y, getMillsInASecond())
**/
public final int getNowMinusYSeconds(Date d_y) {
return getNowMinusYUnits(d_y, getMillsInASecond());
}
/**
How many minutes before now is the provided date?
@returngetNowMinusYUnits(d_y, getMillsInAMinute())
**/
public final int getNowMinusYMinutes(Date d_y) {
return getNowMinusYUnits(d_y, getMillsInAMinute());
}
/**
How many hours before now is the provided date?
@returngetNowMinusYUnits(d_y, getMillsInAnHour())
**/
public final int getNowMinusYHours(Date d_y) {
return getNowMinusYUnits(d_y, getMillsInAnHour());
}
/**
How many days before now is the provided date?
@returngetNowMinusYUnits(d_y, getMillsInADay())
**/
public final int getNowMinusYDays(Date d_y) {
return getNowMinusYUnits(d_y, getMillsInADay());
}
/**
How many weeks before now is the provided date?
@returngetNowMinusYUnits(d_y, getMillsInAWeek())
**/
public final int getNowMinusYWeeks(Date d_y) {
return getNowMinusYUnits(d_y, getMillsInAWeek());
}
/**
How many units before now is the provided date?
@returngetXMinusYUnits((new Date()), d_y, l_multiplier)
**/
public final int getNowMinusYUnits(Date d_y, long l_multiplier) {
return getXMinusYUnits((new Date()), d_y, l_multiplier);
}
/**
How many seconds separate the provided dates?
@returngetXMinusYUnits(d_x, d_y, getMillsInASecond())
**/
public final int getXMinusYSeconds(Date d_x, Date d_y) {
return getXMinusYUnits(d_x, d_y, getMillsInASecond());
}
/**
How many minutes separate the provided dates?
@returngetXMinusYUnits(d_x, d_y, getMillsInAMinute())
**/
public final int getXMinusYMinutes(Date d_x, Date d_y) {
return getXMinusYUnits(d_x, d_y, getMillsInAMinute());
}
/**
How many hours separate the provided dates?
@returngetXMinusYUnits(d_x, d_y, getMillsInAnHour())
**/
public final int getXMinusYHours(Date d_x, Date d_y) {
return getXMinusYUnits(d_x, d_y, getMillsInAnHour());
}
/**
How many days separate the provided dates?
@returngetXMinusYUnits(d_x, d_y, getMillsInADay())
**/
public final int getXMinusYDays(Date d_x, Date d_y) {
return getXMinusYUnits(d_x, d_y, getMillsInADay());
}
/**
How many weeks separate the provided dates?
@returngetXMinusYUnits(d_x, d_y, getMillsInAWeek())
**/
public final int getXMinusYWeeks(Date d_x, Date d_y) {
return getXMinusYUnits(d_x, d_y, getMillsInAWeek());
}
/**
How many units separate the provided dates?
@return(new Long(d_x, d_y, l_multiplier)).intValue()
**/
public final int getXMinusYUnits(Date d_x, Date d_y, long l_multiplier) {
return (new Long(((d_x.getTime() - d_y.getTime()) / l_multiplier))).intValue();
}
/**
How many units, represented by a long, separate the provided dates?
@param d_x The date on the left side of the minus equation. May not be null. @param d_y The date on the right side of the minus equation. May not be null. @param l_multiplier The number used to translate a Date's milleseconds to some other unit. May not be zero. @return(d_x.getTime() - d_y.getTime()) / l_multiplier)
**/
public final long getXMinusYUnitsLong(Date d_x, Date d_y, long l_multiplier) {
throwAXIfNull(d_x, "d_x", "getXMinusYUnits");
throwAXIfNull(d_y, "d_y", "getXMinusYUnits");
if(l_multiplier == 0) {
throwAX("getXMinusYUnits: l_multiplier is zero.");
}
return ((d_x.getTime() - d_y.getTime()) / l_multiplier);
}
/**
How many milleseconds are there in a second?
@return 1000 **/ public static final long getMillsInASecond() { return 1000; } /**How many milleseconds are there in a minute?
@return 60000 getMillsInASecond times sixty. **/ public static final long getMillsInAMinute() { return 60000; } /**How many milleseconds are there in an hour?
@return 360000 getMillsInAMinute times sixty **/ public static final long getMillsInAnHour() { return 3600000; } /**How many milleseconds are there in a day?
@return 8640000 getMillsInAnHour times twenty four **/ public static final long getMillsInADay() { return 8640000; } /**How many milleseconds are there in a week?
@return 60480000 getMillsInADay times seven **/ public static final long getMillsInAWeek() { return 60480000; } }