< prev index next >

src/java.base/share/classes/java/time/format/DateTimeFormatter.java

Print this page




1668      * month, day-of-month and day-of-week, then there is only one way to resolve a
1669      * date, but the parsed value for day-of-week will be cross-checked against the
1670      * resolved date. Calling this method with the arguments {@link ChronoField#YEAR YEAR},
1671      * {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} and
1672      * {@link ChronoField#DAY_OF_MONTH DAY_OF_MONTH} will ensure that the date is
1673      * resolved correctly, but without any cross-check for the day-of-week.
1674      * <p>
1675      * In implementation terms, this method behaves as follows. The result of the
1676      * parsing phase can be considered to be a map of field to value. The behavior
1677      * of this method is to cause that map to be filtered between phase 1 and 2,
1678      * removing all fields other than those specified as arguments to this method.
1679      * <p>
1680      * This instance is immutable and unaffected by this method call.
1681      *
1682      * @param resolverFields  the new set of resolver fields, null if no fields
1683      * @return a formatter based on this formatter with the requested resolver style, not null
1684      */
1685     public DateTimeFormatter withResolverFields(TemporalField... resolverFields) {
1686         Set<TemporalField> fields = null;
1687         if (resolverFields != null) {

1688             fields = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(resolverFields)));
1689         }
1690         if (Objects.equals(this.resolverFields, fields)) {
1691             return this;
1692         }
1693         return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, fields, chrono, zone);
1694     }
1695 
1696     /**
1697      * Returns a copy of this formatter with a new set of resolver fields.
1698      * <p>
1699      * This returns a formatter with similar state to this formatter but with
1700      * the resolver fields set. By default, a formatter has no resolver fields.
1701      * <p>
1702      * Changing the resolver fields only has an effect during parsing.
1703      * Parsing a text string occurs in two phases.
1704      * Phase 1 is a basic text parse according to the fields added to the builder.
1705      * Phase 2 resolves the parsed field-value pairs into date and/or time objects.
1706      * The resolver fields are used to filter the field-value pairs between phase 1 and 2.
1707      * <p>




1668      * month, day-of-month and day-of-week, then there is only one way to resolve a
1669      * date, but the parsed value for day-of-week will be cross-checked against the
1670      * resolved date. Calling this method with the arguments {@link ChronoField#YEAR YEAR},
1671      * {@link ChronoField#MONTH_OF_YEAR MONTH_OF_YEAR} and
1672      * {@link ChronoField#DAY_OF_MONTH DAY_OF_MONTH} will ensure that the date is
1673      * resolved correctly, but without any cross-check for the day-of-week.
1674      * <p>
1675      * In implementation terms, this method behaves as follows. The result of the
1676      * parsing phase can be considered to be a map of field to value. The behavior
1677      * of this method is to cause that map to be filtered between phase 1 and 2,
1678      * removing all fields other than those specified as arguments to this method.
1679      * <p>
1680      * This instance is immutable and unaffected by this method call.
1681      *
1682      * @param resolverFields  the new set of resolver fields, null if no fields
1683      * @return a formatter based on this formatter with the requested resolver style, not null
1684      */
1685     public DateTimeFormatter withResolverFields(TemporalField... resolverFields) {
1686         Set<TemporalField> fields = null;
1687         if (resolverFields != null) {
1688             // Set.of cannot be used because it is hostile to nulls and duplicate elements
1689             fields = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(resolverFields)));
1690         }
1691         if (Objects.equals(this.resolverFields, fields)) {
1692             return this;
1693         }
1694         return new DateTimeFormatter(printerParser, locale, decimalStyle, resolverStyle, fields, chrono, zone);
1695     }
1696 
1697     /**
1698      * Returns a copy of this formatter with a new set of resolver fields.
1699      * <p>
1700      * This returns a formatter with similar state to this formatter but with
1701      * the resolver fields set. By default, a formatter has no resolver fields.
1702      * <p>
1703      * Changing the resolver fields only has an effect during parsing.
1704      * Parsing a text string occurs in two phases.
1705      * Phase 1 is a basic text parse according to the fields added to the builder.
1706      * Phase 2 resolves the parsed field-value pairs into date and/or time objects.
1707      * The resolver fields are used to filter the field-value pairs between phase 1 and 2.
1708      * <p>


< prev index next >