You can use MonthDay#of(int month, int dayOfMonth)
to create an instance of MonthDay
out of the given month and the day of the month.
You need a year
Every year 15-Jul may fall on a different day of the week e.g. in 2014, it falls on Tuesday and in the current year 2023, it falls on Saturday. So, you need a year to get the day of the week out of the MonthDay
.
By assigning a year to the MonthDay
, you get a LocalDate
which can give you the day of the week and many other pieces of information e.g. week number, whether it is before/after another day etc.
Demo:
import java.time.LocalDate;
import java.time.MonthDay;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
class Main {
public static void main(String[] args) {
// Test
System.out.println(weekdayMonthDayFormatted("15", "7"));
}
static String weekdayMonthDayFormatted(String day, String month) {
MonthDay md = MonthDay.of(Integer.parseInt(month), Integer.parseInt(day));
// I have used 2014 here just for the demo. Replace 2014 with the applicable
// year e.g. for the current year, replace it with Year.now().getValue()
LocalDate date = md.atYear(2014);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE MMMM dd", Locale.ENGLISH);
return date.format(formatter);
}
}
Output:
Tuesday July 15
ONLINE DEMO
Learn more about the modern Date-Time API from Trail: Date Time.
Note: In Mar 2014, the java.util
date-time API and their formatting API, SimpleDateFormat
were supplanted by the modern date-time API. Since then, it is highly recommended to stop using the legacy date-time API.