import java.time.{LocalDate, DayOfWeek}
import java.time.format.DateTimeFormatter
// Return all last Sundays of each month in a given year
def lastSundaysOfYear(year: Int): Iterator[LocalDate] = new Iterator[LocalDate] {
private var month: Int = 1
override def hasNext: Boolean = month <= 12
override def next(): LocalDate = {
// Last day of the month
var date: LocalDate =
LocalDate.of(year, month, 1)
.plusMonths(1)
.minusDays(1)
// Walk backward to Sunday
while (date.getDayOfWeek != DayOfWeek.SUNDAY) {
date = date.minusDays(1)
}
month += 1
date
}
}
@main def lastSundaysMain(args: String*): Unit = {
val year: Int =
if (args.nonEmpty)
args.head.toInt
else
2026
val fmt: DateTimeFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy")
for (date <- lastSundaysOfYear(year)) {
println(date.format(fmt))
}
}
/*
run:
01/25/2026
02/22/2026
03/29/2026
04/26/2026
05/31/2026
06/28/2026
07/26/2026
08/30/2026
09/27/2026
10/25/2026
11/29/2026
12/27/2026
*/