Class CalUtil

java.lang.Object
com.groiss.cal.CalUtil

public class CalUtil extends Object
This class provides some utilities for date and time handling. It uses two properties: 'DateFormat' and 'DateTimeFormat' which must be set in the default configuration object. The integer constants in this class can be used with the method getDateFormatter to get a thread save DateFormat instance for this format.
  • Field Details

    • DATE

      public static final int DATE
      Date format as set in the configuration
      See Also:
    • DATE_TIME

      public static final int DATE_TIME
      Date and time format as set in the configuration
      See Also:
    • RFC1123

      public static final int RFC1123
      Date format RFC1123: EEE, dd MMM yyyy HH:mm:ss z
      See Also:
    • RFC1036

      public static final int RFC1036
      Date format RFC1036: EEEEEEEEE, dd-MMM-yy HH:mm:ss z
      See Also:
    • ANSI_C_ASCTIME

      public static final int ANSI_C_ASCTIME
      ANSI C's asctime() format: EEE MMM d HH:mm:ss yyyy
      See Also:
    • LOG_DATE

      public static final int LOG_DATE
      Date format for log output: yyyy-MM-dd HH:mm:ss.SSS
      See Also:
    • ISO

      public static final int ISO
      Date format ISO 8601: yyyy-MM-dd'T'HH:mm:ss'Z'
      See Also:
    • XML_DATE

      public static final int XML_DATE
      xml date format: yyyy-MM-dd
      See Also:
    • XML_DATE_UTC

      public static final int XML_DATE_UTC
      xml date format: yyyy-MM-dd'Z'
      See Also:
    • DATE_TIME_SEC

      public static final int DATE_TIME_SEC
      date format : yyyy-MM-dd HH:mm:ss
      See Also:
    • DATE_TIME_SEC_DIGITS

      public static final int DATE_TIME_SEC_DIGITS
      date format : yyyyMMddHHmmss
      See Also:
    • DATE_TIME_WITHOUT_ZONE

      public static final int DATE_TIME_WITHOUT_ZONE
      DATE_TIME without zone info
      See Also:
    • YEAR_THOUSAND

      public static final long YEAR_THOUSAND
      the time value of the date object of date January 1, 1000
      See Also:
    • MILLISPERDAY

      public static final long MILLISPERDAY
      Number of milliseconds per day
      See Also:
    • NUMBER_CHECK_PATTERN

      public static final Pattern NUMBER_CHECK_PATTERN
    • SECONDS

      public static final int SECONDS
      TimeInterval Reporing & Time Managment
      See Also:
    • MINUTES

      public static final int MINUTES
      See Also:
    • HOURS

      public static final int HOURS
      See Also:
    • DAYS

      public static final int DAYS
      See Also:
    • WEEK

      public static final int WEEK
      See Also:
    • SHORT_UNIT_NAMES

      public static final String[] SHORT_UNIT_NAMES
      short and normal names for TimeUnits
    • UNIT_NAMES

      public static final String[] UNIT_NAMES
    • UNIT_FACTORS

      public static final long[] UNIT_FACTORS
      the Unit Factorys
  • Method Details

    • addWorkdays

      public static Date addWorkdays(Date d1, int days)
      adds the specified amount of workdays to the given date
      Parameters:
      d1 -
      days -
      Returns:
      the new date
    • areHolidaysConfigured

      public static boolean areHolidaysConfigured()
      Returns:
      true if the special class was configured for holidays; otherwise returns false
    • deleteTimeInfoUntil

      public static void deleteTimeInfoUntil(Calendar cal, int field)
      Set the time information to 0 starting at the given field, for example hour
      Parameters:
      cal -
      field -
    • easterDay

      public static int easterDay(int year)
      calculates the day of the easter-sunday for the given year
      Parameters:
      year -
      Returns:
      the easter-sunday for the given year
    • getCalendar

      public static Calendar getCalendar()
      Returns:
      a calendar instance with ThreadContext.getThreadLocale() and ThreadContext.getThreadTimeZone()
      See Also:
    • getCalendar

      public static Calendar getCalendar(Locale loc)
      Returns:
      a calendar instance
      See Also:
    • getCalendar

      public static Calendar getCalendar(TimeZone tz)
      Returns:
      a calendar instance
      See Also:
    • getCalendar

      public static Calendar getCalendar(Locale loc, TimeZone tz)
      Returns a calendar instance. The calendar class can be set using the configuration parameter "calendar.class". Default is the GregorianCalendar.
      Parameters:
      loc - the locale if locale is null, Locale.getDefault() is used
      tz - the TimeZone if timezone is null, TimeZone.getDefault() is used
      Returns:
      a calendar instance
    • getJavaCalendar

      public static Calendar getJavaCalendar(Calendar icuCalendar)
    • getDateFormatSymbols

      public static DateFormatSymbols getDateFormatSymbols()
      Returns a DateFormatSymbols instance for the current ThreadContext.getThreadLocale()
      Returns:
      a DateFormatSymbols instance
      See Also:
    • getDateFormatSymbols

      public static DateFormatSymbols getDateFormatSymbols(Calendar calendar, Locale loc)
      Returns a DateFormatSymbols instance for the specified Locale and Calendar
      if Calendar is null getCalendar() is used
      if Locale is null Locale.getDefault() is used
      Parameters:
      calendar - the calendar
      loc - the locale
      Returns:
      a DateFormatSymbols instance
    • getDateFormatSymbols

      public static DateFormatSymbols getDateFormatSymbols(Locale loc)
      Parameters:
      loc - the locale
      Returns:
      a DateFormatSymbols instance
      See Also:
    • getDateFormatter

      public static SimpleDateFormat getDateFormatter(int type)
      Returns a thread save DateFormat object for the given format.
      Parameters:
      type - is one of the integer constants of this class (DATE, RFC1123, ...)
      Returns:
      a DateFormat instance
    • getDateFormatter

      public static SimpleDateFormat getDateFormatter(int type, Locale loc)
      Get local specific instance of predefined formatter types
      Parameters:
      type -
      loc -
      Returns:
      local specific instance of predefined formatter types
    • getDateFormatter

      public static SimpleDateFormat getDateFormatter(int type, Locale locale, TimeZone timezone)
      get the localized SimpleDateFormat with the pattern specified by type if the given type has a fixed locale / timezone the given locale / timezone is ignored
      Parameters:
      type - one of DATE,DATE_TIME,DATE_TIME_SEC,DATE_TIME_WITHOUT_ZONE,DATE_TIME_SEC_DIGITS
      locale - the locale
      timezone - the timezone
      Returns:
      the DateFormat
    • getDateFormatter

      public static SimpleDateFormat getDateFormatter(int type, Locale locale, TimeZone timezone, Calendar cal)
      Parameters:
      type - one of DATE,DATE_TIME,DATE_TIME_SEC,DATE_TIME_WITHOUT_ZONE,DATE_TIME_SEC_DIGITS
      locale - the locale
      timezone - the timezone
      cal - the calendar to be used
      Returns:
      the DateFormat
    • getDateFormatter

      public static SimpleDateFormat getDateFormatter(int type, TimeZone tz)
      Parameters:
      type -
      tz -
      Returns:
      the date formatter
      See Also:
    • getDateFormatter

      public static SimpleDateFormat getDateFormatter(String pattern)
      Parameters:
      pattern -
      Returns:
      the date formatter
      See Also:
    • getDateFormatter

      public static SimpleDateFormat getDateFormatter(String pattern, Locale loc)
      Parameters:
      pattern -
      loc -
      Returns:
      the date formatter
      See Also:
    • getDateFormatter

      public static SimpleDateFormat getDateFormatter(String pattern, Locale locale, TimeZone timezone)
      get a SimpleDateFormat instance for the pattern/locale/timezone
      Parameters:
      pattern -
      locale -
      timezone -
      Returns:
      the date formatter
    • getDateFormatter

      public static SimpleDateFormat getDateFormatter(String pattern, TimeZone tz)
      Parameters:
      pattern -
      tz -
      Returns:
      the date formatter
      See Also:
    • getIntervalInUnits

      public static String getIntervalInUnits(long interval)
      Format a time interval.
      Parameters:
      interval - duration in milliseconds
      Returns:
    • getIntervalInUnits

      public static String getIntervalInUnits(long interval, int maxunit)
    • getIntervalInUnits

      public static String getIntervalInUnits(long interval, int maxunit, int minunit)
    • getIntervalInUnits

      public static String getIntervalInUnits(long interval, int maxunit, int minunit, String[] unitNames)
    • getIntervalInUnits

      public static String getIntervalInUnits(long interval, int maxunit, String[] unitNames)
      Returns a String representation of a TimeIntervall
      Parameters:
      interval - the intervall in millisecondes
      maxunit -
      unitNames - - The UntiNames to use for the string representation
      Returns:
      a String representation of this Intervals, embedded in a DIV block with textalign right.
    • getIntervalInUnits

      public static String getIntervalInUnits(long interval, String maxunit)
      Returns a String representation of a TimeIntervall using the short names of timeunits
      Parameters:
      interval - the intervall in millisecondes
      maxunit - - the unis as string (maybe short or normal name)
      Returns:
      a String representation of this Intervals, embedded in a DIV block with textalign right.
    • getIntervalInUnits

      public static String getIntervalInUnits(long interval, String maxunit, String minunit)
    • getUnitIntFromString

      public static int getUnitIntFromString(String unit)
    • isHoliday

      public static String isHoliday(Calendar c)
      Returns null or the name of the holiday for the given day
      Returns:
      a holiday name or null
    • isNonWorkingDay

      public static boolean isNonWorkingDay(int day_of_week)
      this method checky configured workind days of week only. It doesnt check local holydays
    • isWorkingDay

      public static boolean isWorkingDay(Calendar c)
      checks if a given date is defined as working day
      Parameters:
      c - the given date
      Returns:
      true if date is a configured working day
    • parseDate

      public static Date parseDate(int type, String dateString)
      parse a date from a string with the specified format
      Parameters:
      type - the specified format to apply on the passed string
      dateString - the string representing a date which shall be parsed to a date object
      Returns:
      Date instance
    • parseDate

      public static Date parseDate(String dateString, int... type)
      Parse a date from a string using the specified format.
      Parameters:
      dateString - the string representing a date which shall be parsed to a date object
      type - the specified formats to apply on the passed string
      Returns:
      Date instance
    • parseDate

      public static Date parseDate(String s)
      Parse a date from a string.
      Parameters:
      s - a string representing a date or date + time
      Returns:
      a date object
    • parseDateEndDay

      public static Date parseDateEndDay(String s)
      Parses the date sets the time to 23:59:59:999. This is useful for intervals. if the date is passed as date+time, the second/minute is also rounded to the next full second/minute
      Returns:
      a date object
    • parseTimeZone

      public static TimeZone parseTimeZone(TimeZone tz)
    • reconfigure

      public static void reconfigure()
    • setDateFormatter

      public static void setDateFormatter(int type, SimpleDateFormat df)
    • showDate

      public static String showDate(int type, Locale locale, TimeZone timezone, Object d)
    • showDate

      public static String showDate(int type, Object d)
    • showDate

      public static String showDate(Object d)
      Show the date using the default formatter.
      Parameters:
      d - the date object
    • showDate

      public static String showDate(String pattern, Locale locale, TimeZone timeZone, Object d)
    • showDate

      public static String showDate(String pattern, Object d)
    • showDateTime

      public static String showDateTime(Object d)
      Show the date and time using the default formatter.
      Parameters:
      d - the date object
    • showDateTimeSeconds

      public static String showDateTimeSeconds(Object d)
      Show the date and time in the form yyyy-MM-dd HH:mm:ss.
      Parameters:
      d - the date object
    • parseDuration

      public static Duration parseDuration(String duration, String key)
      Parses a time string to a java.time.Duration Object
      Parameters:
      duration - Can be a time string in format [dD][hH][mM][s[.f]S] or just seconds
      key - The key of the property
      Returns:
      A Duration in ISO-8601 format
    • showDuration

      public static String showDuration(Object obj)
      Show the duration in format *d*h*m*s
    • thisYear

      public static int thisYear()
      Return the current year.
      Returns:
      the current year as integer.
    • toBeginDay

      public static Date toBeginDay(Date date)
      returns the date set to 00:00:00:00
      Parameters:
      date -
      Returns:
      the date set to 00:00:00:00
    • toEndDay

      public static Date toEndDay(Date date)
      returns a date set to 23:59:59:999
      Parameters:
      date -
      Returns:
      the date set to 23:59:59:999
    • workDaysBetween

      public static int workDaysBetween(Date d1, Date d2)
      Compute the working days between the two argument date
      Parameters:
      d1 - the start date
      d2 - the end date
      Returns:
      the days between, excluding holidays, saturdays, and sundays, including the start and the end day.
    • isBetween

      public static boolean isBetween(Date date, Date start, Date end)
      returns true if the passed date is not before start and not after end. If start is null the passed date is considered as not before start. If end is null the passed date is considered as not after end.
    • daysbetween

      public static int daysbetween(Calendar start, Date endDate, boolean removeEarlyEnd)
      Days between start and end date, including start.
      Parameters:
      start - the FROM date
      endDate - the TO date
      removeEarlyEnd - end date is only included if hour>0
      Returns:
      the days