他妈的格式化为 LocalDate

joda formatting a LocalDate

我有以下创建 LocalDate 的代码。当我对其调用 toString 方法时,输出为 2016-01-05.

我希望日期采用以下格式。 2016 年 1 月 5 日

int dayInt = Integer.parseInt(dayStr);
            int monthInt = Integer.parseInt(monthStr);
            int yearInt = Integer.parseInt(yearStr);

            LocalDate ldt = new LocalDate(yearInt, monthInt, dayInt);

            Log.e(TAG, "LocalDate = " + ldt.toString()); //eg 2016-01-05


            LocalDate localDate2 = LocalDate.parse(ldt.toString(), DateTimeFormat.forPattern("dd-MMM-yy"));

.

我遇到以下异常,有人知道我该怎么做吗?

提前致谢。

01-06 14:34:13.903: E/CustomExceptionHandler(8590): stack = java.lang.IllegalArgumentException: Invalid format: "2016-01-04" is malformed at "16-01-04"
01-06 14:34:13.903: E/CustomExceptionHandler(8590):     at org.joda.time.format.DateTimeFormatter.parseLocalDateTime(DateTimeFormatter.java:821)
01-06 14:34:13.903: E/CustomExceptionHandler(8590):     at org.joda.time.format.DateTimeFormatter.parseLocalDate(DateTimeFormatter.java:765)
01-06 14:34:13.903: E/CustomExceptionHandler(8590):     at org.joda.time.LocalDate.parse(LocalDate.java:178)

年份格式不正确。更改行:

LocalDate localDate2 = LocalDate.parse(ldt.toString(), DateTimeFormat.forPattern("dd-MMM-yy"));

至:

LocalDate localDate2 = LocalDate.parse(ldt.toString(), DateTimeFormat.forPattern("dd-MMM-yyyy"));

表达式 ldt.toString() 产生“2016-01-05”。

所以您的格式模式应该是:“yyyy-MM-dd”但是您从日期开始您的模式只需要两位数字(如错误消息所示)。

你说:

I would like the date to be in the following format. 05-Jan-2016

然后你必须格式化它(而不是解析它):

String s = DateTimeFormat.forPattern("dd-MMM-yyyy").withLocale(Locale.ENGLISH).print(ldt);