title: Localize string from `MonthDay` or `YearMonth` classes in java.time?
body: <p>The java.time classes built into Java 8 and later offer the <code>MonthDay</code> and <code>YearMonth</code> classes. Their <code>toString</code> and <code>parse</code> methods use standard <a href="https://en.wikipedia.org/wiki/ISO_8601" rel="nofollow noreferrer">ISO 8601</a> formats (<code>--MM-DD</code> &amp; <code>YYYY-MM</code>), which is wise. </p>
  
  <p>For presentation to humans, the standard formats may not be suitable. Is there anyway to generate an automatically localized string to represent the values in objects of either <code>MonthDay</code> or <code>YearMonth</code>? </p>
  
  <p>For example, in the United States users might typically want MM/DD for month-day and MM/YY for year-month. While in the UK users might want DD/MM for month-day. </p>
  
  <p>Anyway to automate such variations by <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Locale.html" rel="nofollow noreferrer"><code>Locale</code></a> rather than explicitly define formatting patterns?</p>
  
  <hr>
  
  <p>I tried the following code, using a date-oriented localizing formatter.</p>
  
  <pre><code>Locale l = Locale.US;
  DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate ( FormatStyle.SHORT ).withLocale ( l );
  
  YearMonth ym = YearMonth.of ( 2017 , Month.JANUARY );
  MonthDay md = MonthDay.of ( Month.JANUARY , 29 );
  
  String outputYm = ym.format ( f );
  String outputMd = md.format ( f );
  </code></pre>
  
  <p>That code fails, throwing an exception when used for either <code>YearMonth</code> or <code>MonthDay</code>.</p>
  
  <p>For <code>YearMonth</code>:</p>
  
  <pre class="lang-none prettyprint-override"><code>Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: DayOfMonth
      at java.time.YearMonth.getLong(YearMonth.java:494)
      at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
      at java.time.format.DateTimeFormatterBuilder$NumberPrinterParser.format(DateTimeFormatterBuilder.java:2540)
      at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2179)
      at java.time.format.DateTimeFormatterBuilder$LocalizedPrinterParser.format(DateTimeFormatterBuilder.java:4347)
      at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2179)
      at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1746)
      at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1720)
      at java.time.YearMonth.format(YearMonth.java:1073)
      at javatimestuff.App.doIt(App.java:56)
      at javatimestuff.App.main(App.java:45)
  </code></pre>
  
  <p>For <code>MonthDay</code>:</p>
  
  <pre class="lang-none prettyprint-override"><code>Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: YearOfEra
      at java.time.MonthDay.getLong(MonthDay.java:451)
      at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
      at java.time.format.DateTimeFormatterBuilder$NumberPrinterParser.format(DateTimeFormatterBuilder.java:2540)
      at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2179)
      at java.time.format.DateTimeFormatterBuilder$LocalizedPrinterParser.format(DateTimeFormatterBuilder.java:4347)
      at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2179)
      at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1746)
      at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1720)
      at java.time.MonthDay.format(MonthDay.java:646)
      at javatimestuff.App.doIt(App.java:57)
      at javatimestuff.App.main(App.java:45)
  </code></pre>