java - Formatting MonthDay using DateTimeFormatter.ofLocalizedDate -
this question has answer here:
i trying format monthday
object in way not have specify order. trying use localized datetimeformatter
.
i have code:
localdate datetime = localdate.parse("2017-08-11", datetimeformatter.ofpattern("yyyy-mm-dd")); monthday monthday = monthday.from(datetime); system.out.println(monthday.format(datetimeformatter.ofpattern("mmmm dd").withlocale(locale.english))); system.out.println(monthday.format(datetimeformatter.ofpattern("mmmm dd").withlocale(locale.germany))); system.out.println(monthday.format(datetimeformatter.ofpattern("mmmm dd").withlocale(locale.forlanguagetag("uk")))); system.out.println(datetime.format(datetimeformatter.oflocalizeddate(formatstyle.medium).withlocale(locale.english))); system.out.println(datetime.format(datetimeformatter.oflocalizeddate(formatstyle.medium).withlocale(locale.forlanguagetag("uk")))); // next line throws exception java.time.temporal.unsupportedtemporaltypeexception: unsupported field: yearofera system.out.println(monthday.format(datetimeformatter.oflocalizeddate(formatstyle.medium).withlocale(locale.forlanguagetag("uk"))));
the first 3 prints print expected translated month , day, month , day. not change order because explicitly telling order.
the next 2 (before exception) print respectively:
aug 11, 2017 11 серп. 2017
notice how day either before or after month depending on locale passed function. how do monthday
object last line throws exception when done in way.
the other answers given far describe limitations of standard datetimeformatter
, see unsolved related jdk-issue. suggested workaround edit localized date pattern removing "y" etc. tricky , might not work locales due existence of other localized literals inside pattern.
however, might consider using external libraries have stronger focus on internationalization issues , have capability format month-day-object using locale information. locale determines order of field components , dots, spaces or other special literals (like in chinese).
here 2 options necessary type conversions related system timezone:
icu4j
monthday md = monthday.now(); gregoriancalendar gcal = new gregoriancalendar( 2000, // avoids possible leap year problems md.getmonthvalue() - 1, md.getdayofmonth() ); dateformat df = dateformat.getinstanceforskeleton( dateformat.abbr_month_day, locale.forlanguagetag("en") ); system.out.println(df.format(gcal.gettime())); // aug 15 dateformat df2 = dateformat.getinstanceforskeleton( dateformat.abbr_month_day, locale.forlanguagetag("de") ); system.out.println(df2.format(gcal.gettime())); // 15. aug. dateformat df3 = dateformat.getinstanceforskeleton(dateformat.month_day, locale.forlanguagetag("zh")); system.out.println(df3.format(gcal.gettime())); // 8月15日
time4j
monthday md = monthday.now(); chronoformatter<annualdate> cf1 = chronoformatter.ofstyle(displaymode.short, locale.german, annualdate.chronology()); system.out.println(cf1.format(annualdate.from(md))); // 15.8. chronoformatter<annualdate> cf2 = chronoformatter.ofstyle(displaymode.medium, locale.german, annualdate.chronology()); system.out.println(cf2.format(annualdate.from(md))); // 15.08. chronoformatter<annualdate> cf3 = chronoformatter.ofstyle(displaymode.long, locale.english, annualdate.chronology()); system.out.println(cf3.format(annualdate.from(md))); // aug 15 chronoformatter<annualdate> cf4 = chronoformatter.ofstyle(displaymode.full, locale.german, annualdate.chronology()); system.out.println(cf4.format(annualdate.from(md))); // 15. august chronoformatter<annualdate> cf5 = chronoformatter.ofstyle(displaymode.full, locale.chinese, annualdate.chronology()); system.out.println(cf5.format(annualdate.from(md))); // 8月15日
disclaimer: time4j has been written myself fill gaps or improve other features of jsr-310 (java.time-package).
Comments
Post a Comment