How to resolve the algorithm Last Friday of each month step by step in the Java programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Last Friday of each month step by step in the Java programming language
Table of Contents
Problem Statement
Write a program or a script that returns the date of the last Fridays of each month of a given year. The year may be given through any simple input method in your language (command line, std in, etc).
Example of an expected output:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Last Friday of each month step by step in the Java programming language
This Java program calculates and prints the dates of the last Fridays in each month of a given year, passed as the first argument to the program. Let's break down the code:
-
Input Processing:
int year = Integer.parseInt(args[0]);
: Parses the year from the first command-line argument and stores it in theyear
variable.
-
Calendar Setup:
GregorianCalendar c = new GregorianCalendar(year, 0, 1);
: Creates aGregorianCalendar
objectc
for the specifiedyear
. It initializes the calendar to January 1st of that year.
-
Loop Through Months:
for (String mon : new DateFormatSymbols(Locale.US).getShortMonths())
: Iterates through the short names of the months for the US locale.
-
Month-End Calculations:
int totalDaysOfMonth = c.getActualMaximum(Calendar.DAY_OF_MONTH);
: Gets the total number of days in the current month.c.set(Calendar.DAY_OF_MONTH, totalDaysOfMonth);
: Sets the calendar to the last day of the current month.
-
Days to Roll Back:
int daysToRollBack = (c.get(Calendar.DAY_OF_WEEK) + 1) % 7;
: Calculates the number of days to roll back from the last day of the month to get to the last Friday. This is done by finding the day of the week for the last day (0-based index) and adding 1 to it. The result is then taken modulo 7 to find the number of days to roll back.
-
Last Friday's Date:
int day = totalDaysOfMonth - daysToRollBack;
: Calculates the day of the last Friday by subtracting the days to roll back from the total days in the month.c.set(Calendar.DAY_OF_MONTH, day);
: Sets the calendar to the calculated last Friday's date.
-
Output:
System.out.printf("%d %s %d\n", year, mon, day);
: Prints the calculated last Friday's date in the format "year month day".
-
Advance to Next Month:
c.set(year, c.get(Calendar.MONTH) + 1, 1);
: Advances the calendar to the first day of the next month.
Source code in the java programming language
import java.text.*;
import java.util.*;
public class LastFridays {
public static void main(String[] args) throws Exception {
int year = Integer.parseInt(args[0]);
GregorianCalendar c = new GregorianCalendar(year, 0, 1);
for (String mon : new DateFormatSymbols(Locale.US).getShortMonths()) {
if (!mon.isEmpty()) {
int totalDaysOfMonth = c.getActualMaximum(Calendar.DAY_OF_MONTH);
c.set(Calendar.DAY_OF_MONTH, totalDaysOfMonth);
int daysToRollBack = (c.get(Calendar.DAY_OF_WEEK) + 1) % 7;
int day = totalDaysOfMonth - daysToRollBack;
c.set(Calendar.DAY_OF_MONTH, day);
System.out.printf("%d %s %d\n", year, mon, day);
c.set(year, c.get(Calendar.MONTH) + 1, 1);
}
}
}
}
You may also check:How to resolve the algorithm ABC problem step by step in the Scala programming language
You may also check:How to resolve the algorithm OpenGL step by step in the Java programming language
You may also check:How to resolve the algorithm HTTPS/Authenticated step by step in the Tcl programming language
You may also check:How to resolve the algorithm Heronian triangles step by step in the Rust programming language
You may also check:How to resolve the algorithm Statistics/Normal distribution step by step in the EDSAC order code programming language