DataFormat { @NonNull public DateFormat newDateFormat() throws IllegalArgumentException { DateFormat result = !datePattern.isEmpty() ? new SimpleDateFormat(datePattern, getLocaleOrDefault()) : SimpleDateFormat.getDateInstance(DateFormat.DEFAULT, getLocaleOrDefault()); result.setLenient(datePattern.isEmpty() && locale == null); return result; } private  DataFormat(Locale locale, String datePattern, String numberPattern, Void fake); @Deprecated  DataFormat(@Nullable Locale locale, @Nullable String datePattern); @Deprecated  DataFormat(@Nullable Locale locale, @Nullable String datePattern, @Nullable String numberPattern); @NonNull static DataFormat of(@Nullable Locale locale, @Nullable String datePattern, @Nullable String numberPattern); @NonNull String getNumberPattern(); @NonNull String getDatePattern(); @Nullable Locale getLocale(); @NonNull String getLocaleString(); @Override String toString(); @Override boolean equals(Object obj); @Override int hashCode(); Formatters.@NonNull Formatter<Date> dateFormatter(); Parsers.@NonNull Parser<Date> dateParser(); Formatters.@NonNull Formatter<Number> numberFormatter(); Parsers.@NonNull Parser<Number> numberParser(); @NonNull DateFormat newDateFormat(); @NonNull NumberFormat newNumberFormat(); @Deprecated @NonNull static DataFormat create(@Nullable String locale, @Nullable String datePattern); @Deprecated @NonNull static DataFormat create(@Nullable String locale, @Nullable String datePattern, @Nullable String numberPattern); @Deprecated @NonNull static Parser<Locale> localeParser(); @Deprecated @NonNull static Locale toLocale(@NonNull String str); @Deprecated @Nullable String previewDate(); @Deprecated @NonNull static DateFormat newDateFormat(@Nullable String datePattern, @Nullable Locale locale); @Deprecated @NonNull static NumberFormat newNumberFormat(@Nullable Locale locale); static final DataFormat DEFAULT; static final DataFormat ROOT; static final String SEP; }
@Test public void testNewDateFormat() throws ParseException { assertThatIllegalArgumentException().isThrownBy(() -> of(FRENCH, "c", null).newDateFormat()); assertThatIllegalArgumentException().isThrownBy(() -> of(null, "c", null).newDateFormat()); Date date = DateUtil.parse("2000-01-01"); DateFormat f1 = of(FRANCE, null, null).newDateFormat(); assertThat(f1.format(date)).isEqualTo("1 janv. 2000"); assertThat(f1.parse("1 janv. 2000")).isEqualTo(date); DateFormat f2 = of(US, null, null).newDateFormat(); assertThat(f2.format(date)).isEqualTo("Jan 1, 2000"); assertThat(f2.parse("Jan 1, 2000")).isEqualTo(date); DateFormat f3 = of(FRANCE, "yyyy-MMM", null).newDateFormat(); assertThat(f3.format(date)).isEqualTo("2000-janv."); assertThat(f3.parse("2000-janv.")).isEqualTo(date); DateFormat f4 = of(US, "yyyy-MMM", null).newDateFormat(); assertThat(f4.format(date)).isEqualTo("2000-Jan"); assertThat(f4.parse("2000-Jan")).isEqualTo(date); BiConsumer<DataFormat, DateFormat> equivalence = (o, r) -> { try { DateFormat l = o.newDateFormat(); assertThat(l.parseObject(r.format(date))).isEqualTo(date); assertThat(r.parseObject(l.format(date))).isEqualTo(date); } catch (ParseException ex) { throw new RuntimeException(ex); } }; equivalence.accept(of(FRANCE, "yyyy-MMM", null), new SimpleDateFormat("yyyy-MMM", FRANCE)); equivalence.accept(of(null, "yyyy-MMM", null), new SimpleDateFormat("yyyy-MMM")); equivalence.accept(of(FRANCE, null, null), SimpleDateFormat.getDateInstance(DateFormat.DEFAULT, FRANCE)); equivalence.accept(of(null, null, null), SimpleDateFormat.getDateInstance()); }