Package gr.zeus.util
Class DateHelper
java.lang.Object
gr.zeus.util.DateHelper
Helper methods for
java.util.Date
- Since:
- 1.05
- Author:
- Gregory Kotsaftis
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
addDaysToDate
(Calendar cal, int days) Adds 'days' to a calendar taking into consideration the month, year and leap years change as well.static String
dateToString
(Date uDate, String pattern) Converts a date to a string based on aSimpleDateFormat
pattern.static int
getDayForDate
(String dateToCheck, String pattern) Parses a string into a date.static String
getDayNameForDate
(Date dt, boolean fullname) Gets the name of a day based on a date and current locale.static int
getMonthForDate
(String dateToCheck, String pattern) Parses a string into a date.static int
getYearForDate
(String dateToCheck, String pattern) Parses a string into a date.static boolean
isDateValid
(String dateToCheck, String pattern) Checks a string to see if it contains a valid date inSimpleDateFormat
.static boolean
isLeapYear
(int year) Leap years occur in years exactly divisible by four, except those years ending in 00 are leap years only if they are divisible by 400.static Date
Parses a string into a date.static void
subDaysFromDate
(Calendar cal, int days) Subtracts 'days' from a calendar taking into consideration the month, year and leap years change as well.
-
Constructor Details
-
DateHelper
public DateHelper()
-
-
Method Details
-
subDaysFromDate
Subtracts 'days' from a calendar taking into consideration the month, year and leap years change as well.- Parameters:
cal
- TheCalendar
.days
- The number of days.
-
addDaysToDate
Adds 'days' to a calendar taking into consideration the month, year and leap years change as well.- Parameters:
cal
- TheCalendar
.days
- The number of days.
-
isLeapYear
public static boolean isLeapYear(int year) Leap years occur in years exactly divisible by four, except those years ending in 00 are leap years only if they are divisible by 400.- Parameters:
year
- The year number.- Returns:
true
if it is a leap year.
-
parseDate
Parses a string into a date. String should be inSimpleDateFormat
format. e.g.java.util.Date d = parseDate(myDate, "dd/MM/yyyy");
- Parameters:
myDate
- The date string.pattern
- The pattern to use.- Returns:
- The
Date
. - Throws:
ParseException
-
dateToString
Converts a date to a string based on aSimpleDateFormat
pattern. e.g.String s = dateToString(uDate, "dd/MM/yyyy");
- Parameters:
uDate
- The date string.pattern
- The pattern to use.- Returns:
- The string of the date or
null
on error.
-
isDateValid
Checks a string to see if it contains a valid date inSimpleDateFormat
.- Parameters:
dateToCheck
- The date string to check.pattern
- The pattern to use.- Returns:
true
if it contains a valid date inSimpleDateFormat
.
-
getYearForDate
Parses a string into a date. String should be inSimpleDateFormat
format. Returns only the year of the date or -1 on error.NOTE: only 'yyyy' is supported!
- Parameters:
dateToCheck
- The date string to check.pattern
- the The pattern to use.- Returns:
- The date or -1 on error.
-
getMonthForDate
Parses a string into a date. String should be inSimpleDateFormat
format. Returns only the month of the date or -1 on error.NOTE: only 'MM' is supported!
- Parameters:
dateToCheck
- The date string to check.pattern
- The pattern to use.- Returns:
- The date or -1 on error.
-
getDayForDate
Parses a string into a date. String should be inSimpleDateFormat
format. Returns only the day of the date or -1 on error.NOTE: only 'dd' is supported!
- Parameters:
dateToCheck
- The date string to check.pattern
- The pattern to use.- Returns:
- The day for the date or -1 on error.
-
getDayNameForDate
Gets the name of a day based on a date and current locale.- Parameters:
dt
- The date.fullname
- Fetch complete day's name or the short one.- Returns:
- A string with the name of the day.
-