格式化日期时忽略 DST(夏令时)
我有这个应用程序,它将当前日期与 xml 文件中的解析日期进行比较:
Date timeDiff = new Date(date.getTime() - new Date().getTime());
问题是当我通过 simpleDateFormat 将变量 timeDiff 作为字符串输出时,会考虑夏令时,这会增加一个额外的小时。这搞乱了我的输出。
有没有办法让 SimpleDateFormat 忽略 DST?
谢谢!
I have this application which compares current date to a parsed date from an xml file:
Date timeDiff = new Date(date.getTime() - new Date().getTime());
The problem is when i output the variable timeDiff as a string via simpleDateFormat, daylight saving time is taken to account which adds an extra hour. This messes up my output.
Is there anyway to make SimpleDateFormat ignore DST?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将一个时间减去另一个时间,以获得以毫秒为单位的持续时间。您根本不应该尝试将其格式化为日期。这是毫秒数,而不是日期。
目前尚不清楚您期望的结果是什么,但目前您基本上采用了错误的方式。
You're subtracting one time from another, to get a duration in milliseconds. You shouldn't be trying to format that as a date at all. It's a number of milliseconds, not a date.
It's not clear what you expect the result to be, but at the moment you're basically going about it the wrong way.
为什么不直接使用时间。
它具有 normalize() 方法它需要一个布尔值来指示是否要忽略 DST。
Why don't you just use Time.
It has the normalize() method that takes a boolean to indicate if you want to ignore DST.
好吧伙计们!也许用这个解决方案更好:
Ok guys! Maybe better with this solution: