gpt4 book ai didi

java - 时间减法有什么问题?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:54:56 27 4
gpt4 key购买 nike

我知道 Date 类有时真的很痛苦。

你能给我一些止痛药吗,比如关于如何减去时间的建议?

我有的是:

SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
String s = "13.04.2015 16:00";
String s2 = "13.04.2015 15:30";
Date date = format.parse(s);
System.out.println(timeFormat.format(date.getTime() - format.parse(s2).getTime()));

这给了我 02:30:00,但我很想得到 00:30:00 :)

最佳答案

我建议为 SimpleDateFormat 强制使用公共(public) TimeZone:

format.setTimeZone(TimeZone.getTimeZone("UTC"));
timeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

在格式化 date 之前。

关于java - 时间减法有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29605652/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com