org.hrodberaht.directus.util
Class DateUtil

java.lang.Object
  extended by org.hrodberaht.directus.util.DateUtil

public final class DateUtil
extends java.lang.Object

Simple Java Utils

Since:
1.0
Version:
1.0
Author:
Robert Alexandersson

Method Summary
static boolean after(java.util.Date theDate, java.util.Date compare)
          Compares if the date is after the compare date
static boolean before(java.util.Date theDate, java.util.Date compare)
          Compares if the date is after the compare date
static boolean equals(java.util.Date date1, java.util.Date date2)
          A different version than the java version of Date.equals, it does not care about type.
static java.lang.String formatDate(java.util.Date date)
          Formats the date according to selected locale pattern for short dates (no time).
static java.lang.String formatDate(java.util.Date date, java.lang.String pattern)
          Formats the date according to set pattern, see normal dateformattingrules for pattern
static java.lang.String formatDateTime(java.util.Date date)
          Formats the date according to selected locale pattern for long dates (date & time).
static boolean formattedDateEquals(java.util.Date date1, java.util.Date date2, java.lang.String pattern)
          Reformat the dates according to pattern and then calls equals on them.
static java.lang.String formatTimeZone(java.util.Date theDate)
          Formatted version of selected locale (DateUtil#locale) timezone
static java.util.Date getNow()
          This is very useful if a developer needs to override what the time is for specific tests.
static boolean isBetween(java.util.Date theDate, java.util.Date fromDate, java.util.Date toDate)
          Date is considered to be in between if it is equals to or less than toDate or equal to or more than fromDate
static boolean isFirstInMonth(java.util.Date date)
          Is the date the first in the month of its own definition.
static boolean isFirstInThisMonth(java.util.Date date)
          Is the date the first in the current month, uses DateUtil.getNow for compare
static boolean isLastInMonth(java.util.Date date)
          Is the date the last in the month of its own definition.
static boolean isLessOrEqual(java.util.Date theDate, java.util.Date compare)
          Compare if the dates are less or equal
static boolean isMoreOrEqual(java.util.Date theDate, java.util.Date compare)
          Compare if the dates are more or equal
static java.util.Date parseDate(java.lang.String date, java.lang.String pattern)
          Parse a date according to the provided pattern
static java.util.Date parseSimpleDate(java.lang.String date)
          A simple parser that can find out what date/time type the selected inputted string is of and tries to parse that.
static java.util.Date rollDays(java.util.Date date, int days)
          Uses a Calendar.add(Calendar.DAY_OF_YEAR, hours), but is usable from a Date as input.
static java.util.Date rollHours(java.util.Date date, int hours)
          Uses a Calendar.add(Calendar.HOUR_OF_DAY, hours), but is usable from a Date as input.
static java.util.Date rollMonth(java.util.Date date, int months)
          Uses a Calendar.add(Calendar.MONTH, hours), but is usable from a Date as input.
static void setLocale(java.util.Locale locale)
          Utility method to change the locale of the DateUtil, affects the entire server as its static Will call DateUtil.updateFormatPatterns after setting the locale
static java.util.Date setTimeToActualMaximum(java.util.Date date)
          Sets the maximum possible time (hours, minutes, seconds, millis) and returns as a new instance of date.
static java.util.Date setTimeToActualMinimum(java.util.Date date)
          Sets the minimum possible time (hours, minutes, seconds, millis) and returns as a new instance of date.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

setLocale

public static void setLocale(java.util.Locale locale)
Utility method to change the locale of the DateUtil, affects the entire server as its static Will call DateUtil.updateFormatPatterns after setting the locale

Parameters:
locale - the locale to change to

isLessOrEqual

public static boolean isLessOrEqual(java.util.Date theDate,
                                    java.util.Date compare)
Compare if the dates are less or equal

Parameters:
theDate - the date
compare - the compare date for compare
Returns:
result of compare for equals and DateUtil.before

isMoreOrEqual

public static boolean isMoreOrEqual(java.util.Date theDate,
                                    java.util.Date compare)
Compare if the dates are more or equal

Parameters:
theDate - the date
compare - the compare date for compare
Returns:
result of compare for equals and DateUtil.after

after

public static boolean after(java.util.Date theDate,
                            java.util.Date compare)
Compares if the date is after the compare date

Parameters:
theDate - base date for compare
compare - the compare date for compare
Returns:
result of compare

before

public static boolean before(java.util.Date theDate,
                             java.util.Date compare)
Compares if the date is after the compare date

Parameters:
theDate - base date for compare
compare - the compare date for compare
Returns:
result of compare

parseDate

public static java.util.Date parseDate(java.lang.String date,
                                       java.lang.String pattern)
Parse a date according to the provided pattern

Parameters:
date - the string to use for parsing
pattern - the pattern to use for parsing
Returns:
parsed date object

formattedDateEquals

public static boolean formattedDateEquals(java.util.Date date1,
                                          java.util.Date date2,
                                          java.lang.String pattern)
Reformat the dates according to pattern and then calls equals on them. Useful for comparing if a date is on the same month or day even though there is more details

Parameters:
date1 - date for compare
date2 - date for compare
pattern - to apply before compare
Returns:
results of compare

equals

public static boolean equals(java.util.Date date1,
                             java.util.Date date2)
A different version than the java version of Date.equals, it does not care about type.

Parameters:
date1 - date for compare
date2 - date for compare
Returns:
the result of date1.getTime() == date2.getTime()

parseSimpleDate

public static java.util.Date parseSimpleDate(java.lang.String date)
A simple parser that can find out what date/time type the selected inputted string is of and tries to parse that. Uses the DateUtil.LONG_DATE and DateUtil.SHORT_DATE for its logic

Parameters:
date - the string to test for parsing
Returns:
parsed date object

formatTimeZone

public static java.lang.String formatTimeZone(java.util.Date theDate)
Formatted version of selected locale (DateUtil#locale) timezone

Parameters:
-
theDate - date to check timezone for
Returns:
formatted timezone information for the date that is inout to method

formatDate

public static java.lang.String formatDate(java.util.Date date)
Formats the date according to selected locale pattern for short dates (no time). Uses the Locale to figure out the pattern to use then just calls the java.text.SimpleDateFormat#toLocalizedPattern(). Long patters is created using DateFormat.getDateInstance(DateFormat.SHORT, locale)

Parameters:
date - date for formatting
Returns:
formatted date according to pattern
See Also:
SimpleDateFormat.toLocalizedPattern()

formatDateTime

public static java.lang.String formatDateTime(java.util.Date date)
Formats the date according to selected locale pattern for long dates (date & time). Uses the Locale to figure out the pattern to use then just calls the java.text.SimpleDateFormat#toLocalizedPattern(). Long patters is created using DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale)

Parameters:
date - date for formatting
Returns:
formatted date according to pattern
See Also:
SimpleDateFormat.toLocalizedPattern()

formatDate

public static java.lang.String formatDate(java.util.Date date,
                                          java.lang.String pattern)
Formats the date according to set pattern, see normal dateformattingrules for pattern

Parameters:
date - date for formatting
pattern - intended pattern
Returns:
formatted date according to pattern
See Also:
for patterns

rollDays

public static java.util.Date rollDays(java.util.Date date,
                                      int days)
Uses a Calendar.add(Calendar.DAY_OF_YEAR, hours), but is usable from a Date as input. The date returned is a new instance

Parameters:
date - date to roll
days - amount of days to roll
Returns:
the rolled date
See Also:
Calendar.add(int, int)

rollMonth

public static java.util.Date rollMonth(java.util.Date date,
                                       int months)
Uses a Calendar.add(Calendar.MONTH, hours), but is usable from a Date as input. The date returned is a new instance

Parameters:
date - date to roll
months - amount of months to roll
Returns:
the rolled date
See Also:
Calendar.add(int, int)

rollHours

public static java.util.Date rollHours(java.util.Date date,
                                       int hours)
Uses a Calendar.add(Calendar.HOUR_OF_DAY, hours), but is usable from a Date as input. The date returned is a new instance

Parameters:
date - date to roll
hours - amount of hours to roll
Returns:
the rolled date
See Also:
Calendar.add(int, int)

setTimeToActualMinimum

public static java.util.Date setTimeToActualMinimum(java.util.Date date)
Sets the minimum possible time (hours, minutes, seconds, millis) and returns as a new instance of date.

Parameters:
date - date for minimum time settings
Returns:
the new minimum times date (uses the incoming date as template)

setTimeToActualMaximum

public static java.util.Date setTimeToActualMaximum(java.util.Date date)
Sets the maximum possible time (hours, minutes, seconds, millis) and returns as a new instance of date.

Parameters:
date - date for maximum time settings
Returns:
the new maximum times date (uses the incoming date as template)

getNow

public static java.util.Date getNow()
This is very useful if a developer needs to override what the time is for specific tests. If all parts of the software uses this instead of "new Date" it will be possible to change time for specific

Returns:
the now date (replaces new Date), when used the TestUtilDateUtil class for date manipulation can be used.
See Also:
TestUtilDateUtil.setNowDate(java.util.Date)

isFirstInThisMonth

public static boolean isFirstInThisMonth(java.util.Date date)
Is the date the first in the current month, uses DateUtil.getNow for compare

Parameters:
date -
Returns:
true if it is the first day in this, false if not
See Also:
getNow()

isFirstInMonth

public static boolean isFirstInMonth(java.util.Date date)
Is the date the first in the month of its own definition.

Parameters:
date -
Returns:
true if it is the first day, false if not

isLastInMonth

public static boolean isLastInMonth(java.util.Date date)
Is the date the last in the month of its own definition.

Parameters:
date -
Returns:
true if it is the last day, false if not

isBetween

public static boolean isBetween(java.util.Date theDate,
                                java.util.Date fromDate,
                                java.util.Date toDate)
Date is considered to be in between if it is equals to or less than toDate or equal to or more than fromDate

Parameters:
theDate - is the date for comparison
fromDate - is the start value
toDate - is the end value
Returns:
true when considered in between, false otherwise


Copyright © 2010. All Rights Reserved.