/** * Obtains an instance of {@code LocalDate} from a text string such as {@code 2007-12-03}. * * The string must represent a valid date and is parsed using * {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE}. * * @param text the text to parse such as "2007-12-03", not null * @return the parsed local date, not null * @throws DateTimeParseException if the text cannot be parsed */publicstatic LocalDate parse(CharSequence text) {return parse(text, DateTimeFormatter.ISO_LOCAL_DATE);}/** * Obtains an instance of {@code LocalDate} from a text string using a specific formatter. * * The text is parsed using the formatter, returning a date. * * @param text the text to parse, not null * @param formatter the formatter to use, not null * @return the parsed local date, not null * @throws DateTimeParseException if the text cannot be parsed */publicstatic LocalDate parse(CharSequence text, DateTimeFormatter formatter) { Objects.requireNonNull(formatter, "formatter");return formatter.parse(text, LocalDate::from);}
卖关子好累,不卖了。
LocalDate.parse 方法有两个,区别就是指没指定 DateTimeFormatter。
很明显上面的没指定,下面那个指定了。
/** * Creates a formatter using the specified pattern. * * This method will create a formatter based on a simple * pattern of letters and symbols * as described in the class documentation. * For example, {@code d MMM uuuu} will format 2011-12-03 as '3 Dec 2011'. * * The formatter will use the {@link Locale#getDefault(Locale.Category) default FORMAT locale}. * This can be changed using {@link DateTimeFormatter#withLocale(Locale)} on the returned formatter * Alternatively use the {@link #ofPattern(String, Locale)} variant of this method. * * The returned formatter has no override chronology or zone. * It uses {@link ResolverStyle#SMART SMART} resolver style. * * @param pattern the pattern to use, not null * @return the formatter based on the pattern, not null * @throws IllegalArgumentException if the pattern is invalid * @see DateTimeFormatterBuilder#appendPattern(String) */publicstatic DateTimeFormatter ofPattern(String pattern){returnnew DateTimeFormatterBuilder().appendPattern(pattern).toFormatter();}
划重点:
It uses {@link ResolverStyle#SMART SMART} resolver style.
publicenumResolverStyle{/** * Style to resolve dates and times strictly. * * Using strict resolution will ensure that all parsed values are within * the outer range of valid values for the field. Individual fields may * be further processed for strictness. * * For example, resolving year-month and day-of-month in the ISO calendar * system using strict mode will ensure that the day-of-month is valid * for the year-month, rejecting invalid values. */STRICT,/** * Style to resolve dates and times in a smart, or intelligent, manner. * * Using smart resolution will perform the sensible default for each * field, which may be the same as strict, the same as lenient, or a third * behavior. Individual fields will interpret this differently. * * For example, resolving year-month and day-of-month in the ISO calendar * system using smart mode will ensure that the day-of-month is from * 1 to 31, converting any value beyond the last valid day-of-month to be * the last valid day-of-month. */SMART,/** * Style to resolve dates and times leniently. * * Using lenient resolution will resolve the values in an appropriate * lenient manner. Individual fields will interpret this differently. * * For example, lenient mode allows the month in the ISO calendar system * to be outside the range 1 to 12. * For example, month 15 is treated as being 3 months after month 12. */LENIENT;}
怎么个智能法呢?
1 to 31, converting any value beyond the last valid day-of-month to be the last valid day-of-month.