java中日期格式化的大坑
我们都知道在java中进行日期格式化使用simpledateformat。通过格式 yyyy-MM-dd 等来进行格式化,但是你知道其中微小的坑吗?
yyyy 和 YYYY示例代码
@Test public void testWeekBasedYear() { Calendar calendar = Calendar.getInstance(); // 2019-12-31 calendar.set(2019, Calendar.DECEMBER, 31); Date strDate1 = calendar.getTime(); // 2020-01-01 calendar.set(2020, Calendar.JANUARY, 1); Date strDate2 = calendar.getTime(); // 大写 YYYY SimpleDateFormat formatYYYY = new SimpleDateFormat('YYYY/MM/dd'); System.out.println('2019-12-31 转 YYYY/MM/dd 格式: ' + formatYYYY.format(strDate1)); System.out.println('2020-01-01 转 YYYY/MM/dd 格式: ' + formatYYYY.format(strDate2)); // 小写 YYYY SimpleDateFormat formatyyyy = new SimpleDateFormat('yyyy/MM/dd'); System.out.println('2019-12-31 转 yyyy/MM/dd 格式: ' + formatyyyy.format(strDate1)); System.out.println('2020-01-01 转 yyyy/MM/dd 格式: ' + formatyyyy.format(strDate2)); }
输出的结果
2019-12-31 转 YYYY/MM/dd 格式: 2020/12/312020-01-01 转 YYYY/MM/dd 格式: 2020/01/012019-12-31 转 yyyy/MM/dd 格式: 2019/12/312020-01-01 转 yyyy/MM/dd 格式: 2020/01/01
卧槽?2019变成2020了?
YYYY 是怎么做到的呢Java’s DateTimeFormatter pattern 'YYYY' gives you the week-based-year, (by default, ISO-8601 standard) the year of the Thursday of that week.下面就是用YYYY格式化代码
12/29/2019 将会格式化到2019年 这一周还属于2019年12/30/2019 将会格式化到2020年 这一周已经属于2020年看字说话YYYY,week-based year 是 ISO 8601 规定的。2019-12-31号这一天,按周算年份已经属于2020年了,格式化之后就变成2020年,后面的月份日期不变。
dd和DD示例代码
private static void tryit(int Y, int M, int D, String pat) { DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pat); LocalDate dat = LocalDate.of(Y,M,D); String str = fmt.format(dat); System.out.printf('Y=%04d M=%02d D=%02d ' + 'formatted with ' + ''%s' -> %sn',Y,M,D,pat,str); } public static void main(String[] args){ tryit(2020,01,20,'MM/DD/YYYY'); tryit(2020,01,21,'DD/MM/YYYY'); tryit(2020,01,22,'YYYY-MM-DD'); tryit(2020,03,17,'MM/DD/YYYY'); tryit(2020,03,18,'DD/MM/YYYY'); tryit(2020,03,19,'YYYY-MM-DD'); }
输出结果
Y=2020 M=01 D=20 formatted with 'MM/DD/YYYY' -> 01/20/2020Y=2020 M=01 D=21 formatted with 'DD/MM/YYYY' -> 21/01/2020Y=2020 M=01 D=22 formatted with 'YYYY-MM-DD' -> 2020-01-22Y=2020 M=03 D=17 formatted with 'MM/DD/YYYY' -> 03/77/2020Y=2020 M=03 D=18 formatted with 'DD/MM/YYYY' -> 78/03/2020Y=2020 M=03 D=19 formatted with 'YYYY-MM-DD' -> 2020-03-79
这里的大写的DD代表的是处于这一年中那一天,不是处于这个月的那一天,但是dd就没有问题。
结论格式化日期一定要选用 yyyy-MM-dd哦!
到此这篇关于java中日期格式化的大坑的文章就介绍到这了,更多相关java 日期格式化内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!
相关文章:

网公网安备