- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个日期对象标记为 2013 年 2 月 13 日晚上 11 点。我正试图在凌晨 3 点获取下一个最快的日期对象。所以在这种情况下,时间是 2013 年 2 月 14 日凌晨 3 点。
我可以通过在日期字段中添加 1 天并将时间设置为凌晨 3:00 来简单地完成此操作,但是以下情况如何:
我有一个日期对象标记为 2013 年 2 月 14 日凌晨 1 点。在这里,我不需要添加一天,而是简单地设置时间。
有没有一种优雅的方式来做到这一点?下面是我到目前为止所做的,我认为这会起作用,但我只是想知道是否有一个 api 可以使这更容易。像 getNextSoonestDate() 之类的东西
Calendar calendar = Calendar.getInstance();
//myDate is some arbitrary date, like one of the examples posted above (i.e. feb 13th 11pm)
calendar.setTime(myDate);
//set the calender to be 3am
calendar.set(Calendar.HOUR_OF_DAY, 3);
//check if this comes before my current date, if so we know we need to add a day
if (calendar.getTime().before(myDate)){
calendar.add(Calendar.DAY_OF_YEAR, 1);
}
最佳答案
我不认为有一个预先编写的方法来做你想做的事,但写一个实用方法很简单。
话虽如此,如果myDate
,您的代码已经关闭但无法正常工作介于 3:00:00.001
之间和 3:59:59.999
(在这种情况下,我假设您希望它在第二天返回事件)——您需要将不太重要的字段清零:
public static Date getNextTime(Date base, int hourOfDay) {
Calendar then = Calendar.getInstance();
then.setTime(base);
then.set(Calendar.HOUR_OF_DAY, hourOfDay);
then.set(Calendar.MINUTE, 0);
then.set(Calendar.SECOND, 0);
then.set(Calendar.MILLISECOND, 0);
if (then.getTime().before(base)) {
then.add(Calendar.DAY_OF_YEAR, 1);
}
return then.getTime();
}
Date nextOccurrenceOf3am = getNextTime(myDate, 3);
关于java - 如何找到午夜过后最快的 Java 日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14858207/
我发现了类似的问题here ,但这与我想做的事情无关。我在互联网上做了很多研究,我确定 Delphi 正在按设计或预期工作,如果时间为零,它会忽略时间。我有一个在 ListView 中显示日期和时间的
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Background Worker Check For When It's Midnight? 是否有我可以
尝试创建新日期并将其设置为 UTC ISO 午夜并获得如下“2017-07-12T00:00:00”的输出,但到目前为止没有成功。 var dateNow = new Date(); var date
我如何计算今天的 NSTimeInterval 时分秒为零? 我知道今天的NSTimeInterval是这样的: NSTimeInterval today = [[NSDate date] time
我正在尝试创建一个created_at查询范围,它将捕获从UTC午夜(数据库默认值)到现在创建的所有记录。过去我可以使用以下方式查询时间范围: created_at => (24.hours.ago.
我试图将日期传递给 hubspot。但它显示以下错误消息[消息] => 1462127400 是 22:8:47.400 UTC,不是午夜! 如何使用 PHP 将日期转换为 UTC 午夜格式 最佳答案
我试图将日期传递给 hubspot。但它显示以下错误消息[消息] => 1462127400 是 22:8:47.400 UTC,不是午夜! 如何使用 PHP 将日期转换为 UTC 午夜格式 最佳答案
我想使用 Quartz 的 CronTrigger 每 24 小时安排一个组件例如-午夜。 以下表达式每 5 秒运行一次 CronScheduleBuilder.cronSchedule("0/5 *
我有一列包含一系列时间戳的列。最初我认为它们在 Unix 时间戳系统中,所以我使用以下代码将它们转换为日期时间。 big_frame['date'] = pd.to_datetime(big_fram
我是一名优秀的程序员,十分优秀!