Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,844 questions

55,671 answers

573 users

How to find the dates of the last Fridays of each month of a given year in Kotlin

1 Answer

0 votes
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.format.DateTimeFormatter

object LastFridaysOfYearProgram {

    // Return all last Fridays of each month in a given year
    private fun lastFridaysOfYear(year: Int) = sequence {
        for (month in 1..12) {

            // Last day of the month
            var date = LocalDate.of(year, month, 1)
                .plusMonths(1)
                .minusDays(1)

            // Walk backward to Friday
            while (date.dayOfWeek != DayOfWeek.FRIDAY) {
                date = date.minusDays(1)
            }

            yield(date)
        }
    }

    @JvmStatic
    fun main(args: Array<String>) {

        val year: Int =
            if (args.isNotEmpty()) args[0].toInt()
            else 2026

        val fmt = DateTimeFormatter.ofPattern("MM/dd/yyyy")

        for (date in lastFridaysOfYear(year)) {
            println(date.format(fmt))
        }
    }
}



/*
run:

01/30/2026
02/27/2026
03/27/2026
04/24/2026
05/29/2026
06/26/2026
07/31/2026
08/28/2026
09/25/2026
10/30/2026
11/27/2026
12/25/2026

*/

 



answered May 23 by avibootz

Related questions

...