Class DateStringTranslator

  • All Implemented Interfaces:
    ValueTranslator<java.util.Date,​java.lang.String>

    @ValueTranslatorService(modelClass=java.util.Date.class,
                            viewClass=java.lang.String.class)
    public class DateStringTranslator
    extends ValueStringTranslator<java.util.Date>
    Date translator.

    The date can be entered in the specified format or as a shortcut. The following shortcuts are defined:

    • 5/29/: expands to May 29 of the current year (midnight).
    • 0529: dto.
    • 05290": if the year is 4-digits in length the century will be added which is closest to the current date, i.e.:
    • 5/29/99: will be expanded to "05/29/1999" and _not_ "05/29/2099".
    • 7:00: today at 7:00am
    Furthermore, the date can determined in relation to the current time:
    • /.,-=* or the date-delimiter of the current locale: current date
    • :;'": current time
    • +3d: today 0:00:00 plus 3 days
    • -2y: today 2 years ago.
    • 17: the unit is determined according to the model's type. If Time the unit defaults to the most significant format-pattern (usually hours). Else if Date or Timestamp, the unit defaults to 'd'. For dates this means the 17th of the current month. If the type is a Time, it means 5pm.
    • +14: same as above but the value will be added (or subtracted if negative) to (from) the current time. For date fields, for example, this is again shorter than "+14d".
    • 4m: the unit according to the letter following the number will be set _and_ the next "smaller" unit set to its minimum. In this example, the time (if it is a time field) will be set to 4 minutes and 0 seconds. Likewise, "6y" would mean "January 1st, 2006". Consequently, "8h" is an even shorter way to express "today at 8am" than "8:00".
    The units are the same as described in SimpleDateFormat with some minor differences:
    • "y" or "Y": year(s)
    • "M": month(s). In date fields without minutes a lowercase "m" works as well.
    • "w or W": week(s) or calendar week. For example: "-2w" means "two weeks ago" but "30w" means the first day of week 30.
    • "d oder D": day(s)
    • "h oder H": hour(s). Notice that "-24h" means "24 hours ago" and is not the dame as "-1d" which means "yesterday 0am".
    • "m": minute(s)
    • "s oder S": second(s)
    The shortcuts (except the units) are locale dependent. In German, for example, the shortcuts are as follows:
    • "29.5.": adds the current year
    • "2905": dto.
    • "290506": the closest year if the year is 4-digit, i.e.:
    • "29.5.99": becomes "29.05.1999" and not "29.05.2099".
    • "7:00": today 7 am
    Author:
    harald
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static org.tentackle.log.Logger.Level informationLossLogLevel
      Determines how to handle information loss when a timestamp is edited by a date field without a time format.
      Primarily this is the log-level, but the level also controls what to log and/or when to throw an exception: FINER: log with stacktrace and throw a FxRuntimeException FINE: log with stacktrace SEVERE: check disabled all other levels: just log without stacktrace The default is INFO.
      static java.lang.String REFERENCE_DATE_SUPPLIER
      Property for a reference date supplier of type Supplier<Date>.
      '@' as input will be replaced by that date.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected int convert2DigitYearTo4DigitYear​(int year2)
      Converts a short 2-digit year to a 4-digit year.
      protected java.lang.String format​(java.util.Date value)
      Formats the given date.
      protected java.lang.String getDefaultPattern()
      Gets the default pattern according to the type.
      protected java.text.SimpleDateFormat getFormat()
      Gets the date format.
      protected java.util.Date parse​(java.lang.String str)
      Parses a string.
      protected void setGregorianValue​(java.util.GregorianCalendar cal, int field, int value)
      Sets the gregorian value and checks whether the value is valid if date format is not lenient.
      java.util.function.Function<java.lang.String,​java.util.Date> toModelFunction()
      Function to translate a view value to a model value.
      protected java.util.Date toType​(java.util.Date date)
      Converts the date to the desired type.
      java.util.function.Function<java.util.Date,​java.lang.String> toViewFunction()
      Function to translate a model value to a view value.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • REFERENCE_DATE_SUPPLIER

        public static final java.lang.String REFERENCE_DATE_SUPPLIER
        Property for a reference date supplier of type Supplier<Date>.
        '@' as input will be replaced by that date.
        See Also:
        Constant Field Values
      • informationLossLogLevel

        public static org.tentackle.log.Logger.Level informationLossLogLevel
        Determines how to handle information loss when a timestamp is edited by a date field without a time format.
        Primarily this is the log-level, but the level also controls what to log and/or when to throw an exception:
        • FINER: log with stacktrace and throw a FxRuntimeException
        • FINE: log with stacktrace
        • SEVERE: check disabled
        • all other levels: just log without stacktrace
        The default is INFO.

        The check can be turned off if the level of the logger does not cover the given check level.

    • Constructor Detail

      • DateStringTranslator

        public DateStringTranslator​(FxTextComponent component)
        Creates a translator.
        Parameters:
        component - the text component
    • Method Detail

      • toViewFunction

        public java.util.function.Function<java.util.Date,​java.lang.String> toViewFunction()
        Description copied from interface: ValueTranslator
        Function to translate a model value to a view value.
        Returns:
        the function
      • toModelFunction

        public java.util.function.Function<java.lang.String,​java.util.Date> toModelFunction()
        Description copied from interface: ValueTranslator
        Function to translate a view value to a model value.
        Returns:
        the function
      • format

        protected java.lang.String format​(java.util.Date value)
        Formats the given date.
        Parameters:
        value - the date value
        Returns:
        the formatted string
      • toType

        protected java.util.Date toType​(java.util.Date date)
        Converts the date to the desired type.
        Parameters:
        date - the date
        Returns:
        the desired type
      • parse

        protected java.util.Date parse​(java.lang.String str)
        Parses a string.
        Parameters:
        str - the string
        Returns:
        the date
      • getDefaultPattern

        protected java.lang.String getDefaultPattern()
        Gets the default pattern according to the type.
        Returns:
        the pattern
      • getFormat

        protected java.text.SimpleDateFormat getFormat()
        Gets the date format.
        Returns:
        the date format
      • setGregorianValue

        protected void setGregorianValue​(java.util.GregorianCalendar cal,
                                         int field,
                                         int value)
                                  throws java.text.ParseException
        Sets the gregorian value and checks whether the value is valid if date format is not lenient.
        Parameters:
        cal - the gregorian calendar object
        field - the field index
        value - the value
        Throws:
        java.text.ParseException - if value out of bounds (if not lenient)
      • convert2DigitYearTo4DigitYear

        protected int convert2DigitYearTo4DigitYear​(int year2)
        Converts a short 2-digit year to a 4-digit year.
        Parameters:
        year2 - the 2-digit year
        Returns:
        the 4-digit year