Package org.kissweb
Class DateUtils
java.lang.Object
org.kissweb.DateUtils
This class deals with dates in several formats including int, Date, Calendar, and LocalDate.
All combinations of converting from one type to another are included. In order to reduce the number of methods, most methods deal with the
int dates are formatted as YYYYMMDD. For example, June 8, 2018 would
be represented as 20180608.
All combinations of converting from one type to another are included. In order to reduce the number of methods, most methods deal with the
int date type. Given
the conversion methods, it is possible to perform any function on any date type.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intaddDays(int dt, int n) Add N days to a date.static intaddMonths(int dt, int n) Add N months to a date.static intaddYears(int dt, int n) Add N years to a date.static intcalendar(long d) Convert a julian date back into a standard date.static intday(int dt) Returns day portion of a date.static intdayOfWeek(int dt) Returns the day of week number as follows:
0 = Sunday
1 = Monday
2 = Tuesday
3 = Wednesday
4 = Thursday
5 = Friday
6 = Saturday
-1 = Invalid datestatic StringdayOfWeekName(int dt) Returns the full name of the week day.static StringdayOfWeekName(Date dt) Returns the full name of the week day.static longdaysBetween(int dat1, int dat2) Return the number of days between two dates.static StringFormats a date to a string according to a format specification.static Stringformat2(int dt) Format a date with a two digit year as "m/d/yy"static Stringformat4(int dt) Format a date with a four digit year as "m/d/yyyy"static StringformatLong(int dt) Format date as "Jan 3, 2018"static StringformatLongWithWeekDay(int dt) Format date as "Wed, Jan 3, 2018"static StringformatSQL(int dt) Format a date as "YYYY-MM-DD"static longjulian(int dt) Convert a date into the number of days since a certain date (julian date)static voidstatic intmonth(int dt) Returns month portion of a date.static StringmonthOfYearName(int dt) Returns the full name of the month of the year.static intParses a string containing a date formatted as follows into an integer representation of that date.static CalendartoCalendar(int dat) convert from anintdate to aCalendardate.static CalendartoCalendar(int y, int m, int d) Create aCalendarfrom a year, month, and day.static CalendartoCalendar(LocalDate dt) Convert from aLocalDatedate to aCalendardate.static CalendartoCalendar(Date date) Convert from aDatedate to aCalendardate.static DatetoDate(int dat) convert from anintdate to aDatedate.static DatetoDate(int y, int m, int d) Create aDatefrom a year, month, and day.static Dateconvert from anLocalDatedate to aDatedate.static Dateconvert from anCalendardate to aDatedate.static inttoday()Returns the current date as an integer formatted as YYYYMMDD.static intReturns the current date in the specified time zone as an integer formatted as YYYYMMDD.static CalendarReturn today's date as aCalendarinstance.static DateReturn today's date as aDateinstance.static LocalDateReturn today's date as aLocalDateinstance.static inttoInt(int y, int m, int d) Create anintdate from a year, month, and day.static intconvert from aLocalDateinstance to anintdate.static intconvert from aCalendarinstance to anintdate.static intconvert from aDateinstance to anintdate.static LocalDatetoLocalDate(int dt) convert from anintdate to aLocalDatedate.static LocalDatetoLocalDate(int y, int m, int d) Create aLocalDatefrom a year, month, and day.static LocalDatetoLocalDate(Calendar dt) Convert from aCalendardate to aLocalDatestatic LocalDatetoLocalDate(Date date) Convert from aDatedate to aLocalDatedate.static intyear(int dt) Returns year portion of a date.
-
Constructor Details
-
DateUtils
public DateUtils()
-
-
Method Details
-
today
public static int today()Returns the current date as an integer formatted as YYYYMMDD.- Returns:
-
today
Returns the current date in the specified time zone as an integer formatted as YYYYMMDD.- Parameters:
timeZoneId- - the time zone to use when computing the current date- Returns:
- the current date as an integer formatted as YYYYMMDD in the specified time zone
-
todayDate
Return today's date as aDateinstance.- Returns:
-
todayCalendar
Return today's date as aCalendarinstance.- Returns:
-
todayLocalDate
Return today's date as aLocalDateinstance.- Returns:
-
toInt
public static int toInt(int y, int m, int d) Create anintdate from a year, month, and day.- Parameters:
y-m-d-- Returns:
-
toInt
convert from aDateinstance to anintdate.- Parameters:
dat-- Returns:
-
toInt
convert from aCalendarinstance to anintdate.- Parameters:
date-- Returns:
-
toInt
convert from aLocalDateinstance to anintdate.- Parameters:
dt-- Returns:
-
toDate
Create aDatefrom a year, month, and day.- Parameters:
y-m-d-- Returns:
-
toDate
convert from anintdate to aDatedate.- Parameters:
dat- a date formatted as YYYYMMDD- Returns:
-
toDate
convert from anCalendardate to aDatedate.- Parameters:
cal-- Returns:
-
toDate
convert from anLocalDatedate to aDatedate.- Parameters:
dt-- Returns:
-
toCalendar
Create aCalendarfrom a year, month, and day.- Parameters:
y-m-d-- Returns:
-
toCalendar
convert from anintdate to aCalendardate.- Parameters:
dat-- Returns:
-
toCalendar
Convert from aDatedate to aCalendardate.- Parameters:
date-- Returns:
-
toCalendar
Convert from aLocalDatedate to aCalendardate.- Parameters:
dt-- Returns:
-
toLocalDate
Create aLocalDatefrom a year, month, and day.- Parameters:
y-m-d-- Returns:
-
toLocalDate
convert from anintdate to aLocalDatedate.- Parameters:
dt- a date formatted as YYYYMMDD- Returns:
-
toLocalDate
Convert from aDatedate to aLocalDatedate.- Parameters:
date-- Returns:
-
toLocalDate
Convert from aCalendardate to aLocalDate- Parameters:
dt- a Calendar object- Returns:
-
year
public static int year(int dt) Returns year portion of a date.- Parameters:
dt- YYYYMMDD or YYMMDD- Returns:
- YYYY
-
month
public static int month(int dt) Returns month portion of a date.- Parameters:
dt- YYYYMMDD- Returns:
- MM
-
day
public static int day(int dt) Returns day portion of a date.- Parameters:
dt- YYYYMMDD- Returns:
- DD
-
addDays
public static int addDays(int dt, int n) Add N days to a date.- Parameters:
dt- a date formatted as YYYYMMDDn- the number of days to add, may be negative- Returns:
- See Also:
-
addMonths
public static int addMonths(int dt, int n) Add N months to a date.- Parameters:
dt- a date formatted as YYYYMMDDn-- Returns:
- See Also:
-
addYears
public static int addYears(int dt, int n) Add N years to a date.- Parameters:
dt- a date formatted as YYYYMMDDn-- Returns:
- See Also:
-
format
Formats a date to a string according to a format specification.- Parameters:
fmt- format specificationdt- a date formatted as YYYYMMDD- Returns:
- See Also:
-
format2
Format a date with a two digit year as "m/d/yy"- Parameters:
dt- a date formatted as YYYYMMDD- Returns:
-
format4
Format a date with a four digit year as "m/d/yyyy"- Parameters:
dt- a date formatted as YYYYMMDD- Returns:
-
formatSQL
Format a date as "YYYY-MM-DD"- Parameters:
dt- a date formatted as YYYYMMDD- Returns:
-
formatLong
Format date as "Jan 3, 2018"- Parameters:
dt- a date formatted as YYYYMMDD- Returns:
- See Also:
-
formatLongWithWeekDay
Format date as "Wed, Jan 3, 2018"- Parameters:
dt- a date formatted as YYYYMMDD- Returns:
-
daysBetween
public static long daysBetween(int dat1, int dat2) Return the number of days between two dates. (dat1 - dat2)- Parameters:
dat1-dat2-- Returns:
-
parse
Parses a string containing a date formatted as follows into an integer representation of that date.
MM/DD/YY MM/DD/YYYY MM.DD.YYYY YYYY-MM-DD- Parameters:
date- input date- Returns:
- int date or 0 on error
-
julian
public static long julian(int dt) Convert a date into the number of days since a certain date (julian date)- Parameters:
dt- YYYYMMDD- Returns:
- number of days since some early start date
-
calendar
public static int calendar(long d) Convert a julian date back into a standard date.- Parameters:
d-- Returns:
- YYYYMMDD
-
dayOfWeek
public static int dayOfWeek(int dt) Returns the day of week number as follows:
0 = Sunday
1 = Monday
2 = Tuesday
3 = Wednesday
4 = Thursday
5 = Friday
6 = Saturday
-1 = Invalid date- Parameters:
dt- YYYYMMDD- Returns:
-
dayOfWeekName
Returns the full name of the week day.- Parameters:
dt- YYYYMMDD- Returns:
-
dayOfWeekName
Returns the full name of the week day.- Parameters:
dt-- Returns:
-
monthOfYearName
Returns the full name of the month of the year.- Parameters:
dt- YYYYMMDD- Returns:
-
main
-