net.sourceforge.jaulp.date
Class DateUtils

java.lang.Object
  extended by net.sourceforge.jaulp.date.DateUtils
All Implemented Interfaces:
DatePatterns

public class DateUtils
extends java.lang.Object
implements DatePatterns

Utility class for the use of Date and Calendar object.

Version:
1.0
Author:
Asterios Raptis

Field Summary
 
Fields inherited from interface net.sourceforge.jaulp.date.DatePatterns
DD_MMM_YY, DOT_DD_MM, DOT_DD_MM_YY, DOT_DD_MM_YYYY, DOT_DD_MM_YYYY_HH_MM_SS, DOT_YYYY_MM_DD_HH_MM_SS, EEE_MMM_DD_HH_MM_SS_Z_YYYY, EEEE_MMM_DD_YYYY, LHH_MM_SS, UHH_MM_SS, YYYY_MM_DD, YYYY_MM_DD_HH_MM_SS, YYYY_MM_DD_HH_MM_SS_S, YYYY_MM_DD_T_HH_MM_SS, YYYYMMDDHHMMSS
 
Method Summary
static java.util.Date addDaysToDate(java.util.Date date, int addDays)
          Adds days to the given Date object and returns it.
static java.util.Date addYearsToDate(java.util.Date date, int addYears)
          Adds years to the given Date object and returns it.
static long calculateElapsedTime(java.util.Date past, java.util.Date now)
          Calculates the elapsed time from the past to now.
static long calculateTimeFromNow(java.util.Date now, java.util.Date future)
          Calculates the elapsed time from the future to now.
static int computeAge(java.util.Date birthday, java.util.Date computeDate)
          Computes the Age from the birthday till the computeDate object.
static java.util.Date computeEasternSunday(int year)
          Computes the eastern sunday for the given year.
static int computeEasternSundayNumber(int year)
          Computes the number from eastern sunday for the given year.
 java.util.Calendar convertDateToCalendar(java.util.Date date)
          Converts the given Date object to a Calendar object.
static java.util.Calendar convertToCalendar(java.util.Date date)
          Converts a Date to a Calendar-object.
static java.util.Date convertToDate(java.util.Calendar calendar)
          Converts a Calendar-object to a Date.
static java.sql.Timestamp convertToTimestamp(java.util.Date date)
          Converts a Date to a Timestamp-object.
static java.util.Date createDate(int year, int month, int day)
          Creates a Date object from the given values.
static java.util.Date createDate(int year, int month, int day, int hour, int min, int sec)
          Creates a Date object from the given values.
static java.util.Date createRandomBirthday()
          Creates a random Birthday between the range 9 and 55 years.
static java.util.Date createRandomBirthday(java.util.Date from, java.util.Date till)
          Creates a random Birthday between the given two Date objects.
static java.util.Date createRandomDate(java.util.Date from)
          Deprecated. Use instead RandomDateUtils.createRandomDate( from ).
static java.util.Date createRandomDatebetween(java.util.Date start, java.util.Date end)
          Deprecated. Use instead RandomDateUtils.createRandomDatebetween(start, end ).
static java.util.Date createRandomDateBetween(java.util.Date from, int startDays, int endDays)
          Deprecated. Use instead RandomDateUtils.createRandomDateBetween( from, startDays, endDays ).
static java.lang.String createRandomDatebetween(long startDate, long endDate)
          Deprecated. Use instead RandomDateUtils.createRandomDatebetween( startDate, endDate, format ).
static java.lang.String createRandomDatebetween(long startDate, long endDate, java.lang.String format)
          Deprecated. Use instead RandomDateUtils.createRandomDatebetween( startDate, endDate, format ).
static java.util.List getAllDateFormats()
          Returns a list with all dateformats from the Interface DateFormats.
static java.util.Map<java.lang.String,java.lang.Object> getAllDatePatterns()
          Returns a map with all datepatterns from the Interface DatePatterns.
static int getDay(java.util.Date date)
          The Method from the Date.getDay is deprecated.
static int getHours(java.util.Date date)
          The Method from the Date.getHours is deprecated.
static int getMinutes(java.util.Date date)
          The Method from the Date.getMinutes is deprecated.
static int getMonth(java.util.Date date)
          The Method from the Date.getMonth is deprecated.
static int getSeconds(java.util.Date date)
          The Method from the Date.getSeconds is deprecated.
static int getYear(java.util.Date date)
          The Method from the Date.getYear is deprecated.
static boolean isBetween(java.util.Date start, java.util.Date end, java.util.Date between)
          Checks if the Date object "between" is between from the given to Date objects.
static boolean isDateInTheFuture(java.util.Date date)
          Checks if the given date object is in the future.
static boolean isDateInThePast(java.util.Date date)
          Checks if the given date object is in the past.
static boolean isLeapYear(int year)
          Determines if the given year is a leap year.
static boolean isValidDate(java.lang.String date, java.lang.String format, boolean lenient)
          Checks if the Date is valid to convert.
static java.util.Date parseDate(java.lang.String date, java.util.List<java.lang.String> patterns)
          Tries to convert the given String to a Date.
static java.util.Date parseToDate(java.lang.String date, java.lang.String format)
          Parses the String date to a date object.
static java.util.Date parseToDate(java.lang.String datum, java.lang.String[] formats, java.util.Locale locale)
          Returns a date-object if the array with the formats are valid otherwise null.
static java.util.Date parseToDateLenient(java.lang.String date, java.lang.String format, boolean lenient)
          Parses the String date to a date object.
static java.lang.String parseToString(java.util.Date date, java.lang.String format)
          The Method parseToString(Date, String) formats the given Date to the given Format.
static java.util.Date substractDaysFromDate(java.util.Date date, int substractDays)
          Substract days to the given Date object and returns it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

addDaysToDate

public static java.util.Date addDaysToDate(java.util.Date date,
                                           int addDays)
Adds days to the given Date object and returns it.

Parameters:
date - The Date object to add the days.
addDays - The days to add.
Returns:
The resulted Date object.

addYearsToDate

public static java.util.Date addYearsToDate(java.util.Date date,
                                            int addYears)
Adds years to the given Date object and returns it.

Parameters:
date - The Date object to add the years.
addYears - The years to add.
Returns:
The resulted Date object.

calculateElapsedTime

public static long calculateElapsedTime(java.util.Date past,
                                        java.util.Date now)
Calculates the elapsed time from the past to now.

Parameters:
past - The Date object in the past.
now - The Date from now.
Returns:
The elapsed time in long.

calculateTimeFromNow

public static long calculateTimeFromNow(java.util.Date now,
                                        java.util.Date future)
Calculates the elapsed time from the future to now.

Parameters:
now - The Date from now.
future - The Date in the future.
Returns:
The elapsed time in long.

computeAge

public static int computeAge(java.util.Date birthday,
                             java.util.Date computeDate)
Computes the Age from the birthday till the computeDate object.

Parameters:
birthday - The Date object from the birthday.
computeDate - The Date-object from where to compute.
Returns:
Returns the computed age.

computeEasternSunday

public static java.util.Date computeEasternSunday(int year)
Computes the eastern sunday for the given year. Year should be greater the 1583.

Parameters:
year - The year to compute the eastern sunday.
Returns:
The eastern sunday.

computeEasternSundayNumber

public static int computeEasternSundayNumber(int year)
Computes the number from eastern sunday for the given year. Year should be greater the 1583.

Parameters:
year - The year to compute the number from eastern sunday.
Returns:
The number from eastern sunday.

convertToCalendar

public static java.util.Calendar convertToCalendar(java.util.Date date)
Converts a Date to a Calendar-object.

Parameters:
date - The date to convert.
Returns:
The calendar from the date.

convertToDate

public static java.util.Date convertToDate(java.util.Calendar calendar)
Converts a Calendar-object to a Date.

Parameters:
calendar - The calendar to convert.
Returns:
The date from the calendar.

convertToTimestamp

public static java.sql.Timestamp convertToTimestamp(java.util.Date date)
Converts a Date to a Timestamp-object.

Parameters:
date - The date to convert.
Returns:
The Timestamp from the date.

createDate

public static java.util.Date createDate(int year,
                                        int month,
                                        int day)
Creates a Date object from the given values.

Parameters:
year - The year.
month - The month.
day - The day.
Returns:
Returns the created Date object.

createDate

public static java.util.Date createDate(int year,
                                        int month,
                                        int day,
                                        int hour,
                                        int min,
                                        int sec)
Creates a Date object from the given values.

Parameters:
year - The year.
month - The month.
day - The day.
hour - The hour.
min - The minutes.
sec - The seconds.
Returns:
Returns the created Date object.

createRandomBirthday

public static java.util.Date createRandomBirthday()
Creates a random Birthday between the range 9 and 55 years.

Returns:
Returns a random Birthday between the range 9 and 55 years.

createRandomBirthday

public static java.util.Date createRandomBirthday(java.util.Date from,
                                                  java.util.Date till)
Creates a random Birthday between the given two Date objects.

Parameters:
from - A Date object before the Date object "till".
till - A Date object after the Date object "from".
Returns:
Returns a random Birthday as a Date object.

createRandomDate

@Deprecated
public static java.util.Date createRandomDate(java.util.Date from)
Deprecated. Use instead RandomDateUtils.createRandomDate( from ).

Creates a random Date object in the past from the given date.

Parameters:
from - The Date from where the range starts to the past.
Returns:
The generated random Date object.

createRandomDatebetween

@Deprecated
public static java.util.Date createRandomDatebetween(java.util.Date start,
                                                                java.util.Date end)
Deprecated. Use instead RandomDateUtils.createRandomDatebetween(start, end ).

Creates a random Date between the range from start and end.

Parameters:
start - The Date from where the range starts.
end - The Date from where the range ends.
Returns:
A random Date between the range from start and end.

createRandomDatebetween

@Deprecated
public static java.lang.String createRandomDatebetween(long startDate,
                                                                  long endDate)
Deprecated. Use instead RandomDateUtils.createRandomDatebetween( startDate, endDate, format ).

Creates a random Date between the range from startDate and endDate in the default pattern "dd.MM.yyyy HH:mm:ss".

Parameters:
startDate - The Date from where the random Date to start.
endDate - The Date from where the random Date to end.
Returns:
A random Date between the range from startDate and endDate in the default pattern "dd.MM.yyyy HH:mm:ss".

createRandomDatebetween

@Deprecated
public static java.lang.String createRandomDatebetween(long startDate,
                                                                  long endDate,
                                                                  java.lang.String format)
Deprecated. Use instead RandomDateUtils.createRandomDatebetween( startDate, endDate, format ).

Creates a random Date between the range from startDate and endDate in the given pattern.

Parameters:
startDate - The Date from where the random Date to start.
endDate - The Date from where the random Date to end.
format - The pattern from the Date.
Returns:
A random Date between the range from startDate and endDate in the given pattern.

createRandomDateBetween

@Deprecated
public static java.util.Date createRandomDateBetween(java.util.Date from,
                                                                int startDays,
                                                                int endDays)
Deprecated. Use instead RandomDateUtils.createRandomDateBetween( from, startDays, endDays ).

Creates a random Date between the range from startDays and endDays from the given Date.

Parameters:
from - The Date from where to the random Date to start.
startDays - The int that represents the days from where the range starts.
endDays - The int that represents the days from where the range ends.
Returns:
A random Date between the range from startDays and endDays from the given Date.

getAllDateFormats

public static java.util.List getAllDateFormats()
Returns a list with all dateformats from the Interface DateFormats.

Returns:
Returns a list with all dateformats from the Interface DateFormats.

getAllDatePatterns

public static java.util.Map<java.lang.String,java.lang.Object> getAllDatePatterns()
Returns a map with all datepatterns from the Interface DatePatterns. As key is the name from the pattern.

Returns:
Returns a Map with all datepatterns from the Interface DatePatterns.

getDay

public static int getDay(java.util.Date date)
The Method from the Date.getDay is deprecated. This is a helper-Method.

Parameters:
date - The Date-object to get the day.
Returns:
The day from the Date-object.

getHours

public static int getHours(java.util.Date date)
The Method from the Date.getHours is deprecated. This is a helper-Method.

Parameters:
date - The Date-object to get the hours.
Returns:
The hours from the Date-object.

getMinutes

public static int getMinutes(java.util.Date date)
The Method from the Date.getMinutes is deprecated. This is a helper-Method.

Parameters:
date - The Date-object to get the minutes.
Returns:
The minutes from the Date-object.

getMonth

public static int getMonth(java.util.Date date)
The Method from the Date.getMonth is deprecated. This is a helper-Method.

Parameters:
date - The Date-object to get the month.
Returns:
The month from the Date-object.

getSeconds

public static int getSeconds(java.util.Date date)
The Method from the Date.getSeconds is deprecated. This is a helper-Method.

Parameters:
date - The Date-object to get the seconds.
Returns:
The seconds from the Date-object.

getYear

public static int getYear(java.util.Date date)
The Method from the Date.getYear is deprecated. This is a helper-Method.

Parameters:
date - The Date-object to get the year.
Returns:
The year from the Date-object.

isBetween

public static boolean isBetween(java.util.Date start,
                                java.util.Date end,
                                java.util.Date between)
Checks if the Date object "between" is between from the given to Date objects.

Parameters:
start - the start time.
end - the end time.
between - the date to compare if it is between.
Returns:
true, if is between otherwise false.

isDateInTheFuture

public static boolean isDateInTheFuture(java.util.Date date)
Checks if the given date object is in the future.

Parameters:
date - The date to check.
Returns:
Returns true if the given date is in the future otherwise false.

isDateInThePast

public static boolean isDateInThePast(java.util.Date date)
Checks if the given date object is in the past.

Parameters:
date - The date to check.
Returns:
Returns true if the given date is in the past otherwise false.

isLeapYear

public static boolean isLeapYear(int year)
Determines if the given year is a leap year. Wrappes the method isLeapYear(int) from the GregorianCalendar.

Parameters:
year - The year to determine.
Returns:
True if the year is a leap year otherwise false.

isValidDate

public static boolean isValidDate(java.lang.String date,
                                  java.lang.String format,
                                  boolean lenient)
Checks if the Date is valid to convert.

Parameters:
date - The Date as String
format - The Format for the Date to parse
lenient - Specify whether or not date/time interpretation is to be lenient.
Returns:
True if the Date is valid otherwise false.

parseDate

public static java.util.Date parseDate(java.lang.String date,
                                       java.util.List<java.lang.String> patterns)
Tries to convert the given String to a Date.

Parameters:
date - The date to convert as String.
patterns - The date patterns to convert the String to a date-object.
Returns:
Gives a Date if the convertion was successfull otherwise false.

parseToDate

public static java.util.Date parseToDate(java.lang.String date,
                                         java.lang.String format)
Parses the String date to a date object. For example: USA-Format is : yyyy-MM-dd

Parameters:
date - The Date as String
format - The Format for the Date to parse
Returns:
The formated Date
Throws:
java.text.ParseException - occurs when their are problems with parsing the String to Date.

parseToDate

public static java.util.Date parseToDate(java.lang.String datum,
                                         java.lang.String[] formats,
                                         java.util.Locale locale)
Returns a date-object if the array with the formats are valid otherwise null.

Parameters:
datum - The date as string which to parse to a date-object.
formats - The string-array with the date-patterns.
locale - THe Locale for the SimpleDateFormat.
Returns:
A date-object if the array with the formats are valid otherwise null.

parseToDateLenient

public static java.util.Date parseToDateLenient(java.lang.String date,
                                                java.lang.String format,
                                                boolean lenient)
Parses the String date to a date object. For example: USA-Format is : yyyy-MM-dd

Parameters:
date - The Date as String
format - The Format for the Date to parse
lenient - Specify whether or not date/time interpretation is to be lenient.
Returns:
The formated Date or null.

parseToString

public static java.lang.String parseToString(java.util.Date date,
                                             java.lang.String format)
The Method parseToString(Date, String) formats the given Date to the given Format. For Example: Date date =new Date(System.currentTimeMillis()); String format = "dd.MM.yyyy HH:mm:ss"; String now = UtilDate.parseToString(date, format); System.out.println(now); The output would be something like this:'15.07.2005 14:12:00'

Parameters:
date - The Date to format to a String
format - The Format for the date
Returns:
The formated String

substractDaysFromDate

public static java.util.Date substractDaysFromDate(java.util.Date date,
                                                   int substractDays)
Substract days to the given Date object and returns it.

Parameters:
date - The Date object to substract the days.
substractDays - The days to substract.
Returns:
The resulted Date object.

convertDateToCalendar

public java.util.Calendar convertDateToCalendar(java.util.Date date)
Converts the given Date object to a Calendar object.

Parameters:
date - The Date object to convert.
Returns:
The Calendar object.


Copyright © 2010. All Rights Reserved.