org.jminor.common.model
Class DateUtil

java.lang.Object
  extended by org.jminor.common.model.DateUtil

public final class DateUtil
extends Object

A static utility class for date handling.


Method Summary
static Date floorDate(Date date)
           
static Timestamp floorTimestamp(Timestamp timestamp)
           
static Date getDate(int year, int month, int day)
           
static String getDateMask(SimpleDateFormat format)
          Parses the date pattern and returns mask string that can be used in JFormattedFields.
static Date getFirstDayOfLastMonth()
           
static Date getFirstDayOfMonth(int toAdd)
           
static Date getFirstDayOfQuarter(int quarter)
          Returns the first day of the the given quarter, assuming quarters begin in january, april, july and october.
static Date getFirstDayOfYear()
           
static Date getLastDayOfLastMonth()
           
static Date getLastDayOfMonth(int toAdd)
           
static Date getLastDayOfQuarter(int quarter)
          Returns the last day of the the given quarter, assuming quarters begin in january, april, july and october.
static Date getLastDayOfYear()
           
static ThreadLocal<DateFormat> getThreadLocalDateFormat(String formatString)
           
static Date getYesterday()
           
static boolean isDateValid(String dateString, boolean emptyStringOk, DateFormat... formats)
           
static boolean isDateValid(String date, DateFormat... formats)
           
static int numberOfDaysInRange(Date from, Date to)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isDateValid

public static boolean isDateValid(String date,
                                  DateFormat... formats)
Parameters:
date - the date to check for validity
formats - the date formats to use for validation
Returns:
true if the date is valid

isDateValid

public static boolean isDateValid(String dateString,
                                  boolean emptyStringOk,
                                  DateFormat... formats)
Parameters:
dateString - the date to check for validity
emptyStringOk - if true then an empty string is regarded as a valid date
formats - the date formats to use for validation
Returns:
true if the date is valid, using the given date formats

floorDate

public static Date floorDate(Date date)
Parameters:
date - the Date object to floor
Returns:
a Date object with the same time as timestamp except the Calendar.SECOND and Calendar.MILLISECOND fields are set to zero

floorTimestamp

public static Timestamp floorTimestamp(Timestamp timestamp)
Parameters:
timestamp - the Timestamp object to floor
Returns:
a Timestamp object with the same time as timestamp except the Calendar.SECOND and Calendar.MILLISECOND fields are set to zero

getDate

public static Date getDate(int year,
                           int month,
                           int day)
Parameters:
year - the year
month - the month
day - the day
Returns:
a Date based on the given values

getYesterday

public static Date getYesterday()
Returns:
yesterday

getFirstDayOfLastMonth

public static Date getFirstDayOfLastMonth()
Returns:
the first day of last month

getLastDayOfLastMonth

public static Date getLastDayOfLastMonth()
Returns:
the last day of last month

getFirstDayOfMonth

public static Date getFirstDayOfMonth(int toAdd)
Parameters:
toAdd - the number of months to add to the current month
Returns:
the first day of the month toAdd from the current month

getLastDayOfMonth

public static Date getLastDayOfMonth(int toAdd)
Parameters:
toAdd - the number of months to add to the current month
Returns:
the last day of the month toAdd from the current month

getFirstDayOfYear

public static Date getFirstDayOfYear()
Returns:
the first day of the current year

getLastDayOfYear

public static Date getLastDayOfYear()
Returns:
the last day of the current year

getFirstDayOfQuarter

public static Date getFirstDayOfQuarter(int quarter)
Returns the first day of the the given quarter, assuming quarters begin in january, april, july and october.

Parameters:
quarter - the quarter, 1, 2, 3 or 4
Returns:
the first day of the given quarter

getLastDayOfQuarter

public static Date getLastDayOfQuarter(int quarter)
Returns the last day of the the given quarter, assuming quarters begin in january, april, july and october.

Parameters:
quarter - the quarter, 1, 2, 3 or 4
Returns:
the last day of the given quarter

numberOfDaysInRange

public static int numberOfDaysInRange(Date from,
                                      Date to)
Parameters:
from - the from date
to - the to date
Returns:
the number of days in the given interval

getThreadLocalDateFormat

public static ThreadLocal<DateFormat> getThreadLocalDateFormat(String formatString)
Parameters:
formatString - the format string
Returns:
a thread local date format based on the given format string

getDateMask

public static String getDateMask(SimpleDateFormat format)
Parses the date pattern and returns mask string that can be used in JFormattedFields. This only works with plain numerical date formats.

Parameters:
format - the SimpleDateFormat for which to retrieve the date mask
Returns:
a String representing the mask to use in JFormattedTextFields, i.e. "##-##-####"