Class OBDateUtils


  • public class OBDateUtils
    extends Object
    Utilities to manage dates.
    • Constructor Detail

      • OBDateUtils

        public OBDateUtils()
    • Method Detail

      • formatDate

        public static String formatDate​(Date date)
        Returns an String with the date in the dateFormat.java format defined in Openbravo.properties
        Parameters:
        date - Date to be formatted.
        Returns:
        String formatted.
        See Also:
        formatDate(Date, String)
      • formatDateTime

        public static String formatDateTime​(Date date)
        Returns an String with the date in the dateTimeFormat.java format defined in Openbravo.properties
        Parameters:
        date - Date to be formatted.
        Returns:
        String formatted.
        See Also:
        formatDate(Date, String)
      • formatDate

        public static String formatDate​(Date date,
                                        String pattern)
        Returns an String with the date in the specified format
        Parameters:
        date - Date to be formatted.
        pattern - Format expected for the output.
        Returns:
        String formatted.
      • getDate

        public static Date getDate​(String strDate)
                            throws ParseException
        Parses the string to a date using the dateFormat.java property.
        Parameters:
        strDate - String containing the date
        Returns:
        the date
        Throws:
        ParseException
      • getDateTime

        public static Date getDateTime​(String strDate)
                                throws ParseException
        Parses the string to a date with time using the dateTimeFormat defined in Openbravo.properties. If the string parameter does not have time include it will add the current hours, minutes and seconds.
        Parameters:
        strDate - String date.
        Returns:
        the date with time.
        Throws:
        ParseException
      • calculateLaborDays

        public static String calculateLaborDays​(String _strDate1,
                                                String _strDate2,
                                                DateFormat dateFormatter)
                                         throws ParseException
        Determines the labor days between two dates
        Parameters:
        _strDate1 - Date 1.
        _strDate2 - Date 2.
        dateFormatter - Format of the dates.
        Returns:
        strLaborDays as the number of days between strDate1 and strDate2.
        Throws:
        ParseException
      • addDaysToDate

        public static String addDaysToDate​(String strDate,
                                           int days,
                                           DateFormat dateFormatter)
                                    throws ParseException
        Adds an integer number of days to a given date
        Parameters:
        strDate - Start date.
        days - Number of days to add.
        dateFormatter - Format of the date.
        Returns:
        strFinalDate as the sum of strDate plus strDays.
        Throws:
        ParseException
      • getDateFormatter

        public static DateFormat getDateFormatter​(VariablesSecureApp vars)
        Determines the format of the date
        Parameters:
        vars - Global variables.
        Returns:
        DateFormatter as the format of the date.
      • isWeekendDay

        public static boolean isWeekendDay​(String strDay,
                                           DateFormat dateFormatter)
                                    throws ParseException
        Determines if a day is a day of the weekend, i.e., Saturday or Sunday
        Parameters:
        strDay - Given Date.
        dateFormatter - Format of the date.
        Returns:
        true if the date is a Sunday or a Saturday.
        Throws:
        ParseException
      • isBiggerDate

        public static boolean isBiggerDate​(String strDate1,
                                           String strDate2,
                                           DateFormat dateFormatter)
                                    throws ParseException
        Determines if a date 1 is bigger than a date 2
        Parameters:
        strDate1 - Date 1.
        strDate2 - Date 2.
        dateFormatter - Format of the dates.
        Returns:
        true if strDate1 is bigger than strDate2.
        Throws:
        ParseException
      • getEndOfDay

        public static Date getEndOfDay​(Date day)
        Returns the last second of the day passed as argument
      • convertDateToUTC

        public static Date convertDateToUTC​(Date date)
        Returns a new Date converted to UTC
        Parameters:
        date - Date to be converted to UTC
      • truncate

        public static Date truncate​(Date date)
        Returns a new Date with the time truncated
        Parameters:
        date - Date to be truncated
        Returns:
        Date truncated.
      • truncate

        public static Date truncate​(Date date,
                                    ChronoUnit unit)
        Returns a new Date truncated with the value in the specified unit
        Parameters:
        date - Date to be truncated
        unit - the unit to truncate to, not null
        Returns:
        Date truncated.
      • convertFromServerToOrgDateTime

        public static ZonedDateTime convertFromServerToOrgDateTime​(Date date,
                                                                   String timezone)
        Converts a UTC date contained in a Date object and transforms it into a ZonedDateTime object that takes into account the time zone provided as an argument
        Parameters:
        date - A Date object in the server timezone
        timezone - A Time Zone identifier as returned by ZoneId.getAvailableZoneIds()
        Returns:
        a ZonedDateTime object now taking the provided Time zone into account or null if the timezone is not valid
      • convertFromServerToOrgDateTime

        public static ZonedDateTime convertFromServerToOrgDateTime​(Date date,
                                                                   Organization organization)
        Converts a UTC date contained in a Date object and transforms it into a ZonedDateTime object that takes into account the time zone for the given Organization
        Parameters:
        date - A Date object in the server timezone
        organization - An Organization instance
        Returns:
        a ZonedDateTime object now taking the provided Time zone into account or null if the timezone is not valid
      • formatZonedDateTime

        public static String formatZonedDateTime​(ZonedDateTime date,
                                                 String pattern)
        Format the given ZonedDateTime using the format provided in pattern
        Parameters:
        date - A ZonedDateTime with Time zone information
        pattern - A date String pattern that should be accepted by DateTimeFormatter
        Returns:
        a String with a formatted date, or empty string if there is no date object
      • formatZonedDateTime

        public static String formatZonedDateTime​(ZonedDateTime date)
        Format the given ZonedDateTime using the dateTimeFormat.java format provided in Openbravo.properties including the time zone information
        Parameters:
        date - A ZonedDateTime with Time zone information
        Returns:
        a String with a formatted date, or empty string if there is no date object