Package com.identity4j.util
Class Util
- java.lang.Object
-
- com.identity4j.util.Util
-
public final class Util extends Object
Provides general purpose utilites.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidassertNotNull(Object toCheck, String name)Asserts that the supplied value is not null.static DateclearTime(Date date)Returns the given date with the time values cleared.static intcountIterator(Iterator<?> it)static CalendardayPrecision(Calendar calendar)Remove precision from a calendar so that it represents just a day.static longdaysToMillis(long days)Convert a number of days to millisecondsstatic booleandiffers(Object o1, Object o2)static StringfromObject(Object object)Return a very simple string represent of an object, assuming the object uses bean like getters (getXXX and isXXX).static DatefutureDate(long days)Calculates the date in the future by adding the supplied days to the current date.static longfutureTime(long days)Calculates the time in the future by adding the supplied days to the current time.static DategetEnd(Date date)Returns the given date with time set to the end of the daystatic ThrowablegetRootCause(Throwable exception, Class<? extends Throwable> clazz)static DategetStart(Date date)Returns the given date with the time set to the start of the day.static booleanhasTime(Date date)Determines whether or not a date has any time values.static booleanisAfterDay(Calendar cal1, Calendar cal2)Checks if the first calendar date is after the second calendar date ignoring time.static booleanisAfterDay(Date date1, Date date2)Checks if the first date is after the second date ignoring time.static booleanisBeforeDay(Calendar cal1, Calendar cal2)Checks if the first calendar date is before the second calendar date ignoring time.static booleanisBeforeDay(Date date1, Date date2)Checks if the first date is before the second date ignoring time.static booleanisDatePast(Date fromDate, int days)Calculates if the current time is after the date supplied + the number of days.static booleanisSameDay(Calendar cal1, Calendar cal2)Checks if two calendars represent the same day ignoring time.static booleanisSameDay(Date date1, Date date2)Checks if two dates are on the same day ignoring time.static booleanisToday(Calendar cal)Checks if a calendar date is today.static booleanisToday(Date date)Checks if a date is today.static booleanisWithinDaysFuture(Calendar cal, int days)Checks if a calendar date is after today and within a number of days in the future.static booleanisWithinDaysFuture(Date date, int days)Checks if a date is after today and within a number of days in the future.static Datemax(Date d1, Date d2)Returns the maximum of two dates.static voidmemDbg(String ctx)static Datemin(Date d1, Date d2)Returns the minimum of two dates.static LocaleparseLocale(String languageCountry)Parses the language / country code in a local objectstatic voidrandomSleep(long ms)Sleep for a short random amount of timestatic voidremoveMe(String message, String... args)Temporary debugging.static <T> List<T>toList(Iterator<T> it)static StringtraceString(Throwable t)Prints an exceptions trace to a string.static intunzip(File zipfile, File directory)static voidzip(File directory, File zipfile)
-
-
-
Field Detail
-
MAX_DATE
public static Date MAX_DATE
The maximum date possible.
-
-
Method Detail
-
isSameDay
public static boolean isSameDay(Date date1, Date date2)
Checks if two dates are on the same day ignoring time.
- Parameters:
date1- the first date, not altered, not nulldate2- the second date, not altered, not null- Returns:
- true if they represent the same day
- Throws:
IllegalArgumentException- if either date isnull
-
isSameDay
public static boolean isSameDay(Calendar cal1, Calendar cal2)
Checks if two calendars represent the same day ignoring time.
- Parameters:
cal1- the first calendar, not altered, not nullcal2- the second calendar, not altered, not null- Returns:
- true if they represent the same day
- Throws:
IllegalArgumentException- if either calendar isnull
-
isToday
public static boolean isToday(Date date)
Checks if a date is today.
- Parameters:
date- the date, not altered, not null.- Returns:
- true if the date is today.
- Throws:
IllegalArgumentException- if the date isnull
-
isToday
public static boolean isToday(Calendar cal)
Checks if a calendar date is today.
- Parameters:
cal- the calendar, not altered, not null- Returns:
- true if cal date is today
- Throws:
IllegalArgumentException- if the calendar isnull
-
isBeforeDay
public static boolean isBeforeDay(Date date1, Date date2)
Checks if the first date is before the second date ignoring time.
- Parameters:
date1- the first date, not altered, not nulldate2- the second date, not altered, not null- Returns:
- true if the first date day is before the second date day.
- Throws:
IllegalArgumentException- if the date isnull
-
isBeforeDay
public static boolean isBeforeDay(Calendar cal1, Calendar cal2)
Checks if the first calendar date is before the second calendar date ignoring time.
- Parameters:
cal1- the first calendar, not altered, not null.cal2- the second calendar, not altered, not null.- Returns:
- true if cal1 date is before cal2 date ignoring time.
- Throws:
IllegalArgumentException- if either of the calendars arenull
-
isAfterDay
public static boolean isAfterDay(Date date1, Date date2)
Checks if the first date is after the second date ignoring time.
- Parameters:
date1- the first date, not altered, not nulldate2- the second date, not altered, not null- Returns:
- true if the first date day is after the second date day.
- Throws:
IllegalArgumentException- if the date isnull
-
isAfterDay
public static boolean isAfterDay(Calendar cal1, Calendar cal2)
Checks if the first calendar date is after the second calendar date ignoring time.
- Parameters:
cal1- the first calendar, not altered, not null.cal2- the second calendar, not altered, not null.- Returns:
- true if cal1 date is after cal2 date ignoring time.
- Throws:
IllegalArgumentException- if either of the calendars arenull
-
isWithinDaysFuture
public static boolean isWithinDaysFuture(Date date, int days)
Checks if a date is after today and within a number of days in the future.
- Parameters:
date- the date to check, not altered, not null.days- the number of days.- Returns:
- true if the date day is after today and within days in the future .
- Throws:
IllegalArgumentException- if the date isnull
-
isWithinDaysFuture
public static boolean isWithinDaysFuture(Calendar cal, int days)
Checks if a calendar date is after today and within a number of days in the future.
- Parameters:
cal- the calendar, not altered, not nulldays- the number of days.- Returns:
- true if the calendar date day is after today and within days in the future .
- Throws:
IllegalArgumentException- if the calendar isnull
-
getStart
public static Date getStart(Date date)
Returns the given date with the time set to the start of the day.
-
clearTime
public static Date clearTime(Date date)
Returns the given date with the time values cleared.
-
hasTime
public static boolean hasTime(Date date)
Determines whether or not a date has any time values.- Parameters:
date- The date.- Returns:
- true iff the date is not null and any of the date's hour, minute, seconds or millisecond values are greater than zero.
-
getEnd
public static Date getEnd(Date date)
Returns the given date with time set to the end of the day
-
max
public static Date max(Date d1, Date d2)
Returns the maximum of two dates. A null date is treated as being less than any non-null date.
-
min
public static Date min(Date d1, Date d2)
Returns the minimum of two dates. A null date is treated as being greater than any non-null date.
-
countIterator
public static int countIterator(Iterator<?> it)
-
traceString
public static String traceString(Throwable t)
Prints an exceptions trace to a string.- Parameters:
t- exception- Returns:
- trace
-
parseLocale
public static Locale parseLocale(String languageCountry)
Parses the language / country code in a local object- Parameters:
languageCountry- language country- Returns:
- locale
-
isDatePast
public static boolean isDatePast(Date fromDate, int days)
Calculates if the current time is after the date supplied + the number of days.- Parameters:
fromDate-days-- Returns:
-
futureDate
public static Date futureDate(long days)
Calculates the date in the future by adding the supplied days to the current date.- Parameters:
days-- Returns:
- date
-
futureTime
public static long futureTime(long days)
Calculates the time in the future by adding the supplied days to the current time.- Parameters:
days-- Returns:
- time
-
daysToMillis
public static long daysToMillis(long days)
Convert a number of days to milliseconds- Parameters:
days- days- Returns:
- milliseconds
-
assertNotNull
public static void assertNotNull(Object toCheck, String name)
Asserts that the supplied value is not null.- Parameters:
toCheck- the value to checkname- the name of the supplied value- Throws:
IllegalArgumentException- if the supplied value is null.
-
fromObject
public static String fromObject(Object object)
Return a very simple string represent of an object, assuming the object uses bean like getters (getXXX and isXXX). Only looks for zero argument methods and catches exceptions to replace with the text "!Exception!".- Parameters:
object- object- Returns:
- string
-
dayPrecision
public static Calendar dayPrecision(Calendar calendar)
Remove precision from a calendar so that it represents just a day. A new instance is returned- Parameters:
calendar- calendar- Returns:
- new cal
-
removeMe
public static void removeMe(String message, String... args)
Temporary debugging. References to this method should be checked before release.- Parameters:
message- message patternargs- message arguments
-
getRootCause
public static Throwable getRootCause(Throwable exception, Class<? extends Throwable> clazz)
-
randomSleep
public static void randomSleep(long ms)
Sleep for a short random amount of time- Parameters:
ms- max time
-
unzip
public static int unzip(File zipfile, File directory) throws IOException
- Throws:
IOException
-
zip
public static void zip(File directory, File zipfile) throws IOException
- Throws:
IOException
-
memDbg
public static void memDbg(String ctx)
-
-