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.