How do i exctract the monthDay in a LocalDate? I need to solve this within tomorrow so any help soon would be apreciated!
private final Set<MonthDay> recurringHolidays = new HashSet<>();
private final Set<LocalDate> holidays = new HashSet<>();
public void setHoliday(final LocalDate date) {
holidays.add( date );
}
public void setHolidayIfIsRecurring() {
for (LocalDate holidayLocalDate: getHolidays())
for (MonthDay monthDay: getRecurringHolidays()){
LocalDate monthdayToLocalDate = monthDay -------------????
if (!setOfHolidays().contains( monthdayToLocalDate ) ){
setHoliday( (monthDay) );
}
}
}
LocalDate
→ MonthDay
You can use MonthDay#from(TemporalAccessor)
. For example:
import java.time.LocalDate;
import java.time.MonthDay;
public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2020, 6, 20);
MonthDay md = MonthDay.from(date);
System.out.println(md);
}
}
That will output:
--6-20
MonthDay
→ LocalDate
The issue here is that MonthDay
obviously only represents a month and day-of-month, whereas LocalDate
additionally represents a year. You need to decide how to determine what year should be used. Once you know that then two options are:
For example:
import java.time.LocalDate;
import java.time.MonthDay;
import java.time.Year;
public class Main {
public static void main(String[] args) {
MonthDay md = MonthDay.of(6, 20);
Year year = Year.of(2020);
LocalDate date1 = md.atYear(year.getValue());
LocalDate date2 = year.atMonthDay(md);
System.out.println(date1);
System.out.println(date2);
}
}
That will output:
2020-06-20
2020-06-20