I am trying to create some code in Java where someone can determine the date of the next recurring time of the week specified. This is hard to explain so I’ll give an example. Say it is March 1st (a Thursday) and the user wants to know when the next Saturday 5:00 is the code should output March 3rd, 5:00 as a Date variable and if it is March 4th, the program should output March 10th and so on…
The user however, can specify what time of the week they want. This is done with a long value which offsets the time of the week from Thursday 0:00 in milliseconds. I’m having trouble wrapping my head around this but this is what I got so far:
public static long findNextTimeOfTheWeek(long offset)
{
return System.currentTimeMillis() - ((System.currentTimeMillis() - offset) % 604800000) + 604800000;
}
If anyone could help, it would be greatly appreciated. And if any more clarification is needed, just ask. P.S. 604800000 is the number of milliseconds in a week.
I suggest you to use
Calendarclass, you can easily manipulate it and then get aDateobject usingCalendar#getTime. See the example below, it simply do what you want:More Information: