- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有一个问题,当时钟由于夏令时(每年两次)而移动时,Java 中的日期不正确(我位于中欧:夏季 GMT+2
,冬季GMT+1
)
如果时间提前 1 小时,new Date()
仍返回旧时间(比当前时间晚 1 小时)。
在 Java 7 中,可以在不重新启动 Jboss 应用服务器的情况下解决这个问题吗?
如果我在 Windows 中手动更改时间,则会重现问题:除非重新启动 jboss,否则 Date
不会更新为系统日期。
Calendar c = Calendar.getInstance();
c.setTime(new Date());
最佳答案
在Java <= 7中,您可以使用ThreeTen Backport ,Java 8 新日期/时间类的一个很好的向后移植。
有了这个,您可以轻松处理夏令时更改。
首先,您可以使用 org. Threeten.bp.DateTimeUtils
在 Calendar
之间进行转换。
以下代码将 Calendar
转换为 org. Threeten.bp.Instant
,这是一个表示“UTC 时刻”(独立于时区的时间戳)的类:现在,此时此刻,世界上的每个人都处于同一时刻,尽管他们的本地日期和时间可能会有所不同,具体取决于他们所在的位置)。
然后,Instant
被转换为 org. Threeten.bp.ZonedDateTime
(这意味着:在这一瞬间,这个时区的日期和时间是多少? )。我还使用 org. Threeten.bp.ZoneId 来获取时区:
Calendar c = Calendar.getInstance();
c.setTime(new Date());
// get the current instant in UTC timestamp
Instant now = DateTimeUtils.toInstant(c);
// convert to some timezone
ZonedDateTime z = now.atZone(ZoneId.of("Europe/Berlin"));
// today is 08/06/2017, so Berlin is in DST (GMT+2)
System.out.println(z); // 2017-06-08T14:11:58.608+02:00[Europe/Berlin]
// testing with a date in January (not in DST, GMT+1)
System.out.println(z.withMonth(1)); // 2017-01-08T14:11:58.608+01:00[Europe/Berlin]
我刚刚选择了一些使用中欧时区 (Europe/Berlin
) 的时区:您不能使用这些 3 字母缩写,因为它们是 ambiguous and not standard 。您可以将代码更改为最适合您系统的时区(您可以使用 ZoneId.getAvailableZoneIds()
获取所有可用时区的列表)。
我更喜欢这个解决方案,因为它明确了我们用来向用户显示的时区(Date
和 Calendar
的 toString()
方法在幕后使用默认时区,你永远不知道它们在做什么)。
在内部,我们可以继续使用 UTC 格式的 Instant
,因此它不受时区的影响(并且您可以随时在需要时在时区之间进行转换) - 如果您愿意的话将 ZonedDateTime
转换回 Instant
,只需使用 toInstant()
方法即可。
实际上,如果您想获取当前日期/时间,只需忘记旧的类(Date
和 Calendar
)并仅使用 Instant
:
// get the current instant in UTC timestamp
Instant now = Instant.now();
但是如果您仍然需要使用旧的类,只需使用 DateTimeUtils
进行转换即可。
上述示例的输出是 ZonedDateTime.toString()
方法的结果。如果您想更改格式,请使用 org. Threeten.bp.format.DateTimeFormatter 类(有关所有可能格式的更多详细信息,请查看 javadoc):
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss z X");
// DST (GMT+02)
System.out.println(formatter.format(z)); // 08/06/2017 14:11:58 CEST +02
// not DST (GMT+01)
System.out.println(formatter.format(z.withMonth(1))); // 08/01/2017 14:11:58 CET +01
关于java - Jboss Java Date 夏令时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44434167/
数据框有一个字符串类型的日期列 '2017-01-01' 它被转换为 DateType() df = df.withColumn('date', col('date_string').cast(Dat
这个问题在这里已经有了答案: What is "x && foo()"? (5 个答案) 关闭 8 年前。 我在 bootstrap-datepicker.js 文件中遇到过这个。 作者在_setD
我有一个数据库 utc 字符串,我正在传递到 Date(attrs.endDate),然后通过 new Date() 减去当前的 utc 日期,但我无法得到它来为我提供 2 个 utc 日期的正确差异
这个问题在这里已经有了答案: how to determine if 2 dates object equals each other? [duplicate] (3 个答案) 关闭 6 年前。 我
这个问题已经有答案了: How can I convert "/Date(1399739515000)/" into date format in JavaScript? (3 个回答) 已关闭 8
根据MDN ,我们只能将以下类型的参数传递给 Date 构造函数: new Date(); new Date(value); // Unix timestamp new Date(dateString
我从表中获取所有项目: endDate >= 现在 endDate 为 NULL published 等于 1。 这是我所拥有的,但它给了我 0 个项目: $items = Items::orderB
此查询需要很长时间才能完成。当我将 WHERE 子句设置为 new_dl >= '2014-01-01' 时,查询大约需要 6 分钟才能浏览大约 3 个月的数据。现在不知道为什么这个应该从 12 个月
我有一个正在为项目开发的小型 Java 程序,它使用 JavaMail 从指定的 URI 中提取用户的收件箱,然后开始处理消息。 在 Outlook 中,属性菜单中有一个功能可以设置邮件的到期日期,它
我想在获取 Date.getHours()、Date.getMinutes() 和 Date.getSeconds() 的值后格式化输出>. 这是一条漫长的路: var dt = new Date()
我发现java.text.DateFormat有两种格式化日期的方法。一种是采用 Date 参数,另一种是采用 Object 参数。我检查了DateFormat源代码,似乎他们调用了不同的内部方法。
我有两个对象,p4 和 p5,它们都具有 Date 属性。在某些时候,构造函数工作正常: p4.setClickDate(new Date(System.currentTimeMillis() - 8
我是使用 Sequelize 和 Node.js 的新手,但我的代码中存在日期比较问题。 User.findOne({ where: { resetToken: passwordToken,
我正在使用一个名为 fullcalendar 的 jquery 日历。当用户单击某一天时,他们将被发送到另一个页面以创建该天的事件。单击的日期作为 date 提供。然后通过下面的函数运行将其转换为 U
我有一个列表列表,每个列表中都有整数值,代表 8 年期间的日期。 dates = [[2014, 11, 14], [2014, 11, 13], ....., [2013, 12, 01]
我有两个表: 首先是TimeValues(示例) time | value 12/28/18 | 5.6 01/03/19 | 5.6 01/04/19 | 5.6 01/09/19 | 5.
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
像这样实例化的日期对象: new Date("2011-12-13") 返回一个认为自己是星期一的日期对象: Date {Mon Dec 12 2011 16:00:00 GMT-0800 (PST)
我需要选择入住日期和退房日期在指定日期范围之间的房价。这些费率根据其条件单独命名。房费取决于所选日期。这是我的代码: rate_eb rate_name rate_starts rat
我有 [Int64:[String:String]] 其中 Int64 是时间戳。如何检测和删除 [String:String] 中的参数之一是 ["name"] = "test" 并重复多次的同一天
我是一名优秀的程序员,十分优秀!