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,849 questions

55,678 answers

573 users

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

2 Answers

0 votes
import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;

public class LastFridays {

    // Return the last Friday of a specific month
    static int lastFridayOfMonth(GregorianCalendar c, int year, int month) {

        // Set to first day of this month
        c.set(year, month, 1);

        // Last day of this month
        int totalDays = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        c.set(Calendar.DAY_OF_MONTH, totalDays);

        // Compute rollback to Friday
        int dow = c.get(Calendar.DAY_OF_WEEK);  // 1=Sun ... 7=Sat
        int daysToRollBack = (dow - Calendar.FRIDAY + 7) % 7;

        return totalDays - daysToRollBack;
    }

    public static void main(String[] args) throws Exception {

        // Handle missing argument
        int year;
        if (args.length == 0) {
            year = Calendar.getInstance().get(Calendar.YEAR);
        } else {
            year = Integer.parseInt(args[0]);
        }

        GregorianCalendar c = new GregorianCalendar(year, 0, 1);

        String[] months = new DateFormatSymbols(Locale.US).getShortMonths();

        for (int m = 0; m < 12; m++) {
            String mon = months[m];
            if (!mon.isEmpty()) {

                int day = lastFridayOfMonth(c, year, m);

                System.out.printf("%d %s %d\n", year, mon, day);

                // Move to next month safely
                c.add(Calendar.MONTH, 1);
            }
        }
    }
}



/*
run:

2026 Jan 30
2026 Feb 27
2026 Mar 27
2026 Apr 24
2026 May 29
2026 Jun 26
2026 Jul 31
2026 Aug 28
2026 Sep 25
2026 Oct 30
2026 Nov 27
2026 Dec 25

*/

 



answered May 23 by avibootz
0 votes
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class LastFridaysOfYearProgram {

    // Return all last Fridays of each month in a given year
    private static List<LocalDate> lastFridaysOfYear(int year) {
        List<LocalDate> results = new ArrayList<>();

        for (int month = 1; month <= 12; month++) {

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

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

            results.add(date);
        }

        return results;
    }

    public static void main(String[] args) {

        int year = (args.length > 0)
                ? Integer.parseInt(args[0])
                : 2026;

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

        for (LocalDate date : lastFridaysOfYear(year)) {
            System.out.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

...