Java 获取截至 4 小时前的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4348525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Date as of 4 hours ago
提问by ???u
How can I return a Date object of 4 hours less than the current system time in Java?
如何在 Java 中返回比当前系统时间少 4 小时的 Date 对象?
采纳答案by BalusC
One of the simplest ways:
最简单的方法之一:
Date date = new Date(System.currentTimeMillis() - (4 * 60 * 60 * 1000));
This however doesn't take DST (Daylight Saving Time)into account. An alternative is Calendar
:
然而,这并没有考虑到DST(夏令时)。另一种选择是Calendar
:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR_OF_DAY, -4);
Date date = calendar.getTime();
This is however a pretty ugly API. An alternative is JodaTime:
然而,这是一个非常丑陋的 API。另一种选择是JodaTime:
DateTime dateTime = new DateTime().minusHours(4);
Date date = dateTime.toDate(); // If you ever need this...
回答by Thorbj?rn Ravn Andersen
Convert it to milliseconds, subtract the number of milliseconds in 4 hours, convert it back to a Date.
将其转换为毫秒,减去 4 小时内的毫秒数,将其转换回日期。
回答by Nicolas Repiquet
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR_OF_DAY, -4);
calendar.getTime();
回答by Allan
Calendar c =Calendar.getInstance() ;
c.add(Calendar.HOUR,-4);
Date d = c.getTime();
回答by Avrom
Use a Calendar
object and the add
method.
使用Calendar
对象和add
方法。
calendar.add(Calendar.HOUR, -4);
See http://download.oracle.com/javase/6/docs/api/java/util/Calendar.html
请参阅http://download.oracle.com/javase/6/docs/api/java/util/Calendar.html
回答by Robert
Calendar c = Calendar.getInstance();
c.add(Calendar.HOUR_OF_DAY, -4);
java.util.Date d = c.getTime();
System.out.println(d);
回答by Ole V.V.
The other answers are correct, but I would like to contribute the modern answer. The modern solution uses java.time
, the modern Java date and time API.
其他答案是正确的,但我想贡献现代答案。现代解决方案使用java.time
现代 Java 日期和时间 API。
Instant fourHoursAgo = Instant.now().minus(Duration.ofHours(4));
System.out.println(fourHoursAgo);
This just printed:
这只是打印:
2018-01-31T15:22:21.113710Z
The Z
in the end indicates that the time is printed in UTC — at UTC offset zero if you will. The Instant
class is the modern replacement for Date
, so I recommend you stick to it. The modern API is generally so much nicer to work with, so much cleaner, so much better designed.
在Z
最终表示时间打印在UTC -如果你愿意在UTC抵消为零。该Instant
班是现代替代品Date
,所以我建议你坚持下去。现代 API 通常更易于使用,更简洁,设计也更好。
Please note the advantages of letting the library class do the subtraction of 4 hours for you: the code is clearer to read and less error-prone. No funny constants, and no readers taking time to check if they are correct.
请注意让库类为您做 4 小时减法的好处:代码更清晰易读且不易出错。没有有趣的常数,也没有读者花时间检查它们是否正确。
If you doneed an old-fashioned Date
object, for example when using a legacy API that you cannot change or don't want to change, convert like this:
如果您确实需要旧式Date
对象,例如在使用您无法更改或不想更改的旧式 API 时,请按如下方式进行转换:
Date oldfashionedDate = Date.from(fourHoursAgo);
Link:Oracle Tutorial trail: Date Time. Of course there are other resources on the internet too, please search.
链接:Oracle 教程线索:日期时间。当然网上也有其他资源,请自行搜索。