created: 2022-07-24T14:49:06+08:00
updated: 2022-07-24T14:58:22+08:00
tags:
- DateTimeAPI
Non-ISO Date Conversion
Converting to a Non-ISO-Based Date
example1
LocalDateTime date = LocalDateTime.of(2013, Month.JULY, 20, 19, 30);
JapaneseDate jdate = JapaneseDate.from(date);
HijrahDate hdate = HijrahDate.from(date);
MinguoDate mdate = MinguoDate.from(date);
ThaiBuddhistDate tdate = ThaiBuddhistDate.from(date);
example2
/**
* Converts a LocalDate (ISO) value to a ChronoLocalDate date
* using the provided Chronology, and then formats the
* ChronoLocalDate to a String using a DateTimeFormatter with a
* SHORT pattern based on the Chronology and the current Locale.
*
* @param localDate - the ISO date to convert and format.
* @param chrono - an optional Chronology. If null, then IsoChronology is used.
*/
public static String toString(LocalDate localDate, Chronology chrono) {
if (localDate != null) {
Locale locale = Locale.getDefault(Locale.Category.FORMAT);
ChronoLocalDate cDate;
if (chrono == null) {
chrono = IsoChronology.INSTANCE;
}
try {
cDate = chrono.date(localDate);
} catch (DateTimeException ex) {
System.err.println(ex);
chrono = IsoChronology.INSTANCE;
cDate = localDate;
}
DateTimeFormatter dateFormatter =
DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
.withLocale(locale)
.withChronology(chrono)
.withDecimalStyle(DecimalStyle.of(locale));
String pattern = "M/d/yyyy GGGGG";
return dateFormatter.format(cDate);
} else {
return "";
}
}
use
LocalDate date = LocalDate.of(1996, Month.OCTOBER, 29);
System.out.printf("%s%n",
StringConverter.toString(date, JapaneseChronology.INSTANCE));
System.out.printf("%s%n",
StringConverter.toString(date, MinguoChronology.INSTANCE));
System.out.printf("%s%n",
StringConverter.toString(date, ThaiBuddhistChronology.INSTANCE));
System.out.printf("%s%n",
StringConverter.toString(date, HijrahChronology.INSTANCE));
result
10/29/0008 H
10/29/0085 1
10/29/2539 B.E.
6/16/1417 1
Converting to an ISO-Based Date
example1
LocalDate date = LocalDate.from(JapaneseDate.now());
example2
/**
* Parses a String to a ChronoLocalDate using a DateTimeFormatter
* with a short pattern based on the current Locale and the
* provided Chronology, then converts this to a LocalDate (ISO)
* value.
*
* @param text - the input date text in the SHORT format expected
* for the Chronology and the current Locale.
*
* @param chrono - an optional Chronology. If null, then IsoChronology
* is used.
*/
public static LocalDate fromString(String text, Chronology chrono) {
if (text != null && !text.isEmpty()) {
Locale locale = Locale.getDefault(Locale.Category.FORMAT);
if (chrono == null) {
chrono = IsoChronology.INSTANCE;
}
String pattern = "M/d/yyyy GGGGG";
DateTimeFormatter df = new DateTimeFormatterBuilder().parseLenient()
.appendPattern(pattern)
.toFormatter()
.withChronology(chrono)
.withDecimalStyle(DecimalStyle.of(locale));
TemporalAccessor temporal = df.parse(text);
ChronoLocalDate cDate = chrono.date(temporal);
return LocalDate.from(cDate);
}
return null;
}
use
System.out.printf("%s%n", StringConverter.fromString("10/29/0008 H",
JapaneseChronology.INSTANCE));
System.out.printf("%s%n", StringConverter.fromString("10/29/0085 1",
MinguoChronology.INSTANCE));
System.out.printf("%s%n", StringConverter.fromString("10/29/2539 B.E.",
ThaiBuddhistChronology.INSTANCE));
System.out.printf("%s%n", StringConverter.fromString("6/16/1417 1",
HijrahChronology.INSTANCE));
result
1996-10-29
1996-10-29
1996-10-29
1996-10-29
[[旧版日期时间]]