Class CalendarConversion

  • All Implemented Interfaces:
    Conversion<java.lang.String,​java.util.Calendar>, FormattedConversion<java.text.SimpleDateFormat>

    public class CalendarConversion
    extends ObjectConversion<java.util.Calendar>
    implements FormattedConversion<java.text.SimpleDateFormat>
    Converts Strings to instances of Calendar and vice versa.

    This class supports multiple date formats. For example, you can define conversions from dates represented by different Strings such as "2001/05/02 and Dec/2013".

    The reverse conversion from a Calendar to String (in revert(Calendar) will return a formatted String using the date pattern provided in this class constructor

    The date patterns must follows the pattern rules of SimpleDateFormat

    See Also:
    SimpleDateFormat
    • Constructor Summary

      Constructors 
      Constructor Description
      CalendarConversion​(java.lang.String... dateFormats)
      Defines a conversion from String to Calendar using a sequence of acceptable date patterns.
      CalendarConversion​(java.util.Calendar valueIfStringIsNull, java.lang.String valueIfObjectIsNull, java.lang.String... dateFormats)
      Defines a conversion from String to Calendar using a sequence of acceptable date patterns.
      CalendarConversion​(java.util.Locale locale, java.lang.String... dateFormats)
      Defines a conversion from String to Calendar using a sequence of acceptable date patterns.
      CalendarConversion​(java.util.Locale locale, java.util.Calendar valueIfStringIsNull, java.lang.String valueIfObjectIsNull, java.lang.String... dateFormats)
      Defines a conversion from String to Calendar using a sequence of acceptable date patterns.
      CalendarConversion​(java.util.TimeZone timeZone, java.util.Locale locale, java.util.Calendar valueIfStringIsNull, java.lang.String valueIfObjectIsNull, java.lang.String... dateFormats)
      Defines a conversion from String to Calendar using a sequence of acceptable date patterns.
    • Constructor Detail

      • CalendarConversion

        public CalendarConversion​(java.util.TimeZone timeZone,
                                  java.util.Locale locale,
                                  java.util.Calendar valueIfStringIsNull,
                                  java.lang.String valueIfObjectIsNull,
                                  java.lang.String... dateFormats)
        Defines a conversion from String to Calendar using a sequence of acceptable date patterns. This constructor assumes the output of a conversion should be null when input is null
        Parameters:
        timeZone - the TimeZone of the given calendar date
        locale - the Locale that determines how the date mask should be formatted.
        valueIfStringIsNull - default Calendar value to be returned when the input String is null. Used when ObjectConversion.execute(String) is invoked.
        valueIfObjectIsNull - default String value to be returned when a Calendar input is null. Used when revert(Calendar) is invoked.
        dateFormats - list of acceptable date patterns The first pattern in this sequence will be used to convert a Calendar into a String in revert(Calendar).
      • CalendarConversion

        public CalendarConversion​(java.util.Locale locale,
                                  java.util.Calendar valueIfStringIsNull,
                                  java.lang.String valueIfObjectIsNull,
                                  java.lang.String... dateFormats)
        Defines a conversion from String to Calendar using a sequence of acceptable date patterns. This constructor assumes the output of a conversion should be null when input is null
        Parameters:
        locale - the Locale that determines how the date mask should be formatted.
        valueIfStringIsNull - default Calendar value to be returned when the input String is null. Used when ObjectConversion.execute(String) is invoked.
        valueIfObjectIsNull - default String value to be returned when a Calendar input is null. Used when revert(Calendar) is invoked.
        dateFormats - list of acceptable date patterns The first pattern in this sequence will be used to convert a Calendar into a String in revert(Calendar).
      • CalendarConversion

        public CalendarConversion​(java.util.Calendar valueIfStringIsNull,
                                  java.lang.String valueIfObjectIsNull,
                                  java.lang.String... dateFormats)
        Defines a conversion from String to Calendar using a sequence of acceptable date patterns. This constructor assumes the output of a conversion should be null when input is null
        Parameters:
        valueIfStringIsNull - default Calendar value to be returned when the input String is null. Used when ObjectConversion.execute(String) is invoked.
        valueIfObjectIsNull - default String value to be returned when a Calendar input is null. Used when revert(Calendar) is invoked.
        dateFormats - list of acceptable date patterns The first pattern in this sequence will be used to convert a Calendar into a String in revert(Calendar).
      • CalendarConversion

        public CalendarConversion​(java.util.Locale locale,
                                  java.lang.String... dateFormats)
        Defines a conversion from String to Calendar using a sequence of acceptable date patterns. This constructor assumes the output of a conversion should be null when input is null
        Parameters:
        locale - the Locale that determines how the date mask should be formatted.
        dateFormats - list of acceptable date patterns The first pattern in this sequence will be used to convert a Calendar into a String in revert(Calendar).
      • CalendarConversion

        public CalendarConversion​(java.lang.String... dateFormats)
        Defines a conversion from String to Calendar using a sequence of acceptable date patterns. This constructor assumes the output of a conversion should be null when input is null
        Parameters:
        dateFormats - list of acceptable date patterns The first pattern in this sequence will be used to convert a Calendar into a String in revert(Calendar).
    • Method Detail

      • revert

        public java.lang.String revert​(java.util.Calendar input)
        Converts Calendar to a formatted date String.

        The pattern used to generate the formatted date is the first date pattern provided in the constructor of this class

        Specified by:
        revert in interface Conversion<java.lang.String,​java.util.Calendar>
        Overrides:
        revert in class ObjectConversion<java.util.Calendar>
        Parameters:
        input - the Calendar to be converted to a String
        Returns:
        a formatted date String representing the date provided by the given Calendar, or the value of ObjectConversion.getValueIfObjectIsNull() if the Calendar parameter is null.
      • fromString

        protected java.util.Calendar fromString​(java.lang.String input)
        Converts a formatted date String to an instance of Calendar.

        The pattern in the formatted date must match one of the date patterns provided in the constructor of this class.

        Specified by:
        fromString in class ObjectConversion<java.util.Calendar>
        Parameters:
        input - the String containing a formatted date which must be converted to a Calendar
        Returns:
        the Calendar instance containing the date information represented by the given String, or the value of ObjectConversion.getValueIfStringIsNull() if the String input is null.
      • getFormatterObjects

        public java.text.SimpleDateFormat[] getFormatterObjects()
        Description copied from interface: FormattedConversion
        Returns the formatter objects
        Specified by:
        getFormatterObjects in interface FormattedConversion<java.text.SimpleDateFormat>
        Returns:
        the formatter objects used to apply formatting to values to generate formatted Strings, and parsing formatted Strings into values