How can I get a DateTime corresponding to the next occurrence of an hour, minute?

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

Also, note that if the minute current has already been started (which, I think, means that we are within 00 milliseconds of that minute), again, we need to find the next β€œoccurrence of this time.

An example of (say) getting a DateTime for the next 10:34 and the next 12:45 pm, which on Wednesday would be greatly appreciated.

How can I do this in Yoda?

+4
source share
2 answers

Something like this, for "next 10:34 am 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 
+9
source

java.time

Answer Matt Ball is suitable for Joda-Time. The creators of Joda-Time said that we should go to java.time , built into Java 8 and later, as soon as it is convenient. So here is the same code rewritten for java.time.

DayOfWeek enum

Since java.time is built into Java, it seems reasonable to replace the argument using int as the day of the week with enum DayOfWeek enabled.

Timezone

The code in the other answer has one serious problem: it implicitly depends on the current JVM time zone. This default value can change at any time, even at runtime (!). It is best to always indicate your expected / desired time zone. So I added one more argument to our method.

DST Setup

If your given hour and minute of land is in an anomaly, such as daylight saving time (DST), java.time will do the tuning. Be sure to read the document to understand this behavior and make sure that it meets your desires / expectations.

Using TemporalAdjustor next( dayOfWeek ) defined in TemporalAdjustors (note the plural s ) means that we will receive the next such day, in the future and in the future, never the past. Therefore, we can discard the ternary test in the return .

 public ZonedDateTime getNextTimeOnDay ( ZoneId zoneId, DayOfWeek dayOfWeek, int hourOfDay, int minuteOfHour ) { ZonedDateTime now = ZonedDateTime.now ( zoneId ); ZonedDateTime then = now .with ( TemporalAdjusters.next ( dayOfWeek ) ) .withHour ( hourOfDay ) .withMinute ( minuteOfHour ) .withSecond ( 0 ) .withNano ( 0 ); return then; } 

An example of calling this method.

 ZoneId zoneId = ZoneId.of ( "America/Montreal" ); ZonedDateTime nextWednesdayAtTenThirtyFour = this.getNextTimeOnDay ( zoneId, DayOfWeek.WEDNESDAY, 10, 34 ); 

Dump for the console.

 System.out.println ( nextWednesdayAtTenThirtyFour ); 

2016-01-20T10: 34-05: 00 [America / Montreal]

By the way, you may need to pass ZonedDateTime , which will be used by the method as a starting point, rather than counting "now." This route may be more useful / flexible and make testing easier. If you choose this route, you do not need to transmit ZoneId , because the past ZonedDateTime carries with it the assigned time zone.

0
source

Source: https://habr.com/ru/post/1389541/


All Articles