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
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
addDays
(int dt, int n) Add N days to a date.static int
addMonths
(int dt, int n) Add N months to a date.static int
addYears
(int dt, int n) Add N years to a date.static int
calendar
(long d) Convert a julian date back into a standard date.static int
day
(int dt) Returns day portion of a date.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 datestatic String
dayOfWeekName
(int dt) Returns the full name of the week day.static String
dayOfWeekName
(Date dt) Returns the full name of the week day.static long
daysBetween
(int dat1, int dat2) Return the number of days between two dates.static String
Formats a date to a string according to a format specification.static String
format2
(int dt) Format a date with a two digit year as "m/d/yy"static String
format4
(int dt) Format a date with a four digit year as "m/d/yyyy"static String
formatLong
(int dt) Format date as "Jan 3, 2018"static String
formatLongWithWeekDay
(int dt) Format date as "Wed, Jan 3, 2018"static String
formatSQL
(int dt) Format a date as "YYYY-MM-DD"static long
julian
(int dt) Convert a date into the number of days since a certain date (julian date)static void
static int
month
(int dt) Returns month portion of a date.static String
monthOfYearName
(int dt) Returns the full name of the month of the year.static int
Parses a string containing a date formatted as follows into an integer representation of that date.static Calendar
toCalendar
(int dat) convert from anint
date to aCalendar
date.static Calendar
toCalendar
(int y, int m, int d) Create aCalendar
from a year, month, and day.static Calendar
toCalendar
(LocalDate dt) Convert from aLocalDate
date to aCalendar
date.static Calendar
toCalendar
(Date date) Convert from aDate
date to aCalendar
date.static Date
toDate
(int dat) convert from anint
date to aDate
date.static Date
toDate
(int y, int m, int d) Create aDate
from a year, month, and day.static Date
convert from anLocalDate
date to aDate
date.static Date
convert from anCalendar
date to aDate
date.static int
today()
Returns the current date as an integer formatted as YYYYMMDD.static int
Returns the current date in the specified time zone as an integer formatted as YYYYMMDD.static Calendar
Return today's date as aCalendar
instance.static Date
Return today's date as aDate
instance.static LocalDate
Return today's date as aLocalDate
instance.static int
toInt
(int y, int m, int d) Create anint
date from a year, month, and day.static int
convert from aLocalDate
instance to anint
date.static int
convert from aCalendar
instance to anint
date.static int
convert from aDate
instance to anint
date.static LocalDate
toLocalDate
(int dt) convert from anint
date to aLocalDate
date.static LocalDate
toLocalDate
(int y, int m, int d) Create aLocalDate
from a year, month, and day.static LocalDate
toLocalDate
(Calendar dt) Convert from aCalendar
date to aLocalDate
static LocalDate
toLocalDate
(Date date) Convert from aDate
date to aLocalDate
date.static int
year
(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 aDate
instance.- Returns:
-
todayCalendar
Return today's date as aCalendar
instance.- Returns:
-
todayLocalDate
Return today's date as aLocalDate
instance.- Returns:
-
toInt
public static int toInt(int y, int m, int d) Create anint
date from a year, month, and day.- Parameters:
y
-m
-d
-- Returns:
-
toInt
convert from aDate
instance to anint
date.- Parameters:
dat
-- Returns:
-
toInt
convert from aCalendar
instance to anint
date.- Parameters:
date
-- Returns:
-
toInt
convert from aLocalDate
instance to anint
date.- Parameters:
dt
-- Returns:
-
toDate
Create aDate
from a year, month, and day.- Parameters:
y
-m
-d
-- Returns:
-
toDate
convert from anint
date to aDate
date.- Parameters:
dat
- a date formatted as YYYYMMDD- Returns:
-
toDate
convert from anCalendar
date to aDate
date.- Parameters:
cal
-- Returns:
-
toDate
convert from anLocalDate
date to aDate
date.- Parameters:
dt
-- Returns:
-
toCalendar
Create aCalendar
from a year, month, and day.- Parameters:
y
-m
-d
-- Returns:
-
toCalendar
convert from anint
date to aCalendar
date.- Parameters:
dat
-- Returns:
-
toCalendar
Convert from aDate
date to aCalendar
date.- Parameters:
date
-- Returns:
-
toCalendar
Convert from aLocalDate
date to aCalendar
date.- Parameters:
dt
-- Returns:
-
toLocalDate
Create aLocalDate
from a year, month, and day.- Parameters:
y
-m
-d
-- Returns:
-
toLocalDate
convert from anint
date to aLocalDate
date.- Parameters:
dt
- a date formatted as YYYYMMDD- Returns:
-
toLocalDate
Convert from aDate
date to aLocalDate
date.- Parameters:
date
-- Returns:
-
toLocalDate
Convert from aCalendar
date 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
-