Class DateTimeConverter

java.lang.Object
jakarta.faces.convert.DateTimeConverter
All Implemented Interfaces:
PartialStateHolder, StateHolder, Converter

public class DateTimeConverter extends Object implements Converter, PartialStateHolder

Converter implementation for java.util.Date values.

The getAsObject() method parses a String into a java.util.Date, according to the following algorithm:

  • If the specified String is null, return a null. Otherwise, trim leading and trailing whitespace before proceeding.
  • If the specified String - after trimming - has a zero length, return null.
  • If the locale property is not null, use that Locale for managing parsing. Otherwise, use the Locale from the UIViewRoot.
  • If a pattern has been specified, its syntax must conform the rules specified by java.text.SimpleDateFormat or java.time.format.DateTimeFormatter. Which of these two formatters is used depends on the value of type. Such a pattern will be used to parse, and the type, dateStyle, and timeStyle properties will be ignored, unless the value of type is one of the java.time specific values listed in setType(java.lang.String). In this case, DateTimeFormatter.ofPattern(String, Locale) must be called, passing the value of pattern as the first argument and the current Locale as the second argument, and this formatter must be used to parse the incoming value.
  • If a pattern has not been specified, parsing will be based on the type property, which expects a date value, a time value, both, or one of several values specific to classes in java.time as listed in setType(java.lang.String). Any date and time values included will be parsed in accordance to the styles specified by dateStyle and timeStyle, respectively.
  • If a timezone has been specified, it must be passed to the underlying DateFormat instance. Otherwise the "GMT" timezone is used.
  • In all cases, parsing must be non-lenient; the given string must strictly adhere to the parsing format.

The getAsString() method expects a value of type java.util.Date (or a subclass), and creates a formatted String according to the following algorithm:

  • If the specified value is null, return a zero-length String.
  • If the specified value is a String, return it unmodified.
  • If the locale property is not null, use that Locale for managing formatting. Otherwise, use the Locale from the UIViewRoot.
  • If a timezone has been specified, it must be passed to the underlying DateFormat instance. Otherwise the "GMT" timezone is used.
  • If a pattern has been specified, its syntax must conform the rules specified by java.text.SimpleDateFormat or java.time.format.DateTimeFormatter. Which of these two formatters is used depends on the value of type. Such a pattern will be used to format, and the type, dateStyle, and timeStyle properties will be ignored, unless the value of type is one of the java.time specific values listed in setType(java.lang.String). In this case, DateTimeFormatter.ofPattern(String, Locale) must be called, passing the value of pattern as the first argument and the current Locale as the second argument, and this formatter must be used to format the outgoing value.
  • If a pattern has not been specified, formatting will be based on the type property, which includes a date value, a time value, both or into the formatted String. Any date and time values included will be formatted in accordance to the styles specified by dateStyle and timeStyle, respectively.