Class CalendarUtils


  • public final class CalendarUtils
    extends Object
    This class provides the calendar utilities for MOSIP implementations.
    Since:
    version 1.0.0
    Author:
    Sagar Mahapatra
    • Method Detail

      • getCeiling

        public static Calendar getCeiling​(Calendar calendar,
                                          int field)
        Gets a date ceiling, leaving the field specified as the most significant field.
        Parameters:
        calendar - the date to work with, not null
        field - the field from
        Returns:
        the different ceil date, not null
        Throws:
        IllegalArgumentException - if the date is null
        ArithmeticException - if the year is over 280 million
      • getFragmentInDays

        public static long getFragmentInDays​(Calendar calendar,
                                             int fragment)
        Returns the number of days within the fragment. All datefields greater than the fragment will be ignored.
        Parameters:
        calendar - the calendar to work with, not null
        fragment - the field part of calendar to calculate
        Returns:
        number of days within the fragment of date
        Throws:
        IllegalArgumentException - if the date is null or fragment is not supported
      • getFragmentInHours

        public static long getFragmentInHours​(Calendar calendar,
                                              int fragment)
        Returns the number of hours within the fragment. All datefields greater than the fragment will be ignored.
        Parameters:
        calendar - the calendar to work with, not null
        fragment - the field part of calendar to calculate
        Returns:
        number of hours within the fragment of date
        Throws:
        IllegalArgumentException - if the date is null or fragment is not supported
        Since:
        2.4
      • getFragmentInMilliseconds

        public static long getFragmentInMilliseconds​(Calendar calendar,
                                                     int fragment)
        Returns the number of milliseconds within the fragment. All datefields greater than the fragment will be ignored.
        Parameters:
        calendar - the calendar to work with, not null
        fragment - the field part of calendar to calculate
        Returns:
        number of milliseconds within the fragment of date
        Throws:
        IllegalArgumentException - if the date is null or fragment is not supported
      • getFragmentInMinutes

        public static long getFragmentInMinutes​(Calendar calendar,
                                                int fragment)
        Returns the number of minutes within the fragment. All datefields greater than the fragment will be ignored.
        Parameters:
        calendar - the calendar to work with, not null
        fragment - the field part of calendar to calculate
        Returns:
        number of minutes within the fragment of date
        Throws:
        IllegalArgumentException - if the date is null or fragment is not supported
      • getFragmentInSeconds

        public static long getFragmentInSeconds​(Calendar calendar,
                                                int fragment)
        Returns the number of seconds within the fragment. All datefields greater than the fragment will be ignored.
        Parameters:
        calendar - the calendar to work with, not null
        fragment - the field part of calendar to calculate
        Returns:
        number of seconds within the fragment of date
        Throws:
        IllegalArgumentException - if the date is null or fragment is not supported
      • isSameDay

        public static boolean isSameDay​(Calendar cal1,
                                        Calendar cal2)
        Checks if two calendar objects are on the same day ignoring time.
        Parameters:
        cal1 - the first calendar, not altered, not null
        cal2 - the second calendar, not altered, not null
        Returns:
        true if they represent the same day
        Throws:
        IllegalArgumentException - if either calendar is null
      • isSameInstant

        public static boolean isSameInstant​(Calendar cal1,
                                            Calendar cal2)
        Checks if two calendar objects represent the same instant in time. This method compares the long millisecond time of the two objects.
        Parameters:
        cal1 - the first calendar, not altered, not null
        cal2 - the second calendar, not altered, not null
        Returns:
        true if they represent the same millisecond instant
        Throws:
        IllegalArgumentException - if either date is null
      • isSameLocalTime

        public static boolean isSameLocalTime​(Calendar cal1,
                                              Calendar cal2)
        Checks if two calendar objects represent the same local time. This method compares the values of the fields of the two objects. In addition, both calendars must be the same of the same type.
        Parameters:
        cal1 - the first calendar, not altered, not null
        cal2 - the second calendar, not altered, not null
        Returns:
        true if they represent the same millisecond instant
        Throws:
        IllegalArgumentException - if either date is null
      • getRound

        public static Calendar getRound​(Calendar calendar,
                                        int field)
        Rounds a date, leaving the field specified as the most significant field.
        Parameters:
        calendar - the date to work with, not null
        field - the field from Calendar
        Returns:
        the different rounded date, not null
        Throws:
        IllegalArgumentException - if the date is null
        ArithmeticException - if the year is over 280 million
      • toCalendar

        public static Calendar toCalendar​(Date date)
        Converts a Date of a given TimeZone into a Calendar.
        Parameters:
        date - the date to convert to a Calendar
        Returns:
        the created Calendar
        Throws:
        NullPointerException - if date is null
      • toCalendar

        public static Calendar toCalendar​(Date date,
                                          TimeZone tz)
        Parameters:
        date - the date to convert to a Calendar
        tz - the time zone of the date
        Returns:
        the created calendar
        Throws:
        NullPointerException - if date or timezone is null
      • truncate

        public static Calendar truncate​(Calendar date,
                                        int field)
        Truncates a date, leaving the field specified as the most significant field.
        Parameters:
        date - the date to work with, not null
        field - the field from Calendar
        Returns:
        the different truncated date, not null
        Throws:
        IllegalArgumentException - if the date is null
        ArithmeticException - if the year is over 280 million
      • truncatedEquals

        public static boolean truncatedEquals​(Calendar cal1,
                                              Calendar cal2,
                                              int field)
        Determines if two calendars are equal up to no more than the specified most significant field.
        Parameters:
        cal1 - the first calendar, not null
        cal2 - the second calendar, not null
        field - the field from Calendar
        Returns:
        true if equal; otherwise false
        Throws:
        IllegalArgumentException - if any argument is null