Package com.storedobject.common
Class DateUtility
java.lang.Object
com.storedobject.common.DateUtility
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <D extends Date>
DaddDay
(D date, int day) Adds a particular number of days to a specified datestatic <D extends Date>
DaddMonth
(D date, int month) Adds a particular number of months to a specified datestatic <D extends Date>
DaddYear
(D date, int year) Adds a particular number of year to a specified datestatic <D extends Date>
Dclone
(D date) Duplicates the date object passedstatic <D extends Date>
intcompareTo
(D one, D two) Compare two datesstatic Date
create()
Create and return Date instance for the current datestatic Date
create
(int dateValue) Get date corresponding to a particular date value in a yearstatic Date
create
(int year, int month, int day) Create a Date instance for the specified day, month and yearcreate
(D date) Get an java.sql.Date corresponding to a given java.util.Datestatic Date
Get date by passing date string separated by comma or hyphen (, or -) in year, month and date formatstatic Date
Get java.sql.Date by passing java.time.LocalDatestatic Date
Create a Date instance by passing a Calendar instancestatic Time
createTime
(LocalDateTime date) Get time by passing LocalDateTimestatic Time
createTime
(Calendar calendar) Get the Time value corresponding to a Calendar instancestatic Time
createTime
(Date date) Get the Time value corresponding to a java.util.Date instancestatic Timestamp
createTimestamp
(long time) static Timestamp
createTimestamp
(LocalDateTime date) Get TimeStamp by passing LocalDateTimestatic Timestamp
createTimestamp
(Date date) static DateFormat
Returns a clone of the Java DateFormat instance.static DateFormat
Returns a clone of the default date and time format.difference
(D from, int days) Get the difference between two dates in-terms of year, month and date by passing a date and an offsetdifference
(D one, D two) Get the difference between two dates in-terms of year, month and datestatic Date
Get end date of current month.static Date
endOfMonth
(int monthOffset) Get end date of a month.static <D extends Date>
DendOfMonth
(int monthOffset, D date) Get end date of a month.static <D extends Date>
DendOfMonth
(D date) Get end date of month for a give datestatic Timestamp
Gets the timestamp for the current date with the time set to end time of the daystatic Date
Get end date of current yearstatic <D extends Date>
DendOfYear
(D date) Get end date of year for a give datestatic Timestamp
Gets the timestamp for the previous date with the time set to end time of the daystatic Timestamp
Gets the timestamp for the date provided and the time set to end time of that daystatic <D extends Date>
booleanequals
(D one, D two) Check whether two dates are sameformat
(D date) Formats the given dateformatDate
(D date) Formats a given date in sql date formatstatic String
formatHHMM
(long time) formatHHMM
(D date) formatLongDate
(D date) Formats a given date and timeformatMonth
(D date) formatShortDate
(D date) Formats a given date in MMM dd, yy formatformatTime
(D date) formatTinyDate
(D date) Formats a given date in MM yy formatformatWithTime
(D date) Formats the given dateformatWithTimeHHMM
(D date) Formats the given datestatic <D extends Date>
DConverts the given date from GMT to the specified time zone.static <D extends Date>
DConverts the given date from GMT (Greenwich Mean Time) to a specified time zone.static <D extends Date>
DfromGMT
(D dateGMT, ZoneOffset toZoneOffset) Translates a given date from GMT to a specified time zone.static <D extends Date>
DConverts a given date from GMT (Greenwich Mean Time) to the specified time zone.static <D extends Date>
intget
(D date, int field) static int
getDay()
static <D extends Date>
intgetDay
(D date) static <D extends Date>
intgetDayOfYear
(D date) Get the day of the year.static <D extends Date>
intgetHourOfYear
(D date) Get the hour of the year.static int
getMonth()
static <D extends Date>
intgetMonth
(D date) static int
static String
getMonthName
(int m) getMonthName
(D date) static String[]
Returns an array of month names.static int
static <D extends Date>
intgetMonthZeroBased
(D date) static <D extends Date>
intgetPeriodInDays
(D one, D two) Get difference in days between two datesstatic <D extends Date>
intgetPeriodInDays
(D one, D two, boolean considerTime) Get difference in days between two dates by considering the time as wellstatic <D extends Date>
intgetPeriodInMonths
(D one, D two) Get difference in months between two datesstatic String[]
Retrieves the names of the days in a week.static <D extends Date>
intgetWeekOfYear
(D date) Get the week of the year (1 - 53/54).static int
getYear()
static <D extends Date>
intgetYear
(D date) Gets the year value for a particular datestatic boolean
isLeapYear
(int year) Checks if a particular year specified is leap yearstatic <D extends Date>
booleanisLeapYear
(D date) Checks if a particular year is leap year by taking in a datestatic <D extends Date>
booleanisSameDate
(D one, D two) Validates and returns true if two dates passed to the method are samelocal
(D date) Get java.time.LocalDate by passing a Datestatic <D extends Date>
LocalDateTimelocalTime
(D date) Get java.time.LocalDateTime by passing a Datestatic Date
Returns the maximum possible date value.static Timestamp
now()
Get the current timestampstatic <D extends Date>
DsetDay
(D date, int day) static <D extends Date>
DsetMonth
(D date, int month) Sets the month of this date to the specified value.static <D extends Date>
DsetYear
(D date, int year) static Date
Get start date of the current monthstatic Date
startOfMonth
(int monthOffset) Get start date of a month.static <D extends Date>
DstartOfMonth
(int monthOffset, D date) Get starting date of a month.static <D extends Date>
DstartOfMonth
(D date) Get start date of month for a give datestatic Timestamp
Gets the timestamp for the current date with the time set to start time of the daystatic Timestamp
Gets the timestamp for the next date with the time set to start time of the daystatic Date
Get start date of the current yearstatic <D extends Date>
DstartOfYear
(D date) Get start date of year for a given datestatic Timestamp
Gets a timestamp for the date provided and the time set to start time of that daystatic Time
time()
Gets the current date and time valuestatic DateFormat
Retrieves a clone of the time format used by the application.static DateFormat
Returns a cloned instance of the default 24-hour time format.static Date
today()
Get the current datestatic <D extends Date>
DConverts a given date to Greenwich Mean Time (GMT) based on the provided time zone.static <D extends Date>
DConverts a given date to the GMT timezone.static <D extends Date>
DtoGMT
(D date, ZoneOffset fromZoneOffset) Converts the given date to GMT (Greenwich Mean Time) by subtracting the offset caused by the provided ZoneOffset.static <D extends Date>
DConverts the given date to GMT (Greenwich Mean Time) by converting its time zone to GMT.static Date
tomorrow()
Get one day after the current datestatic Time
static Timestamp
static Time
trimMillis
(Time time) static Timestamp
trimMillis
(Timestamp time) static Time
trimMinutes
(Time time) static Timestamp
trimMinutes
(Timestamp time) static Time
trimSeconds
(Time time) static Timestamp
trimSeconds
(Timestamp time) static Date
Get one day prior to current date
-
Constructor Details
-
DateUtility
public DateUtility()
-
-
Method Details
-
today
-
tomorrow
Get one day after the current date- Returns:
- One day after current date
-
yesterday
Get one day prior to current date- Returns:
- One day prior to current date
-
startOfYear
Get start date of the current year- Returns:
- Start date of the current year
-
startOfYear
Get start date of year for a given date- Type Parameters:
D
- Date-type- Parameters:
date
- Date for which the start of the year is to be returned- Returns:
- Start date of year for a give date
-
endOfYear
-
endOfYear
Get end date of year for a give date- Type Parameters:
D
- Date-type- Parameters:
date
- Date for which the end of the year is to be returned- Returns:
- End date of year for a give date
-
startOfMonth
Get start date of the current month- Returns:
- Start date of current month
-
startOfMonth
Get start date of month for a give date- Type Parameters:
D
- Date-type- Parameters:
date
- Date for which the start of the month is to be returned- Returns:
- Start date of month for a give date
-
startOfMonth
Get start date of a month. Month is calculated by adding an offset to the current month- Parameters:
monthOffset
- An integer offset value that has to be added to the current month value- Returns:
- Start date of month
-
startOfMonth
Get starting date of a month.- Type Parameters:
D
- Date-type- Parameters:
monthOffset
- An integer offset value that needs to be added to the month corresponding to a given date.- Returns:
- Start date of month
-
endOfMonth
-
endOfMonth
Get end date of month for a give date- Parameters:
date
- Date for which the end of the month is to be returned- Returns:
- Start date of month for a give date
-
endOfMonth
Get end date of a month. Month is calculated by adding an offset to the current month- Parameters:
monthOffset
- An integer offset value that has to be added to the current month value- Returns:
- Start date of month
-
endOfMonth
Get end date of a month. Month is calculated by adding an offset to the month corresponding to a given date- Type Parameters:
D
- Date-type- Parameters:
monthOffset
- An integer offset value that has to be added to the month corresponding to a given date- Returns:
- End date of month
-
create
-
create
Create and return Date instance for the current date- Returns:
- Current date
-
create
Create a Date instance for the specified day, month and year- Parameters:
year
- Year.month
- Month (1-based, not 0-based).day
- Day.- Returns:
- Date for the specified day, month and year
-
create
-
create
Get date corresponding to a particular date value in a year- Parameters:
dateValue
- an integer representing the date- Returns:
- Date corresponding to the given date value in integer
-
create
-
create
-
createTime
Get time by passing LocalDateTime- Parameters:
date
- LocalDateTime- Returns:
- Time
-
createTimestamp
Get TimeStamp by passing LocalDateTime- Parameters:
date
- LocalDateTime- Returns:
- Timestamp
-
local
-
localTime
Get java.time.LocalDateTime by passing a Date- Type Parameters:
D
- Date-type- Parameters:
date
- Date- Returns:
- date LocalDateTime
-
createTime
-
createTime
-
createTimestamp
-
createTimestamp
-
time
-
trimMillis
-
trimSeconds
-
trimMinutes
-
trimHours
-
now
-
startTime
-
endTime
-
endOfToday
Gets the timestamp for the current date with the time set to end time of the day- Returns:
- timestamp
-
endOfYesterday
Gets the timestamp for the previous date with the time set to end time of the day- Returns:
- timestamp
-
startOfToday
Gets the timestamp for the current date with the time set to start time of the day- Returns:
- timestamp
-
startOfTomorrow
Gets the timestamp for the next date with the time set to start time of the day- Returns:
- Timestamp
-
trimMillis
-
trimSeconds
-
trimMinutes
-
trimHours
-
formatMonth
-
formatDate
-
formatLongDate
-
formatShortDate
-
formatTinyDate
-
format
-
formatWithTime
-
formatWithTimeHHMM
-
formatTime
-
formatHHMM
-
formatHHMM
-
equals
Check whether two dates are same- Type Parameters:
D
- Date-type- Parameters:
one
- first date to comparetwo
- second date to compare- Returns:
- a boolean true if one and two are same, otherwise false
-
compareTo
Compare two dates . *- Type Parameters:
D
- Date-type- Parameters:
one
- Date to comparetwo
- Date to compare- Returns:
- A negative integer, zero, or a positive integer as 'one' is less than, equal to, or greater than 'two'.
-
getMonth
-
getMonthName
-
getMonthName
-
getYear
public static int getYear() -
getYear
Gets the year value for a particular date- Type Parameters:
D
- Date-type- Parameters:
date
- Date- Returns:
- year corresponding to the date passed
-
setYear
-
getWeekOfYear
Get the week of the year (1 - 53/54).- Type Parameters:
D
- Date type.- Parameters:
date
- Date- Returns:
- Week number.
-
getDayOfYear
Get the day of the year.- Type Parameters:
D
- Date type.- Parameters:
date
- Date- Returns:
- Day of the year.
-
getHourOfYear
Get the hour of the year.- Type Parameters:
D
- Date type.- Parameters:
date
- Date- Returns:
- Hour of the year.
-
getMonth
public static int getMonth() -
getMonth
-
getMonthZeroBased
public static int getMonthZeroBased() -
getMonthZeroBased
-
setMonth
Sets the month of this date to the specified value.- Type Parameters:
D
- Date-type- Parameters:
date
- The original datemonth
- the month value that has to be set- Returns:
- Date after setting the month value
-
getDay
public static int getDay() -
getDay
-
setDay
-
addYear
Adds a particular number of year to a specified date- Type Parameters:
D
- Date-type- Parameters:
date
- The date to which the years are addedyear
- The number of year to add to the date passed- Returns:
- The date after adding the year offset
-
addMonth
Adds a particular number of months to a specified date- Parameters:
date
- The date to which the months are addedmonth
- The number of months to add to the date passed- Returns:
- The date after adding the month offset
-
addDay
Adds a particular number of days to a specified date- Type Parameters:
D
- Date-type- Parameters:
date
- The date to which the days are addedday
- The number of days to add to the date passed- Returns:
- The date after adding the days offset
-
clone
Duplicates the date object passed- Type Parameters:
D
- Date-type- Parameters:
date
- Date- Returns:
- A duplicate of the date passed
-
isLeapYear
public static boolean isLeapYear(int year) Checks if a particular year specified is leap year- Parameters:
year
- Year- Returns:
- true if year passed to the method is leap year
-
isLeapYear
Checks if a particular year is leap year by taking in a date- Type Parameters:
D
- Date-type- Parameters:
date
- Date- Returns:
- true if year in the date passed to the method is leap year
-
getPeriodInDays
Get difference in days between two dates- Type Parameters:
D
- Date-type- Parameters:
one
- Date that has to be subtracted from the greater datetwo
- The greater date from which the other date is subtracted- Returns:
- The difference between date two and date one in days
-
getPeriodInDays
Get difference in days between two dates by considering the time as well- Parameters:
one
- Date that has to be subtracted from the greater datetwo
- The greater date from which the other date is subtracted- Returns:
- The difference between date two and date one in days
-
getPeriodInMonths
Get difference in months between two dates- Parameters:
one
- Date that has to be subtracted from the greater datetwo
- The greater date from which the other date is subtracted- Returns:
- The difference between date two and date one in months
-
get
-
isSameDate
Validates and returns true if two dates passed to the method are same- Type Parameters:
D
- Date-type- Parameters:
one
- First datetwo
- Second date- Returns:
- True if two dates passed to the method are same
-
difference
-
difference
Get the difference between two dates in-terms of year, month and date by passing a date and an offset- Type Parameters:
D
- Date-type- Parameters:
from
- From date for finding the differencedays
- offset added to from date to get to date- Returns:
- Differences between the two dates
-
maximumDate
Returns the maximum possible date value.- Returns:
- the maximum date value available
-
getMonthNames
Returns an array of month names.- Returns:
- an array of Strings representing the names of the months.
-
getWeekNames
Retrieves the names of the days in a week.- Returns:
- a string array containing the names of the days in a week
-
dateFormat
Returns a clone of the Java DateFormat instance.- Returns:
- a clone of the Java DateFormat instance.
-
dateTimeFormat
Returns a clone of the default date and time format.- Returns:
- a clone of the default date and time format.
-
timeFormat
Retrieves a clone of the time format used by the application.- Returns:
- A clone of the time format.
-
timeFormat24
Returns a cloned instance of the default 24-hour time format. The returned DateFormat object can be used to format time values in the 24-hour format.- Returns:
- a cloned instance of the default 24-hour time format
-
toGMT
Converts a given date to Greenwich Mean Time (GMT) based on the provided time zone.- Parameters:
date
- the date to be converted to GMTfromTimeZone
- the time zone of the given date, represented as a string- Returns:
- the converted date in GMT
-
toGMT
Converts the given date to GMT (Greenwich Mean Time) by converting its time zone to GMT.- Parameters:
date
- The date to be converted to GMT.fromTimeZone
- The time zone of the given date. If null, the default time zone of the system will be used.- Returns:
- The converted date in GMT.
-
toGMT
-
toGMT
Converts the given date to GMT (Greenwich Mean Time) by subtracting the offset caused by the provided ZoneOffset.- Type Parameters:
D
- the type of date to convert, must extend java.util.Date- Parameters:
date
- the date to convert to GMTfromZoneOffset
- the ZoneOffset that represents the offset from GMT- Returns:
- the converted date in GMT
-
fromGMT
Converts the given date from GMT to the specified time zone.- Parameters:
date
- The date object to convert from GMT to the specified time zone. Cannot be null.toTimeZone
- The target time zone to convert the date to. Can be null.- Returns:
- The converted date object in the specified time zone, or the original date object if toTimeZone is null.
-
fromGMT
Converts a given date from GMT (Greenwich Mean Time) to the specified time zone.- Parameters:
date
- the date to be converted from GMTtoTimeZone
- the target time zone to convert the date to, can be null- Returns:
- the converted date in the specified time zone
-
fromGMT
Converts the given date from GMT (Greenwich Mean Time) to a specified time zone.- Type Parameters:
D
- the type of the date object- Parameters:
date
- the date to be convertedtoZoneId
- the target time zone ID to convert the date to- Returns:
- the converted date in the specified time zone
-
fromGMT
Translates a given date from GMT to a specified time zone.- Parameters:
dateGMT
- the date to be translated from GMT to the specified time zone.toZoneOffset
- the time zone offset to be applied to the date. If null, the date will not be modified.- Returns:
- the translated date in the specified time zone, or the original date if no offset is provided.
-