Class LocalDateTimeStringConverter

java.lang.Object
javafx.util.StringConverter<LocalDateTime>
javafx.util.converter.LocalDateTimeStringConverter

public class LocalDateTimeStringConverter extends StringConverter<LocalDateTime>
A StringConverter implementation for LocalDateTime values.
Since:
JavaFX 8u40
See Also:
  • Constructor Details

    • LocalDateTimeStringConverter

      public LocalDateTimeStringConverter()

      Creates a LocalDateTimeStringConverter that uses a formatter and parser based on IsoChronology, FormatStyle.SHORT for both date and time, and the user's Locale.

      This converter ensures symmetry between the toString() and fromString() methods. Many of the default locale based patterns used by DateTimeFormatter will display only two digits for the year when formatting to a string. This would cause a value like 1955 to be displayed as 55, which in turn would be parsed back as 2055. This converter modifies two-digit year patterns to always use four digits. The input parsing is not affected, so two digit year values can still be parsed as expected in these locales.

    • LocalDateTimeStringConverter

      public LocalDateTimeStringConverter(FormatStyle dateStyle, FormatStyle timeStyle)
      Creates a LocalDateTimeStringConverter that uses a formatter and parser based on IsoChronology, the given FormatStyle values for date and time, and the user's Locale.
      Parameters:
      dateStyle - the FormatStyle that will be used by the formatter and parser for the date. If null, FormatStyle.SHORT will be used.
      timeStyle - the FormatStyle that will be used by the formatter and parser for the time. If null, FormatStyle.SHORT will be used.
    • LocalDateTimeStringConverter

      public LocalDateTimeStringConverter(FormatStyle dateStyle, FormatStyle timeStyle, Locale locale, Chronology chronology)
      Creates a LocalDateTimeStringConverter that uses a formatter and parser based on the given FormatStyles, Locale, and Chronology.
      Parameters:
      dateStyle - the FormatStyle that will be used by the formatter and parser for the date. If null, FormatStyle.SHORT will be used.
      timeStyle - the FormatStyle that will be used by the formatter and parser for the time. If null, FormatStyle.SHORT will be used.
      locale - the Locale that will be used by the formatter and parser. If null, the user's locale will be used.
      chronology - the Chronology that will be used by the formatter and parser. If null, IsoChronology.INSTANCE will be used.
    • LocalDateTimeStringConverter

      public LocalDateTimeStringConverter(DateTimeFormatter formatter, DateTimeFormatter parser)

      Creates a LocalDateTimeStringConverter that uses the given formatter and parser.

      For example, to use a fixed pattern for converting both ways:

      String pattern = "yyyy-MM-dd HH:mm";
      DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
      StringConverter<LocalDateTime> converter = DateTimeStringConverter.getLocalDateTimeConverter(formatter, null);
      

      Note that the formatter and parser can be created to handle non-default Locale and Chronology as needed.

      Parameters:
      formatter - the formatter that will be used by the toString() method. If null, a default formatter will be used.
      parser - the parser that will be used by the fromString() method. This can be identical to formatter. If null, formatter will be used, and if that is also null, a default parser will be used.
  • Method Details

    • fromString

      public LocalDateTime fromString(String value)
      Converts a string to an object. The parsing mechanism is defined by the specific converter. null and empty string are converted to null.
      Parameters:
      value - the String to convert
      Returns:
      an object of type T created from the string passed in
    • toString

      public String toString(LocalDateTime value)
      Converts an object to a string form. The format of the returned string is defined by the specific converter. null is converted to an empty string, otherwise the type's toString is used.
      Parameters:
      value - the object of type T to convert
      Returns:
      a string representation of the object passed in