Class DateUtil
- java.lang.Object
-
- hu.icellmobilsoft.coffee.tool.utils.date.DateUtil
-
public class DateUtil extends Object
Date util.- Since:
- 1.0.0
- Author:
- imre.scheffer
-
-
Field Summary
Fields Modifier and Type Field Description static StringDEFAULT_FULL_PATTERNConstantDEFAULT_FULL_PATTERN="yyyy-MM-dd'T'HH:mm:ss.SSSZ"
-
Constructor Summary
Constructors Constructor Description DateUtil()Default constructor, constructs a new object.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static DateaddValueToDate(Date date, int value, int measureUnit)static CalendarclearTimePart(Calendar cal)Returns givenCalendarwithout hour, minute, second and millis parts.static DateclearTimePart(Date date)Returns givenDatewithout time part.static longdaysBetween(Date dateFrom, Date dateTo)Returns the number of calendar days between 2 dates.static DateendTimeOfDate(Date day)Sets givenDate's time part to end of day (23:59:59:999) then returns it.static ZonedDateTimeendZonedDateTime(YearMonth yearMonth)YearMonthtoZonedDateTimeconverter using end of last day in given month with system default zone idstatic LocalDatelastDayOfMonth(LocalDate baseDate)Returns the last day of the month of the target date.static LocalDatelastDayOfQuarter(LocalDate baseDate)Returns the last day of quarter-year of the target date.static LocalDatelastDayOfYear(LocalDate baseDate)Returns the last day of the target date's year.static longmonthsBetween(Date dateFrom, Date dateTo)Returns the number of months between twoDates.static OffsetDateTimenowUTC()Actual date + time in UTC.static OffsetDateTimenowUTCTruncatedToMillis()Actual date + time in UTC.static Dateparse(String dateString, String pattern)ParsesStringtoDate.static intsecondInDay(Date date)Returns the elapsed number of seconds of aDate.static ZonedDateTimestartZonedDateTime(YearMonth yearMonth)YearMonthtoZonedDateTimeconverter using start of first day in given month with system default zone idstatic CalendartoCalendar(Date date)Creates a new calendar instance from aDate.static DatetoDate(LocalDate localDate)static DatetoDate(LocalDateTime localDateTime)LocalDateTimetoDateconverter with system default zone idstatic DatetoDate(OffsetDateTime offsetDateTime)OffsetDateTimetoDateconverterstatic DatetoDate(ZonedDateTime zonedDateTime)ZonedDateTimetoDateconverterprotected static GregorianCalendartoGregorianCalendar(Calendar c)ConvertCalendartoGregorianCalendarstatic LocalDatetoLocalDate(Date date)static LocalDateTimetoLocalDateTime(Date date)DatetoLocalDateTimeconverter with system default zone idstatic LocalTimetoLocalTime(Date date)static OffsetDateTimetoOffsetDateTime(LocalDateTime localDateTime)LocalDateTime-OffsetDateTimeconverter with system default zone idstatic OffsetDateTimetoOffsetDateTime(Date date)DatetoOffsetDateTimeconverter with system default zone idstatic ZonedDateTimetoZoneDateTime(String stringISODate)java 8ZonedDateTimefrom ISO formatStringdatestatic ZonedDateTimetoZonedDateTime(Calendar cal)static ZonedDateTimetoZonedDateTime(Date date)DatetoZonedDateTimeconverter with system default zone idstatic OffsetDateTimetryToParseToOffsetDateTime(String isoDateTime)Parses a date asDateTimeFormatter.ISO_OFFSET_DATE_TIMEformat.
-
-
-
Field Detail
-
DEFAULT_FULL_PATTERN
public static final String DEFAULT_FULL_PATTERN
ConstantDEFAULT_FULL_PATTERN="yyyy-MM-dd'T'HH:mm:ss.SSSZ"- See Also:
- Constant Field Values
-
-
Method Detail
-
toCalendar
public static Calendar toCalendar(Date date)
Creates a new calendar instance from aDate.- Parameters:
date-Dateto convert- Returns:
Calendaror null ifdateis null
-
addValueToDate
public static Date addValueToDate(Date date, int value, int measureUnit)
- Parameters:
date-Dateto add value tovalue- value to change thedatewithmeasureUnit- unit ofvalue- Returns:
- modified
Dateor null ifdateis null
-
parse
public static Date parse(String dateString, String pattern)
ParsesStringtoDate.- Parameters:
dateString- value to parsepattern- to parse the dateString with- Returns:
- parsed
Dateor null ifdateStringorpatternis null
-
daysBetween
public static long daysBetween(Date dateFrom, Date dateTo)
Returns the number of calendar days between 2 dates. Independent of summer time change. For example: 2020.01.01 23:59:59 - 2020.01.02 00:00:01. The result is 1 day.- Parameters:
dateFrom- date fromdateTo- date to- Returns:
- number of days or 0 if
dateFromordateTois null
-
clearTimePart
public static Date clearTimePart(Date date)
Returns givenDatewithout time part.- Parameters:
date-Dateto clear- Returns:
- cleared
Dateor null ifdateis null
-
clearTimePart
public static Calendar clearTimePart(Calendar cal)
Returns givenCalendarwithout hour, minute, second and millis parts. Does not clear ZONE_OFFSET!- Parameters:
cal-Calendarto clear- Returns:
- cleared
Calendaror null ifcalis null
-
secondInDay
public static int secondInDay(Date date)
Returns the elapsed number of seconds of aDate.- Parameters:
date-Dateto calculate with- Returns:
- elapsed seconds of the
dateor 0 ifdateis null
-
endTimeOfDate
public static Date endTimeOfDate(Date day)
Sets givenDate's time part to end of day (23:59:59:999) then returns it.- Parameters:
day-Dateto calculate with- Returns:
- end time of
dayor null ifdayis null
-
toZoneDateTime
public static ZonedDateTime toZoneDateTime(String stringISODate)
java 8ZonedDateTimefrom ISO formatStringdate- Parameters:
stringISODate- "yyyy-MM-dd\'T\'HH:mm:ss.SSSZ"- Returns:
- parsed
ZonedDateTimeor null if unable to parse
-
toGregorianCalendar
protected static GregorianCalendar toGregorianCalendar(Calendar c)
ConvertCalendartoGregorianCalendar- Parameters:
c-Calendarto convert- Returns:
GregorianCalendarinstance or null ifcis null
-
toZonedDateTime
public static ZonedDateTime toZonedDateTime(Calendar cal)
- Parameters:
cal-Calendarto convert- Returns:
ZonedDateTimeinstance or null ifcalis null
-
toZonedDateTime
public static ZonedDateTime toZonedDateTime(Date date)
DatetoZonedDateTimeconverter with system default zone id- Parameters:
date-Dateto convert- Returns:
ZonedDateTimeinstance or null ifdateis null
-
toOffsetDateTime
public static OffsetDateTime toOffsetDateTime(Date date)
DatetoOffsetDateTimeconverter with system default zone id- Parameters:
date-Dateto convert- Returns:
OffsetDateTimeinstance or null ifdateis null
-
toOffsetDateTime
public static OffsetDateTime toOffsetDateTime(LocalDateTime localDateTime)
LocalDateTime-OffsetDateTimeconverter with system default zone id- Parameters:
localDateTime-LocalDateTimeto convert- Returns:
OffsetDateTimethe offset date time in UTC
-
toLocalDate
public static LocalDate toLocalDate(Date date)
- Parameters:
date-Dateto convert- Returns:
LocalDateinstance or null ifdateis null
-
toLocalTime
public static LocalTime toLocalTime(Date date)
- Parameters:
date-Dateto convert- Returns:
LocalTimeinstance or null ifdateis null
-
monthsBetween
public static long monthsBetween(Date dateFrom, Date dateTo)
Returns the number of months between twoDates. The "from" month is inclusive, the "to" is exclusive.- Parameters:
dateFrom- fromDate, inclusivedateTo- toDate, exclusive- Returns:
- Number of months between
dateFromanddateToor null ifdateFromordateTois null
-
toDate
public static Date toDate(ZonedDateTime zonedDateTime)
ZonedDateTimetoDateconverter- Parameters:
zonedDateTime-ZonedDateTimeto convert- Returns:
Dateinstance or null ifzonedDateTimeis null
-
toDate
public static Date toDate(OffsetDateTime offsetDateTime)
OffsetDateTimetoDateconverter- Parameters:
offsetDateTime-OffsetDateTimeto convert- Returns:
Dateinstance or null ifoffsetDateTimeis null
-
startZonedDateTime
public static ZonedDateTime startZonedDateTime(YearMonth yearMonth)
YearMonthtoZonedDateTimeconverter using start of first day in given month with system default zone id- Parameters:
yearMonth-YearMonthto convert- Returns:
ZonedDateTimeinstance with start of first day or null ifyearMonthis null
-
endZonedDateTime
public static ZonedDateTime endZonedDateTime(YearMonth yearMonth)
YearMonthtoZonedDateTimeconverter using end of last day in given month with system default zone id- Parameters:
yearMonth-YearMonthto convert- Returns:
ZonedDateTimeinstance with end of last day or null ifyearMonthis null
-
toLocalDateTime
public static LocalDateTime toLocalDateTime(Date date)
DatetoLocalDateTimeconverter with system default zone id- Parameters:
date-Dateto convert- Returns:
LocalDateTimeinstance or null ifdateis null
-
toDate
public static Date toDate(LocalDateTime localDateTime)
LocalDateTimetoDateconverter with system default zone id- Parameters:
localDateTime-LocalDateTimeto convert- Returns:
Dateinstance or null iflocalDateTimeis null
-
toDate
public static Date toDate(LocalDate localDate)
- Parameters:
localDate- localDate to convert- Returns:
Dateinstance or null iflocalDateis empty
-
nowUTC
public static OffsetDateTime nowUTC()
Actual date + time in UTC. When it is serialized, the local 2020-08-26 10:34:28 "2020-08-26T08:34:28.955024Z" format takes place instead of the default "2020-08-26T10:34:28.955024+02:00"- Returns:
OffsetDateTime, example: "2020-08-26T08:34:28.955024Z"
-
nowUTCTruncatedToMillis
public static OffsetDateTime nowUTCTruncatedToMillis()
Actual date + time in UTC. When it is serialized, the local 2020-08-26 10:34:28 "2020-08-26T08:34:28.955024Z" format takes place instead of the default "2020-08-26T10:34:28.955024+02:00"- Returns:
OffsetDateTime, example: "2020-08-26T08:34:28.955Z"
-
lastDayOfMonth
public static LocalDate lastDayOfMonth(LocalDate baseDate)
Returns the last day of the month of the target date.- Parameters:
baseDate-LocalDatethe base date- Returns:
LocalDate, the last day of the month of the target date.
-
lastDayOfQuarter
public static LocalDate lastDayOfQuarter(LocalDate baseDate)
Returns the last day of quarter-year of the target date.- Parameters:
baseDate-LocalDatethe base date- Returns:
LocalDate, the last day of quarter-year of the target date.
-
lastDayOfYear
public static LocalDate lastDayOfYear(LocalDate baseDate)
Returns the last day of the target date's year.- Parameters:
baseDate-LocalDatethe base date- Returns:
LocalDatethe last day of the target date's year. (yyyy.12.31)
-
tryToParseToOffsetDateTime
public static OffsetDateTime tryToParseToOffsetDateTime(String isoDateTime) throws hu.icellmobilsoft.coffee.dto.exception.BaseException
Parses a date asDateTimeFormatter.ISO_OFFSET_DATE_TIMEformat. In case of any formatting problem, it throwsBaseException.- Parameters:
isoDateTime- date to be parsedString- Returns:
OffsetDateTimedate time object- Throws:
hu.icellmobilsoft.coffee.dto.exception.BaseException- in case of bad date format
-
-