DateTimeFormatter Support for Single Digit Day of Month and Month of Year

后端 未结 2 1417
無奈伤痛
無奈伤痛 2020-12-13 16:59

DateTimeFormmater doesn\'t seem to handle single digit day of the month:

String format = \"MM/dd/yyyy\";
String date   = \"5/3/1969\";
System.ou         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 17:46

    From the documentation:

    Number: If the count of letters is one, then the value is output using the minimum number of digits and without padding.

    So the format specifier you want is M/d/yyyy, using single letter forms. Of course, it will still parse date Strings like "12/30/1969" correctly as for these day/month values, two digits are the “minimum number of digits”.

    The important difference is that MM and dd require zero padding, not that M and d can’t handle values greater than 9 (that would be a bit… unusual).

提交回复
热议问题