Class TimeUtils

java.lang.Object
ru.bgcrm.util.TimeUtils

public class TimeUtils extends Object
  • Field Details

  • Constructor Details

    • TimeUtils

      public TimeUtils()
  • Method Details

    • getTypeFormat

      public static String getTypeFormat(String type)
      Date and time format string compatible with SimpleDateFormat
      Parameters:
      type - type of date-time: FORMAT_TYPE_YMD,FORMAT_TYPE_YMDH, FORMAT_TYPE_YMDHM, FORMAT_TYPE_YMDHMS
      Returns:
      matching date format or type itself
    • format

      public static final String format(Date date, String patternType)
      Formats a string with the date by the given pattern
      Parameters:
      date - the input date
      patternType - the date pattern: FORMAT_TYPE_YMD, FORMAT_TYPE_YMDH, FORMAT_TYPE_YMDHM, FORMAT_TYPE_YMDHMS
      Returns:
      the string if the input date is not null and the pattern is correct, otherwise null
    • formatSqlDate

      public static final String formatSqlDate(Date date)
      Formats a date as 'yyyy-MM-dd' for substitution into an SQL query, already wrapped in quotes
      Parameters:
      date - the input date
      Returns:
      the formatted string
    • format

      public static final String format(Duration duration)
      Formats a duration object to a human-readable string like 5h 15m
      Parameters:
      duration - the duration
      Returns:
      the formatted string
    • formatPeriod

      public static final String formatPeriod(Date dateFrom, Date dateTo, String patternType)
    • formatPeriod

      public static final String formatPeriod(Date date1, Date date2)
      Formats a period string
      Parameters:
      date1 - the period start
      date2 - the period end
      Returns:
      the string "dd.MM.yyyy-dd.MM.yyyy"; if one of the parameters is null, an empty string is printed instead of the corresponding date, e.g. "-dd.MM.yyyy", "dd.MM.yyyy-", "-"
    • parse

      public static final Date parse(String date, String patternType)
    • parse

      public static final Date parse(String date, String patternType, Date defaultValue)
    • parsePeriod

      public static final void parsePeriod(String period, PeriodSet periodSet)
    • parsePeriod

      public static final void parsePeriod(String period, String patternType, PeriodSet periodSet)
    • convertCalendarToDate

      public static final Date convertCalendarToDate(Calendar calendar)
      Converts a Calendar object to Date
      Parameters:
      calendar - the input Calendar object
      Returns:
      the Date object, or null if the input object is null
    • convertSqlDateToDate

      public static final Date convertSqlDateToDate(Date date)
      Converts a Date object to Date
      Parameters:
      date - the input object
      Returns:
      the Date object, or null if the input object is null
    • convertDateToCalendar

      public static final Calendar convertDateToCalendar(Date date)
      Converts Date to Calendar
      Parameters:
      date - the input object
      Returns:
      the Calendar, if date is not null, otherwise null
    • convertTimestampToCalendar

      public static final Calendar convertTimestampToCalendar(Timestamp time)
      Converts Timestamp to Calendar
      Parameters:
      time - the input object
      Returns:
      the Calendar, if time is not null, otherwise null
    • convertDateToSqlDate

      public static final Date convertDateToSqlDate(Date date)
      Converts a Date object to Date
      Parameters:
      date - the input object
      Returns:
      the Date, if date is not null, otherwise null
    • convertCalendarToTimestamp

      public static final Timestamp convertCalendarToTimestamp(Calendar calendar)
      Converts a Calendar object to Timestamp
      Parameters:
      calendar - the input object
      Returns:
      the Timestamp, if calendar is not null, otherwise null
    • convertLongToTimestamp

      public static final Timestamp convertLongToTimestamp(long millis)
      Converts long to Timestamp
      Parameters:
      millis - the input value
      Returns:
      the Timestamp
    • daysDelta

      public static final int daysDelta(Date dayFrom, Date dayTo)
      24h days difference between two dates. The method is time-proven, but since Java 8 there is also available: ChronoUnit.DAYS.between(Temporal, Temporal)
      Parameters:
      dayFrom - the first day
      dayTo - the second day
      Returns:
      the difference in days
    • getDayOfWeekPosition

      public static final int getDayOfWeekPosition(Date date)
      Day of week starting from Monday = 1
      Parameters:
      date - the date
      Returns:
      the day of week position
    • dateBefore

      public static boolean dateBefore(Date date1, Date date2)
      Checks date1 < date2 (day precision)
      Parameters:
      date1 - the first date
      date2 - the second date
      Returns:
      true if date1 < date2, otherwise false
    • dateEqual

      public static boolean dateEqual(Date date1, Date date2)
      Checks date1 == date2 (day precision)
      Parameters:
      date1 - the first date
      date2 - the second date
      Returns:
      true if date1 == date2, otherwise false
    • dateBeforeOrEq

      public static boolean dateBeforeOrEq(Calendar date1, Calendar date2)
      Checks date1 <= date2 (day precision)
      Parameters:
      date1 - the first date
      date2 - the second date
      Returns:
      true if date1 <= date2, otherwise false
    • dateBeforeOrEq

      public static boolean dateBeforeOrEq(Date date1, Date date2)
      Checks date1 <= date2 (day precision)
      Parameters:
      date1 - the first date
      date2 - the second date
      Returns:
      true if date1 <= date2, otherwise false
    • dateInRange

      public static final boolean dateInRange(Calendar checking, Calendar date1, Calendar date2)
      Checks whether the checked date falls within the given period
      Parameters:
      checking - the checked date
      date1 - the period start
      date2 - the period end
      Returns:
      true if it falls within, false otherwise
    • dateInRange

      public static final boolean dateInRange(Date checking, Date date1, Date date2)
      Checks whether the checked date falls within the given period
      Parameters:
      checking - the checked date
      date1 - the period start
      date2 - the period end
      Returns:
      true if it falls within, false otherwise
    • periodInRange

      public static final boolean periodInRange(Calendar checkingDate1, Calendar checkingDate2, Calendar date1, Calendar date2)
      Checks whether the checked period falls within the given one
      Parameters:
      checkingDate1 - the checked period start
      checkingDate2 - the checked period end
      date1 - the given period start
      date2 - the given period end
      Returns:
      true if it falls within, false otherwise
    • checkDateIntervalsIntersection

      public static boolean checkDateIntervalsIntersection(Calendar date1, Calendar date2, Calendar dateFrom, Calendar dateTo)
      Checks the intersection of two date intervals
      Parameters:
      date1 - the left bound of the first interval
      date2 - the right bound of the first interval
      dateFrom - the left bound of the second interval
      dateTo - the right bound of the second interval
      Returns:
      true if the intervals intersect, or one of them has both bounds null
    • moveToEndOfMonth

      public static final void moveToEndOfMonth(Calendar date)
      Sets the date to the last day of the month
      Parameters:
      date - the input date
    • moveToStartNextDay

      public static final void moveToStartNextDay(Calendar date)
      Increases date by 1 day and resets the time to 00:00:00.0
      Parameters:
      date - the input date
    • clear_HOUR_MIN_MIL_SEC

      @Deprecated public static final Calendar clear_HOUR_MIN_MIL_SEC(Calendar time)
      Deprecated.
      Resets the time to the start of the day
      Parameters:
      time - the input date
      Returns:
      the reset date (hours=minutes=seconds=millis=0)
    • clear_HOUR_MIN_MIL_SEC

      @Deprecated public static final Date clear_HOUR_MIN_MIL_SEC(Date time)
      Deprecated.
      Resets the time to the start of the day
      Parameters:
      time - the input date
      Returns:
      the reset date (hours=minutes=seconds=millis=0)
    • getEndMonth

      public static final Date getEndMonth(Date date)
      Last day of a month
      Parameters:
      date - any day of the month
      Returns:
      null if date is null, else the last day of the month
    • getNextDay

      public static final Calendar getNextDay(Calendar date)
      The next day from the date
      Parameters:
      date - the date
      Returns:
      the next day
    • getPrevDay

      public static final Calendar getPrevDay(Calendar date)
      The previous day from the date
      Parameters:
      date - the date
      Returns:
      the previous day
    • getNextDay

      public static final Date getNextDay(Date date)
      The next day from the date
      Parameters:
      date - the date
      Returns:
      the next day
    • getPrevDay

      public static final Date getPrevDay(Date date)
      The previous day from the date
      Parameters:
      date - the date
      Returns:
      the previous day
    • getPrevMonth

      public static final Date getPrevMonth()
      Returns:
      first day in the previous month from the current time
    • getShortDateName

      @Deprecated public static String getShortDateName(Date date)
      Deprecated.
      Returns the abbreviated day-of-week name ("пн" - Monday, "вт" - Tuesday, etc.)
      Parameters:
      date - the date used to determine the day of week
      Returns:
      the abbreviated day-of-week name
    • timezoneChange

      public static Date timezoneChange(Date time, TimeZone fromTz, TimeZone toTz)
      Adjusts a Date object so that the same local time results in a different timezone
      Parameters:
      time - the input Date object with some time for timezone fromTz
      fromTz - the source timezone
      toTz - the target timezone
      Returns:
      the adjusted date