Skip to content

Instantly share code, notes, and snippets.

@erics
Last active February 6, 2023 07:13
Show Gist options
  • Save erics/8eada72001a03853cff741bdbe3815d5 to your computer and use it in GitHub Desktop.
Save erics/8eada72001a03853cff741bdbe3815d5 to your computer and use it in GitHub Desktop.
/**
* Transfer AD date to minguo date.
* 西元年 yyyyMMdd 轉 民國年 yyyMMdd
*
* @param dateString the String dateString
* @return the string
*/
public static String transferADDateToMinguoDate(String dateString) {
LocalDate localDate = LocalDate.parse(dateString, DateTimeFormatter.ofPattern("yyyyMMdd"));
return MinguoDate.from(localDate).format(DateTimeFormatter.ofPattern("yyyMMdd"));
}
/**
* Transfer minguo date to AD date.
* 民國年 yyyMMdd 轉 西元年 yyyyMMdd
*
* @param dateString the String dateString
* @return the string
*/
public static String transferMinguoDateToADDate(String dateString) {
Chronology chrono = MinguoChronology.INSTANCE;
DateTimeFormatter df = new DateTimeFormatterBuilder().parseLenient()
.appendPattern("yyyMMdd")
.toFormatter()
.withChronology(chrono)
.withDecimalStyle(DecimalStyle.of(Locale.getDefault()));
ChronoLocalDate chDate = chrono.date(df.parse(dateString));
return LocalDate.from(chDate).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment