job*_*bin 4 java datetime-format
我正在尝试将格式2019-12-30的日期转换30-12-2019为带格式的日期,为此我想到了使用DateTimeFormatter,我有以下代码:
LocalDate date = LocalDate.parse("2019-12-30");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-YYYY");
System.out.println(formatter.format(date));
Run Code Online (Sandbox Code Playgroud)
然而,令我惊讶的是,这会返回一个输出:
30-12-2020
Run Code Online (Sandbox Code Playgroud)
而不是预期的30-12-2019。如果日期设置为2019-11-30,它会正确返回30-11-2019。我正在做一些非常错误的事情,但我无法弄清楚到底是什么。有人可以帮忙吗?
从DateTimeFormatter文档:,指示符号,含义和示例:
Y 以周为基础的年份 1996 年;96
因此,您正在格式化基于周的年份,而不是常规年份。2019 年 12 月 30 日属于 2020 年的第一周,因此您的输出。
使用yyyy(year-of-era) 或uuuu(year) 代替,YYYY您将得到 2019。
基本上,YYYY通常应该与w(week-of-week-based-year) 和E(day-of-week) 一起使用。
| 归档时间: |
|
| 查看次数: |
804 次 |
| 最近记录: |