如何获得与下一次出现的小时,分​​钟相对应的DateTime? [英] How can I get a DateTime corresponding to next occurence of hour, minute?

查看:58
本文介绍了如何获得与下一次出现的小时,分​​钟相对应的DateTime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够为我可以用一个小时和一分钟指定的下一次出现的时间获得正确的DateTime.我还希望能够通过为该小时和分钟指定相同的内容来做到这一点,但是要在(例如)星期三指定下一次出现的内容.

I'd like to be able to get a correct DateTime for the NEXT occurrence of a time that I can specify with just an hour and minute. I'd also like to be able to do this by specifying the same thing for that hour and minute, but for the next occurrence of that on (say) a Wednesday.

此外,请注意,如果当前分钟已经开始"(我想这意味着我们处于该分钟的00毫秒内),那么,我们再次需要找到下一个" 那个时间的发生.

Also, note that if the current minute has already "started" (which I guess means that we're in 00 milliseconds of that minute) then, again, we need to find the "next" occurrence of that time.

一个非常有趣的示例(例如)在星期三获取下一个10:34 AM和下一个12:45 PM的DateTime.

An example with (say) getting a DateTime for the next 10:34 AM and next 12:45 PM that's on a Wednesday, would be greatly appreciated.

我如何在Joda中做到这一点?

How can I do this in Joda?

推荐答案

星期三下一个上午10:34:"之类的东西

Something like this, for "next 10:34 AM on a Wednesday:"

DateTime getNextTimeOnDay(int dayOfWeek, int hourOfDay, int minuteOfHour)
{
    DateTime now = new DateTime();
    DateTime then = now
        .withDayOfWeek(dayOfWeek)
        .withHourOfDay(hourOfDay)
        .withMinuteOfHour(minuteOfHour)
        .withSecondOfMinute(0)
        .withMillisOfSecond(0);

    return then.isBefore(now) ? then.plusWeeks(1) : then;
}

DateTime nextWednesdayAtTenThirtyFour
    = getNextTimeOnDay(DateTimeConstants.WEDNESDAY, 10, 34);

// as of now, this is 2012-01-06T10:34:00.000-05:00

这篇关于如何获得与下一次出现的小时,分​​钟相对应的DateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆