Class DateUtil

java.lang.Object
  extended by DateUtil

public class DateUtil
extends java.lang.Object

A class DateUtil with the following methods for dealing with dates. public static String convertDateToLongString(Date date) public static String convertDateToShortString(Date date) public static Date convertStringToDate(String dateString) public static int daysBetween(Date startDate, Date endDate) public static Date incrementDate(Date date, int noOfDays) public static boolean isLeapYear(int year) public static boolean isValidDateString(String dateString) public static Date nextDate(Date date)

Author:
D E Newton

Field Summary
static java.text.SimpleDateFormat dateFormatter
           
 
Constructor Summary
DateUtil()
           
 
Method Summary
static java.lang.String convertDateToLongString(java.util.Date date)
          Converts a Date object to a corresponding String in the long date pattern style "Saturday, 25 March 2023".
static java.lang.String convertDateToShortString(java.util.Date date)
          Converts a Date object to a corresponding String in the short date pattern style "25-03-2023".
static java.util.Date convertStringToDate(java.lang.String dateString)
          Converts a string in the short date pattern style "25-03-2023" to a corresponding Date object.
static int daysBetween(java.util.Date startDate, java.util.Date endDate)
          Calculates the number of days between two given dates, startDate and endDate.
static java.util.Date incrementDate(java.util.Date date, int noOfDays)
          Given date, a Date object, and noOfDays, an int, the method returns a Date object corresponding to noOfDays later than date.
static boolean isLeapYear(int year)
          Given year, an int, the method checks to see if the year is a leap year.
static boolean isValidDateString(java.lang.String dateString)
          Given dateString, a String, the method checks to see if string corresponds to a valid shortDatePattern.
static java.util.Date nextDate(java.util.Date date)
          Given date, a Date object, the method returns a Date object corresponding to the next day.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dateFormatter

public static java.text.SimpleDateFormat dateFormatter
Constructor Detail

DateUtil

public DateUtil()
Method Detail

convertDateToLongString

public static java.lang.String convertDateToLongString(java.util.Date date)
Converts a Date object to a corresponding String in the long date pattern style "Saturday, 25 March 2023".

Parameters:
date - a Date object
Returns:
a String, containing a long date pattern

convertDateToShortString

public static java.lang.String convertDateToShortString(java.util.Date date)
Converts a Date object to a corresponding String in the short date pattern style "25-03-2023".

Parameters:
date - a Date object
Returns:
a String, containing a short date pattern

convertStringToDate

public static java.util.Date convertStringToDate(java.lang.String dateString)
Converts a string in the short date pattern style "25-03-2023" to a corresponding Date object. Any leading or trailing spaces are first removed from the date string. The String parameter that represent a date as a string must be in the format dd-mm-yyy (e.g. 25-03-2023 or 25-3-2023) where dd represents one or two digits representing the day in the month, similarly for mm representing the month in the year and yyyy represents the four digits for the year. A RuntimeException is thrown if the date string is not recognised as a valid date. Such exceptions do not need to be caught or thrown as they are unchecked exceptions, but can be caught if necessary.

Parameters:
dateString - a Date object
Returns:
the Date object

daysBetween

public static int daysBetween(java.util.Date startDate,
                              java.util.Date endDate)
Calculates the number of days between two given dates, startDate and endDate. If startDate is after endDate then the number of days returned will be negative.

Parameters:
startDate - a Date object
endDate - a Date object
Returns:
an int, number of days between the dates

incrementDate

public static java.util.Date incrementDate(java.util.Date date,
                                           int noOfDays)
Given date, a Date object, and noOfDays, an int, the method returns a Date object corresponding to noOfDays later than date. If noOfDays is negative, the resulting Date object will be before date.

Parameters:
date - a Date object
noOfDays - an int
Returns:
a Date

isLeapYear

public static boolean isLeapYear(int year)
Given year, an int, the method checks to see if the year is a leap year.

Parameters:
year, - an int
Returns:
a boolean, true only if the year is a leap year.

isValidDateString

public static boolean isValidDateString(java.lang.String dateString)
Given dateString, a String, the method checks to see if string corresponds to a valid shortDatePattern.

Parameters:
dateString, - a String
Returns:
a boolean, true only if the dateString is a valid pattern

nextDate

public static java.util.Date nextDate(java.util.Date date)
Given date, a Date object, the method returns a Date object corresponding to the next day.

Parameters:
noOfDays - an int
Returns:
a Date