与SimpleDateFormat转换日期混淆 [英] Confused with Converting the date with SimpleDateFormat

查看:169
本文介绍了与SimpleDateFormat转换日期混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的日期转换有问题。我使用下面的程序,我期望输出: 19.05.2017



但输出是: 05.00.2017



任何人都可以帮忙吗?

 字符串t =5月19日星期五00:00:00 CEST 2017; 
Date d = new SimpleDateFormat(EEE MMM DD hh:mm:ss zzzz YYYY,Locale.US).parse(t);
String s = new SimpleDateFormat(dd.mm.yyyy)。format(d).toString();
System.out.println(s);


解决方案

令人惊讶的结果。老式的类 SimpleDateFormat 和朋友们都充满了惊喜。这是一个负面的事情。



大写 DD 是一年中的一天。小写 hh 是AM或PM(1到12)的小时。大写 YYYY 是以星期为基准的年份(仅用于星期数)。所以你要求的是一个五月的星期五和一年的第十九天的日期。显然,这是不可能的。

解析的结果是 Thu Jan 05 23:00:00 CET 2017 。显然 SimpleDateFormat opts给你一个星期五和使用由 CEST 隐含的2小时区域偏移量,即使日期它选择的不是在CEST(夏令时)正在使用的年份。我不知道它是否仅仅给出了本周的第一个星期五(每年第1周的星期五)。星期五在0000小时抵消GMT + 2等于星期四在23 GMT + 1,即CET。



接下来是格式化,05是预期的日期,但是小写 mm 表示分钟。由于分钟数为0,因此您会得到 00 。你得到了正确的一年。



我不同意使用过时的类给你带来这样的惊喜,我同意 Sam的答案,您应该使用 java.time 中的新类:

  ZonedDateTime dt = ZonedDateTime.parse(t,
DateTimeFormatter.ofPattern(EEE MMM dd HH:mm:ss zzz yyyy,Locale.US));
String s = dt.format(DateTimeFormatter.ofPattern(dd.MM.uuuu));

此代码为您提供 19.05.2017 as你曾预料过。现代类的好处之一是,如果你试图用原始格式模式字符串解析,你将得到一个 DateTimeParseException ,这样你就会知道某些东西是错误的。



另一件好事是这些类尊重输入中的时区并在输出中使用它(除非您明确地指示它们除此以外)。由于一些有趣的时区问题,它们将永远不会在1月6日星期五到1月5日星期四。

I have a problem with date converting. I use the following program and I expect the output: 19.05.2017

But the output is: 05.00.2017

Can anybody help?

String t = "Fri May 19 00:00:00 CEST 2017";
Date d = new SimpleDateFormat("EEE MMM DD hh:mm:ss zzzz YYYY", Locale.US).parse(t);
String s = new SimpleDateFormat("dd.mm.yyyy").format(d).toString();
System.out.println(s);

解决方案

A surprising result. The oldfashioned classes SimpleDateFormat and friends are full of surprises. This is meant as a negative thing.

Uppercase DD is day of year. Lowercase hh is hour of AM or PM (1 through 12). Uppercase YYYY is weekbased year (only useful with week number). So you are asking for a date that is a Friday in May and the 19th day of the year. Obviously this is not possible.

The result of parsing is Thu Jan 05 23:00:00 CET 2017. Apparently SimpleDateFormat opts for giving you a Friday and for using the zone offset of 2 hours implied by CEST even though the date it has chosen is not at the time of year where CEST (summer time) is in use. I don’t know whether it just gives you the first Friday of the weekbased year (Friday in week 1 of the year). Friday at 0000 hours at offset GMT+2 equals Thursday at 23 at GMT+1, which is CET.

Next for the formatting, 05 is the date as expected, but lowercase mm means minutes. Since the minutes are 0, you get 00. You got the right year.

Rather than using the outdated classes that give you such surprises, I agree with Sam’s answer that you should use the newer classes in java.time:

    ZonedDateTime dt = ZonedDateTime.parse(t,
            DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US));
    String s = dt.format(DateTimeFormatter.ofPattern("dd.MM.uuuu"));

This code gives you 19.05.2017 as you had expected. One of the good things about the modern classes is, if you try to parse with your original format pattern string, you will get a DateTimeParseException so you will know something is wrong. I certainly prefer an exception over incorrect output.

Another good thing is these classes respect the time zone in the input and use it in the output too (unless you explicitly instruct them otherwise). They will never turn Friday 6 January into Thursday 5 January because of some funny time zone issue.

这篇关于与SimpleDateFormat转换日期混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆