import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields;
import java.util.Locale;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Main {
	public static void main(String[] args) {
		// Let's test
		System.out.println(getMonthCalendar(Locale.UK));
		System.out.println("-+-+-+-+-+-+-+-+-+-+-+-+-+-");
		System.out.println(getMonthCalendar(Locale.US));
	}

	static String getMonthCalendar(Locale locale) {
		LocalDate localDate = LocalDate.now();
		YearMonth ym = YearMonth.of(localDate.getYear(), localDate.getMonthValue());
		StringBuilder sb = new StringBuilder();

		// First day of week
		DayOfWeek firstDayOfWeek = WeekFields.of(locale).getFirstDayOfWeek();

		LocalDate date = localDate.with(TemporalAdjusters.dayOfWeekInMonth(0, firstDayOfWeek));
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE", locale);
		sb.append(
					IntStream.rangeClosed(0, 6)
					.mapToObj(i -> dtf.format(date.plusDays(i)))
					.collect(Collectors.joining(" "))
		)
		.append(System.lineSeparator());

		int counter = 1;

		// Print as many space as the difference between the day of week of 1st date of
		// the month and the first day of the week in that Locale
		int dayValue = localDate.withDayOfMonth(1).getDayOfWeek().getValue() - firstDayOfWeek.getValue();
		dayValue = dayValue < 0 ? 7 + dayValue : dayValue;
		for (int i = 0; i < dayValue; i++, counter++) {
			sb.append(String.format("%-4s", ""));
		}

		for (int i = 1; i <= ym.getMonth().length(ym.isLeapYear()); i++, counter++) {
			sb.append(String.format("%-4d", i));

			// Break the line if the value of the counter is multiple of 7
			if (counter % 7 == 0) {
				sb.append(System.lineSeparator());
			}
		}

		return sb.toString();
	}
}