I need to get next Friday 13th since the given date using LocalDate
.
How it could be implemented?
CodePudding user response:
You can do it by using only methods plusMonths()
, withDayOfMonth()
of the LocalDate
class.
The starting point is to adjust the day of the month of the given date to the target day 13
. If the day of month of the given date is greater than 13
, then the current date will be assigned to the 13th day of the next month, otherwise to the 13th day of the current month.
And then check in the loop whether the day of the week of the current date is Friday. Note that days of the week in java.util.time API are represented by enum DayOfWeek
.
This approach allows to find the target date only withing a couple of iterations by skipping the whole month.
To avoid hard-coded values and make the code reusable, the day of the month and the day of the week are expected as parameters of the method getNextDay()
.
public static LocalDate getNextDay(LocalDate date, int dayOfMonth, DayOfWeek dayOfWeek) {
LocalDate current = date.getDayOfMonth() > dayOfMonth ?
date.plusMonths(1).withDayOfMonth(dayOfMonth) :
date.withDayOfMonth(dayOfMonth);
while (current.getDayOfWeek() != dayOfWeek) {
current = current.plusMonths(1);
}
return current;
}
public static void main(String[] args) {
System.out.println(getNextDay(LocalDate.now(), 13, DayOfWeek.FRIDAY));
System.out.println(getNextDay(LocalDate.now().plusMonths(5), 13, DayOfWeek.FRIDAY));
}
Output
2022-05-13 - from now ('2022-03-16')
2023-01-13 - from '2022-08-16'
CodePudding user response:
You can do it by just adding 7 days to your LocalDate
and you will get the expected day:
For example:
LocalDate today = LocalDate.now();
//tomorrow
LocalDate tomorrow = today.plusDays(1);
// next week day
LocalDate nextWeekDay= today.plusDays(7);
Or get it by this one :
today.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
CodePudding user response:
public static void main(String[] args) {
boolean is13Friday = false;
LocalDate nextFriday = LocalDate.now();
while (!is13Friday) {
nextFriday = getNextFriday(nextFriday);
is13Friday = is13Friday(nextFriday);
}
System.out.println(nextFriday);
}
public static LocalDate getNextFriday(LocalDate d) {
return d.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
}
public static boolean is13Friday(LocalDate d) {
return d.getDayOfMonth() == 13;
}
CodePudding user response:
You can use IntStream
to iterate through days:
public static LocalDate findFridayThirteen(LocalDate fromDate) {
return IntStream.iterate(0, i -> i 1)
.mapToObj(fromDate::plusDays)
.filter(date -> date.getDayOfWeek() == DayOfWeek.FRIDAY
&& date.getDayOfMonth() == 13)
.findFirst()
.orElseThrow();
}
Or, you can iterate only Fridays:
public static LocalDate findFridayThirteen2(LocalDate fromDate) {
LocalDate nextFriday = fromDate.with(
TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY));
return IntStream.iterate(0, i -> i 7)
.mapToObj(nextFriday::plusDays)
.filter(date -> date.getDayOfMonth() == 13)
.findFirst()
.orElseThrow();
}
Or, you can iterate only the 13 of each month:
public static LocalDate findFridayThirteen3(LocalDate fromDate) {
LocalDate nextThirteen = fromDate.getDayOfMonth() > 13
? fromDate.withDayOfMonth(13).plusMonths(1)
: fromDate.withDayOfMonth(13);
return IntStream.iterate(0, i -> i 1)
.mapToObj(nextThirteen::plusMonths)
.filter(date -> date.getDayOfWeek() == DayOfWeek.FRIDAY)
.findFirst()
.orElseThrow();
}
Test:
LocalDate date = LocalDate.of(2022, 1, 1);
LocalDate friday13 = findFridayThirteen(date);
DateTimeFormatter formatter = DateTimeFormatter
.ofLocalizedDate(FormatStyle.FULL);
System.out.println(friday13.format(formatter));
Output:
Friday, May 13, 2022
CodePudding user response:
public static LocalDateTime nextFriday13() {
LocalDateTime now = LocalDateTime.now();
while (now.getDayOfMonth() != 13) {
now = now.plusDays(1);
}
while (!now.getDayOfWeek().equals(DayOfWeek.FRIDAY)) {
now = now.plusMonths(1);
}
return now;
}