下面列出了怎么用java.time.format.FormatStyle的API类实例代码及写法,或者点击链接到github查看源代码。
public static void main(String[] args) {
DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
LocalDateTime currentDT = LocalDateTime.now();
System.out.println(dtf.format(currentDT));
ZonedDateTime gmt = ZonedDateTime.now(ZoneId.of("GMT+0"));
System.out.println(dtf.format(gmt));
ZonedDateTime ny = ZonedDateTime.now(ZoneId.of("America/New_York"));
System.out.println(dtf.format(ny));
Set<String> zones = ZoneId.getAvailableZoneIds();
Predicate<String> condition = str -> str.contains("London");
zones.forEach(z -> {
if(condition.test(z))
System.out.println(z);
});
}
@DataProvider(name="time")
Object[][] data_time() {
return new Object[][] {
{LocalTime.of(11, 30), FormatStyle.SHORT, DateFormat.SHORT, Locale.UK},
{LocalTime.of(11, 30), FormatStyle.SHORT, DateFormat.SHORT, Locale.US},
{LocalTime.of(11, 30), FormatStyle.SHORT, DateFormat.SHORT, Locale.FRANCE},
{LocalTime.of(11, 30), FormatStyle.SHORT, DateFormat.SHORT, Locale.JAPAN},
{LocalTime.of(11, 30), FormatStyle.MEDIUM, DateFormat.MEDIUM, Locale.UK},
{LocalTime.of(11, 30), FormatStyle.MEDIUM, DateFormat.MEDIUM, Locale.US},
{LocalTime.of(11, 30), FormatStyle.MEDIUM, DateFormat.MEDIUM, Locale.FRANCE},
{LocalTime.of(11, 30), FormatStyle.MEDIUM, DateFormat.MEDIUM, Locale.JAPAN},
// these localized patterns include "z" which isn't available from LocalTime
// {LocalTime.of(11, 30), FormatStyle.LONG, DateFormat.LONG, Locale.UK},
// {LocalTime.of(11, 30), FormatStyle.LONG, DateFormat.LONG, Locale.US},
// {LocalTime.of(11, 30), FormatStyle.LONG, DateFormat.LONG, Locale.FRANCE},
// {LocalTime.of(11, 30), FormatStyle.LONG, DateFormat.LONG, Locale.JAPAN},
//
// {LocalTime.of(11, 30), FormatStyle.FULL, DateFormat.FULL, Locale.UK},
// {LocalTime.of(11, 30), FormatStyle.FULL, DateFormat.FULL, Locale.US},
// {LocalTime.of(11, 30), FormatStyle.FULL, DateFormat.FULL, Locale.FRANCE},
// {LocalTime.of(11, 30), FormatStyle.FULL, DateFormat.FULL, Locale.JAPAN},
};
}
private static Stream<DateTimeFormatter> getFormatters() {
return Stream.of(
// English (Standard) Formats
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.LONG).withLocale(Locale.ENGLISH),
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.LONG).withLocale(Locale.ENGLISH),
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.LONG).withLocale(Locale.ENGLISH),
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.MEDIUM).withLocale(Locale.ENGLISH),
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.MEDIUM).withLocale(Locale.ENGLISH),
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.MEDIUM).withLocale(Locale.ENGLISH),
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.SHORT).withLocale(Locale.ENGLISH),
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT).withLocale(Locale.ENGLISH),
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.SHORT).withLocale(Locale.ENGLISH),
// ISO Formats
LOCAL_DATE_TIME_FORMATTER_WITH_SPACE_SEPARATOR,
DateTimeFormatter.ISO_LOCAL_DATE_TIME,
DateTimeFormatter.ISO_DATE_TIME
);
}
@PostConstruct
public void postConstruct(Composite parent, MPart part){
parent.setLayout(new GridLayout(1, false));
ITask task = (ITask) part.getTransientData().get("task");
part.setIconURI(TaskResultLabelProvider.getInstance().getIconURI(task));
String runAt;
LocalDateTime _runAt = task.getRunAt();
if(_runAt != null) {
runAt = _runAt.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL));
} else {
runAt = "queued";
}
String partLabel = task.getTaskDescriptor().getReferenceId() + " - " + runAt;
part.setLabel(partLabel);
Map<String, Object> e4Services = new HashMap<String, Object>();
e4Services.put(ECommandService.class.getName(), commandService);
e4Services.put(EHandlerService.class.getName(), handlerService);
taskResultDetailDialogContributions.createDetailCompositeForTask(parent, task, e4Services);
}
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate tomorrow = today.plus(1, ChronoUnit.DAYS);
LocalDate yesterday = tomorrow.minusDays(2);
System.out.println(today);
System.out.println(tomorrow);
System.out.println(yesterday);
LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4);
DayOfWeek dayOfWeek = independenceDay.getDayOfWeek();
System.out.println(dayOfWeek); // FRIDAY
DateTimeFormatter germanFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
.withLocale(Locale.GERMAN);
LocalDate xmas = LocalDate.parse("24.12.2014", germanFormatter);
System.out.println(xmas); // 2014-12-24
}
@Nonnull
public static FormatStyle toFormatStyle (final int nStyle)
{
switch (nStyle)
{
case DateFormat.FULL:
return FormatStyle.FULL;
case DateFormat.LONG:
return FormatStyle.LONG;
case DateFormat.MEDIUM:
return FormatStyle.MEDIUM;
case DateFormat.SHORT:
return FormatStyle.SHORT;
default:
throw new IllegalArgumentException ("Invalid style passed: " + nStyle);
}
}
@DataProvider(name="formatStyle")
Object[][] data_formatStyle() {
return new Object[][] {
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.FULL, "Tuesday, October 2, 2001 1:02:03 AM CEST Europe/Paris"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.LONG, "October 2, 2001 1:02:03 AM CEST Europe/Paris"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.MEDIUM, "Oct 2, 2001 1:02:03 AM Europe/Paris"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.SHORT, "10/2/01 1:02 AM Europe/Paris"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.FULL, "Tuesday, October 2, 2001 1:02:03 AM +02:00 +02:00"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.LONG, "October 2, 2001 1:02:03 AM +02:00 +02:00"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.MEDIUM, "Oct 2, 2001 1:02:03 AM +02:00"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.SHORT, "10/2/01 1:02 AM +02:00"},
};
}
@SuppressWarnings("deprecation")
@Test(dataProvider="date")
public void test_date_parse(LocalDate date, FormatStyle dateStyle, int dateStyleOld, Locale locale) {
DateFormat old = DateFormat.getDateInstance(dateStyleOld, locale);
Date oldDate = new Date(date.getYear() - 1900, date.getMonthValue() - 1, date.getDayOfMonth());
String text = old.format(oldDate);
DateTimeFormatter f = builder.appendLocalized(dateStyle, null).toFormatter(locale);
TemporalAccessor parsed = f.parse(text, pos);
assertEquals(pos.getIndex(), text.length());
assertEquals(pos.getErrorIndex(), -1);
assertEquals(LocalDate.from(parsed), date);
}
@SuppressWarnings("deprecation")
@Test(dataProvider="date")
public void test_date_parse(LocalDate date, FormatStyle dateStyle, int dateStyleOld, Locale locale) {
DateFormat old = DateFormat.getDateInstance(dateStyleOld, locale);
Date oldDate = new Date(date.getYear() - 1900, date.getMonthValue() - 1, date.getDayOfMonth());
String text = old.format(oldDate);
DateTimeFormatter f = builder.appendLocalized(dateStyle, null).toFormatter(locale);
TemporalAccessor parsed = f.parse(text, pos);
assertEquals(pos.getIndex(), text.length());
assertEquals(pos.getErrorIndex(), -1);
assertEquals(LocalDate.from(parsed), date);
}
@Test(dataProvider="chronoDateTimes")
public void test_formatterOfLocalizedDate(int year, int month, int day, String expected) {
HijrahDate hd = HijrahChronology.INSTANCE.date(year, month, day);
ChronoLocalDateTime<HijrahDate> hdt = hd.atTime(LocalTime.NOON);
hdt = hdt.plus(1, ChronoUnit.YEARS);
hdt = hdt.plus(1, ChronoUnit.MONTHS);
hdt = hdt.plus(1, ChronoUnit.DAYS);
hdt = hdt.plus(1, ChronoUnit.HOURS);
hdt = hdt.plus(1, ChronoUnit.MINUTES);
hdt = hdt.plus(1, ChronoUnit.SECONDS);
DateTimeFormatter df = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).withChronology(Chronology.of("Hijrah-umalqura")).withLocale(Locale.US);
assertEquals(df.format(hdt), expected);
}
@DataProvider
public static Object[][] data_formatStyle() {
return new Object[][] {
// Android-changed: date/time patterns changed in new CLDR; adapt to UK locale.
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.FULL, "Tuesday, 2 October 2001 at 01:02:03 Central European Summer Time Europe/Paris"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.LONG, "2 October 2001 at 01:02:03 CEST Europe/Paris"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.MEDIUM, "2 Oct 2001, 01:02:03 Europe/Paris"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.SHORT, "02/10/2001, 01:02 Europe/Paris"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.FULL, "Tuesday, 2 October 2001 at 01:02:03 +02:00 +02:00"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.LONG, "2 October 2001 at 01:02:03 +02:00 +02:00"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.MEDIUM, "2 Oct 2001, 01:02:03 +02:00"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.SHORT, "02/10/2001, 01:02 +02:00"},
};
}
@SuppressWarnings("deprecation")
@Test(dataProvider="date")
public void test_date_print(LocalDate date, FormatStyle dateStyle, int dateStyleOld, Locale locale) {
DateFormat old = DateFormat.getDateInstance(dateStyleOld, locale);
Date oldDate = new Date(date.getYear() - 1900, date.getMonthValue() - 1, date.getDayOfMonth());
String text = old.format(oldDate);
DateTimeFormatter f = builder.appendLocalized(dateStyle, null).toFormatter(locale);
String formatted = f.format(date);
assertEquals(formatted, text);
}
@SuppressWarnings("deprecation")
@Test(dataProvider="time")
public void test_time_print(LocalTime time, FormatStyle timeStyle, int timeStyleOld, Locale locale) {
DateFormat old = DateFormat.getTimeInstance(timeStyleOld, locale);
Date oldDate = new Date(1970, 0, 0, time.getHour(), time.getMinute(), time.getSecond());
String text = old.format(oldDate);
DateTimeFormatter f = builder.appendLocalized(null, timeStyle).toFormatter(locale);
String formatted = f.format(time);
assertEquals(formatted, text);
}
@SuppressWarnings("deprecation")
@Test(dataProvider="time")
public void test_time_parse(LocalTime time, FormatStyle timeStyle, int timeStyleOld, Locale locale) {
DateFormat old = DateFormat.getTimeInstance(timeStyleOld, locale);
Date oldDate = new Date(1970, 0, 0, time.getHour(), time.getMinute(), time.getSecond());
String text = old.format(oldDate);
DateTimeFormatter f = builder.appendLocalized(null, timeStyle).toFormatter(locale);
TemporalAccessor parsed = f.parse(text, pos);
assertEquals(pos.getIndex(), text.length());
assertEquals(pos.getErrorIndex(), -1);
assertEquals(LocalTime.from(parsed), time);
}
@Test(dataProvider="format_data")
public void test_parseLocalizedText(Chronology chrono, Locale formatLocale, Locale numberingLocale,
ChronoLocalDate expected, String text) {
DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
.withChronology(chrono).withLocale(formatLocale)
.withDecimalStyle(DecimalStyle.of(numberingLocale));
TemporalAccessor temporal = dtf.parse(text);
ChronoLocalDate date = chrono.date(temporal);
assertEquals(date, expected);
}
@DataProvider(name="formatStyle")
Object[][] data_formatStyle() {
return new Object[][] {
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.FULL, "Tuesday, October 2, 2001 1:02:03 AM CEST Europe/Paris"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.LONG, "October 2, 2001 1:02:03 AM CEST Europe/Paris"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.MEDIUM, "Oct 2, 2001 1:02:03 AM Europe/Paris"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.SHORT, "10/2/01 1:02 AM Europe/Paris"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.FULL, "Tuesday, October 2, 2001 1:02:03 AM +02:00 +02:00"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.LONG, "October 2, 2001 1:02:03 AM +02:00 +02:00"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.MEDIUM, "Oct 2, 2001 1:02:03 AM +02:00"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.SHORT, "10/2/01 1:02 AM +02:00"},
};
}
private DateTimeFormatter getFormatter(ZoneId zoneId) {
DateTimeFormatter formatter;
if (showTime) {
formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
} else formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
return formatter.withLocale(getLocale())
.withZone(convertTimeZone&&zoneId!=null
?zoneId
:OrienteerWebSession.get().getDatabase().getStorage()
.getConfiguration().getTimeZone().toZoneId());
}
@Test(dataProvider="format_data")
public void test_formatLocalizedDate(Chronology chrono, Locale formatLocale, Locale numberingLocale,
ChronoLocalDate date, String expected) {
DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
.withChronology(chrono).withLocale(formatLocale)
.withDecimalStyle(DecimalStyle.of(numberingLocale));
String text = dtf.format(date);
assertEquals(text, expected);
}
void updateValue() {
if (is24HourView) {
LocalTimeStringConverter localTimeStringConverter =
new LocalTimeStringConverter(FormatStyle.SHORT, Locale.GERMAN);
timePicker.setValue(localTimeStringConverter.fromString(selectedHourLabel.getText()
+ ":" + selectedMinLabel.getText()));
} else {
timePicker.setValue(LocalTime.parse(selectedHourLabel.getText() + ":" + selectedMinLabel.getText() + " " + period.get(), DateTimeFormatter.ofPattern("h:mm a").withLocale(Locale.ENGLISH)));
}
}
public static String toHumanEpoch(LocalDateTime dt) {
String formatted = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL)
.format(dt.atZone(ZoneId.systemDefault()));
String formattedUTC = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL)
.format(dt.atZone(ZoneId.systemDefault()).withZoneSameInstant(ZoneOffset.UTC));
return "Local Time:\n" + formatted + "\nGMT:\n" + formattedUTC;
}
@SuppressWarnings("deprecation")
@Test(dataProvider="date")
public void test_date_parse(LocalDate date, FormatStyle dateStyle, int dateStyleOld, Locale locale) {
DateFormat old = DateFormat.getDateInstance(dateStyleOld, locale);
Date oldDate = new Date(date.getYear() - 1900, date.getMonthValue() - 1, date.getDayOfMonth());
String text = old.format(oldDate);
DateTimeFormatter f = builder.appendLocalized(dateStyle, null).toFormatter(locale);
TemporalAccessor parsed = f.parse(text, pos);
assertEquals(pos.getIndex(), text.length());
assertEquals(pos.getErrorIndex(), -1);
assertEquals(LocalDate.from(parsed), date);
}
@Test(dataProvider = "formatStyle")
public void test_formatStyle(Temporal temporal, FormatStyle style, String formattedStr) {
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
DateTimeFormatter formatter = builder.appendLocalized(style, style).appendLiteral(" ").appendZoneOrOffsetId().toFormatter();
formatter = formatter.withLocale(Locale.US);
assertEquals(formatter.format(temporal), formattedStr);
}
@SuppressWarnings("deprecation")
@Test(dataProvider="time")
public void test_time_parse(LocalTime time, FormatStyle timeStyle, int timeStyleOld, Locale locale) {
DateFormat old = DateFormat.getTimeInstance(timeStyleOld, locale);
Date oldDate = new Date(1970, 0, 0, time.getHour(), time.getMinute(), time.getSecond());
String text = old.format(oldDate);
DateTimeFormatter f = builder.appendLocalized(null, timeStyle).toFormatter(locale);
TemporalAccessor parsed = f.parse(text, pos);
assertEquals(pos.getIndex(), text.length());
assertEquals(pos.getErrorIndex(), -1);
assertEquals(LocalTime.from(parsed), time);
}
@DataProvider(name="formatStyle")
Object[][] data_formatStyle() {
return new Object[][] {
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.FULL, "Tuesday, October 2, 2001 1:02:03 AM CEST Europe/Paris"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.LONG, "October 2, 2001 1:02:03 AM CEST Europe/Paris"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.MEDIUM, "Oct 2, 2001 1:02:03 AM Europe/Paris"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), ZONEID_PARIS), FormatStyle.SHORT, "10/2/01 1:02 AM Europe/Paris"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.FULL, "Tuesday, October 2, 2001 1:02:03 AM +02:00 +02:00"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.LONG, "October 2, 2001 1:02:03 AM +02:00 +02:00"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.MEDIUM, "Oct 2, 2001 1:02:03 AM +02:00"},
{ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 1, 2, 3), OFFSET_PTWO), FormatStyle.SHORT, "10/2/01 1:02 AM +02:00"},
};
}
@SuppressWarnings("deprecation")
@Test(dataProvider="time")
public void test_time_parse(LocalTime time, FormatStyle timeStyle, int timeStyleOld, Locale locale) {
DateFormat old = DateFormat.getTimeInstance(timeStyleOld, locale);
Date oldDate = new Date(1970, 0, 0, time.getHour(), time.getMinute(), time.getSecond());
String text = old.format(oldDate);
DateTimeFormatter f = builder.appendLocalized(null, timeStyle).toFormatter(locale);
TemporalAccessor parsed = f.parse(text, pos);
assertEquals(pos.getIndex(), text.length());
assertEquals(pos.getErrorIndex(), -1);
assertEquals(LocalTime.from(parsed), time);
}
@Test
public void shouldPrintStyledDateTime() {
LocalDateTime anotherSummerDay = LocalDateTime.of(2016, 8, 23, 13, 12, 45);
Assert.assertEquals("Tuesday, August 23, 2016 1:12:45 PM EET", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay));
Assert.assertEquals("August 23, 2016 1:12:45 PM EET", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay));
Assert.assertEquals("Aug 23, 2016 1:12:45 PM", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay));
Assert.assertEquals("8/23/16 1:12 PM", DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withZone(ZoneId.of("Europe/Helsinki")).format(anotherSummerDay));
}
@Test(dataProvider="format_data")
public void test_formatLocalizedDate(Chronology chrono, Locale formatLocale, Locale numberingLocale,
ChronoLocalDate date, String expected) {
DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
.withChronology(chrono).withLocale(formatLocale)
.withDecimalStyle(DecimalStyle.of(numberingLocale));
String text = dtf.format(date);
assertEquals(text, expected);
}
@Test(dataProvider="format_data")
public void test_parseLocalizedText(Chronology chrono, Locale formatLocale, Locale numberingLocale,
ChronoLocalDate expected, String text) {
DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
.withChronology(chrono).withLocale(formatLocale)
.withDecimalStyle(DecimalStyle.of(numberingLocale));
TemporalAccessor temporal = dtf.parse(text);
ChronoLocalDate date = chrono.date(temporal);
assertEquals(date, expected);
}
@Test(dataProvider="chronoDateTimes")
public void test_formatterOfLocalizedDate(int year, int month, int day, String expected) {
HijrahDate hd = HijrahChronology.INSTANCE.date(year, month, day);
ChronoLocalDateTime<HijrahDate> hdt = hd.atTime(LocalTime.NOON);
hdt = hdt.plus(1, ChronoUnit.YEARS);
hdt = hdt.plus(1, ChronoUnit.MONTHS);
hdt = hdt.plus(1, ChronoUnit.DAYS);
hdt = hdt.plus(1, ChronoUnit.HOURS);
hdt = hdt.plus(1, ChronoUnit.MINUTES);
hdt = hdt.plus(1, ChronoUnit.SECONDS);
DateTimeFormatter df = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).withChronology(Chronology.of("Hijrah-umalqura")).withLocale(Locale.US);
assertEquals(df.format(hdt), expected);
}
@Nullable
private FormatStyle convertStyleCharacter(char c) {
switch (c) {
case 'S': return FormatStyle.SHORT;
case 'M': return FormatStyle.MEDIUM;
case 'L': return FormatStyle.LONG;
case 'F': return FormatStyle.FULL;
case '-': return null;
default: throw new IllegalArgumentException("Invalid style character '" + c + "'");
}
}