Class DateUtil


  • public class DateUtil
    extends Object
    Date util.
    Since:
    1.0.0
    Author:
    imre.scheffer
    • Field Detail

      • DEFAULT_FULL_PATTERN

        public static final String DEFAULT_FULL_PATTERN
        Constant DEFAULT_FULL_PATTERN="yyyy-MM-dd'T'HH:mm:ss.SSSZ"
        See Also:
        Constant Field Values
    • Constructor Detail

      • DateUtil

        public DateUtil()
        Default constructor, constructs a new object.
    • Method Detail

      • toCalendar

        public static Calendar toCalendar​(Date date)
        Creates a new calendar instance from a Date.
        Parameters:
        date - Date to convert
        Returns:
        Calendar or null if date is null
      • addValueToDate

        public static Date addValueToDate​(Date date,
                                          int value,
                                          int measureUnit)
        Converts to Calendar, then changes value and converts back to Date.
        Parameters:
        date - Date to add value to
        value - value to change the date with
        measureUnit - unit of value
        Returns:
        modified Date or null if date is null
      • parse

        public static Date parse​(String dateString,
                                 String pattern)
        Parses String to Date.
        Parameters:
        dateString - value to parse
        pattern - to parse the dateString with
        Returns:
        parsed Date or null if dateString or pattern is 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 from
        dateTo - date to
        Returns:
        number of days or 0 if dateFrom or dateTo is null
      • clearTimePart

        public static Date clearTimePart​(Date date)
        Returns given Date without time part.
        Parameters:
        date - Date to clear
        Returns:
        cleared Date or null if date is null
      • clearTimePart

        public static Calendar clearTimePart​(Calendar cal)
        Returns given Calendar without hour, minute, second and millis parts. Does not clear ZONE_OFFSET!
        Parameters:
        cal - Calendar to clear
        Returns:
        cleared Calendar or null if cal is null
      • secondInDay

        public static int secondInDay​(Date date)
        Returns the elapsed number of seconds of a Date.
        Parameters:
        date - Date to calculate with
        Returns:
        elapsed seconds of the date or 0 if date is null
      • endTimeOfDate

        public static Date endTimeOfDate​(Date day)
        Sets given Date's time part to end of day (23:59:59:999) then returns it.
        Parameters:
        day - Date to calculate with
        Returns:
        end time of day or null if day is null
      • toZonedDateTime

        public static ZonedDateTime toZonedDateTime​(Calendar cal)
        Calendar to ZonedDateTime converter Calendar belso idozona szerint (classic)
        Parameters:
        cal - Calendar to convert
        Returns:
        ZonedDateTime instance or null if cal is null
      • toZonedDateTime

        public static ZonedDateTime toZonedDateTime​(Date date)
        Date to ZonedDateTime converter with system default zone id
        Parameters:
        date - Date to convert
        Returns:
        ZonedDateTime instance or null if date is null
      • toOffsetDateTime

        public static OffsetDateTime toOffsetDateTime​(Date date)
        Date to OffsetDateTime converter with system default zone id
        Parameters:
        date - Date to convert
        Returns:
        OffsetDateTime instance or null if date is null
      • toOffsetDateTime

        public static OffsetDateTime toOffsetDateTime​(LocalDateTime localDateTime)
        LocalDateTime - OffsetDateTime converter with system default zone id
        Parameters:
        localDateTime - LocalDateTime to convert
        Returns:
        OffsetDateTime the offset date time in UTC
      • toLocalDate

        public static LocalDate toLocalDate​(Date date)
        Date to LocalDate converter with system default zone id
        Parameters:
        date - Date to convert
        Returns:
        LocalDate instance or null if date is null
      • toLocalTime

        public static LocalTime toLocalTime​(Date date)
        Date to LocalTime converter with system default zone id
        Parameters:
        date - Date to convert
        Returns:
        LocalTime instance or null if date is null
      • monthsBetween

        public static long monthsBetween​(Date dateFrom,
                                         Date dateTo)
        Returns the number of months between two Dates. The "from" month is inclusive, the "to" is exclusive.
        Parameters:
        dateFrom - from Date, inclusive
        dateTo - to Date, exclusive
        Returns:
        Number of months between dateFrom and dateTo or null if dateFrom or dateTo is null
      • toDate

        public static Date toDate​(ZonedDateTime zonedDateTime)
        ZonedDateTime to Date converter
        Parameters:
        zonedDateTime - ZonedDateTime to convert
        Returns:
        Date instance or null if zonedDateTime is null
      • toDate

        public static Date toDate​(OffsetDateTime offsetDateTime)
        OffsetDateTime to Date converter
        Parameters:
        offsetDateTime - OffsetDateTime to convert
        Returns:
        Date instance or null if offsetDateTime is null
      • startZonedDateTime

        public static ZonedDateTime startZonedDateTime​(YearMonth yearMonth)
        YearMonth to ZonedDateTime converter using start of first day in given month with system default zone id
        Parameters:
        yearMonth - YearMonth to convert
        Returns:
        ZonedDateTime instance with start of first day or null if yearMonth is null
      • endZonedDateTime

        public static ZonedDateTime endZonedDateTime​(YearMonth yearMonth)
        YearMonth to ZonedDateTime converter using end of last day in given month with system default zone id
        Parameters:
        yearMonth - YearMonth to convert
        Returns:
        ZonedDateTime instance with end of last day or null if yearMonth is null
      • toLocalDateTime

        public static LocalDateTime toLocalDateTime​(Date date)
        Date to LocalDateTime converter with system default zone id
        Parameters:
        date - Date to convert
        Returns:
        LocalDateTime instance or null if date is null
      • toDate

        public static Date toDate​(LocalDateTime localDateTime)
        LocalDateTime to Date converter with system default zone id
        Parameters:
        localDateTime - LocalDateTime to convert
        Returns:
        Date instance or null if localDateTime is null
      • toDate

        public static Date toDate​(LocalDate localDate)
        Returns a Date instance created from LocalDate with system default zone id
        Parameters:
        localDate - localDate to convert
        Returns:
        Date instance or null if localDate is 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 - LocalDate the 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 - LocalDate the 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 - LocalDate the base date
        Returns:
        LocalDate the 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 as DateTimeFormatter.ISO_OFFSET_DATE_TIME format. In case of any formatting problem, it throws BaseException.
        Parameters:
        isoDateTime - date to be parsed String
        Returns:
        OffsetDateTime date time object
        Throws:
        hu.icellmobilsoft.coffee.dto.exception.BaseException - in case of bad date format