-
Notifications
You must be signed in to change notification settings - Fork 20.5k
Dev: Add Zeller's Congruence utility class to calculate the day of the week #6614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
DenizAltunkapan
merged 7 commits into
TheAlgorithms:master
from
BanulaKumarage:feature/add-zellers-congruence
Oct 9, 2025
+143
−0
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7bec682
Add Zeller's Congruence utility class and unit tests
BanulaKumarage a3e0975
fixed identified bugs
BanulaKumarage 6730f26
fixed pmd failure
BanulaKumarage 5772966
Merge branch 'master' into feature/add-zellers-congruence
BanulaKumarage 0541be2
Merge branch 'master' into feature/add-zellers-congruence
BanulaKumarage 9c12012
Merge branch 'master' into feature/add-zellers-congruence
BanulaKumarage fbb3f57
Merge branch 'master' into feature/add-zellers-congruence
BanulaKumarage File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
src/main/java/com/thealgorithms/maths/ZellersCongruence.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package com.thealgorithms.maths; | ||
|
||
import java.time.DateTimeException; | ||
import java.time.LocalDate; | ||
import java.util.Objects; | ||
|
||
/** | ||
* A utility class for calculating the day of the week for a given date using Zeller's Congruence. | ||
* | ||
* <p>Zeller's Congruence is an algorithm devised by Christian Zeller in the 19th century to calculate | ||
* the day of the week for any Julian or Gregorian calendar date. The input date must be in the format | ||
* "MM-DD-YYYY" or "MM/DD/YYYY". | ||
* | ||
* <p>This class is final and cannot be instantiated. | ||
* | ||
* @see <a href="https://en.wikipedia.org/wiki/Zeller%27s_congruence">Wikipedia: Zeller's Congruence</a> | ||
*/ | ||
public final class ZellersCongruence { | ||
|
||
private static final String[] DAYS = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; | ||
|
||
// Private constructor to prevent instantiation | ||
private ZellersCongruence() { | ||
} | ||
|
||
/** | ||
* Calculates the day of the week for a given date using Zeller's Congruence. | ||
* | ||
* <p>The algorithm works for both Julian and Gregorian calendar dates. The input date must be | ||
* in the format "MM-DD-YYYY" or "MM/DD/YYYY". | ||
* | ||
* @param input the date in the format "MM-DD-YYYY" or "MM/DD/YYYY" | ||
* @return a string indicating the day of the week for the given date | ||
* @throws IllegalArgumentException if the input format is invalid, the date is invalid, | ||
* or the year is out of range | ||
*/ | ||
public static String calculateDay(String input) { | ||
if (input == null || input.length() != 10) { | ||
throw new IllegalArgumentException("Input date must be 10 characters long in the format MM-DD-YYYY or MM/DD/YYYY."); | ||
} | ||
|
||
int month = parsePart(input.substring(0, 2), 1, 12, "Month must be between 1 and 12."); | ||
char sep1 = input.charAt(2); | ||
validateSeparator(sep1); | ||
|
||
int day = parsePart(input.substring(3, 5), 1, 31, "Day must be between 1 and 31."); | ||
char sep2 = input.charAt(5); | ||
validateSeparator(sep2); | ||
|
||
int year = parsePart(input.substring(6, 10), 46, 8499, "Year must be between 46 and 8499."); | ||
|
||
try { | ||
Objects.requireNonNull(LocalDate.of(year, month, day)); | ||
} catch (DateTimeException e) { | ||
throw new IllegalArgumentException("Invalid date.", e); | ||
} | ||
if (month <= 2) { | ||
year -= 1; | ||
month += 12; | ||
} | ||
|
||
int century = year / 100; | ||
int yearOfCentury = year % 100; | ||
int t = (int) (2.6 * month - 5.39); | ||
int u = century / 4; | ||
int v = yearOfCentury / 4; | ||
int f = (int) Math.round((day + yearOfCentury + t + u + v - 2 * century) % 7.0); | ||
|
||
int correctedDay = (f + 7) % 7; | ||
|
||
return "The date " + input + " falls on a " + DAYS[correctedDay] + "."; | ||
} | ||
|
||
/** | ||
* Parses a part of the date string and validates its range. | ||
* | ||
* @param part the substring to parse | ||
* @param min the minimum valid value | ||
* @param max the maximum valid value | ||
* @param error the error message to throw if validation fails | ||
* @return the parsed integer value | ||
* @throws IllegalArgumentException if the part is not a valid number or is out of range | ||
*/ | ||
private static int parsePart(String part, int min, int max, String error) { | ||
try { | ||
int value = Integer.parseInt(part); | ||
if (value < min || value > max) { | ||
throw new IllegalArgumentException(error); | ||
} | ||
return value; | ||
} catch (NumberFormatException e) { | ||
throw new IllegalArgumentException("Invalid numeric part: " + part, e); | ||
} | ||
} | ||
|
||
/** | ||
* Validates the separator character in the date string. | ||
* | ||
* @param sep the separator character | ||
* @throws IllegalArgumentException if the separator is not '-' or '/' | ||
*/ | ||
private static void validateSeparator(char sep) { | ||
if (sep != '-' && sep != '/') { | ||
throw new IllegalArgumentException("Date separator must be '-' or '/'."); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/test/java/com/thealgorithms/maths/ZellersCongruenceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.thealgorithms.maths; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.util.stream.Stream; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
class ZellersCongruenceTest { | ||
|
||
static Stream<Arguments> validDates() { | ||
return Stream.of(Arguments.of("01-01-2000", "Saturday"), Arguments.of("12-25-2021", "Saturday"), Arguments.of("07-04-1776", "Thursday"), Arguments.of("02-29-2020", "Saturday"), Arguments.of("03-01-1900", "Thursday"), Arguments.of("03/01/1900", "Thursday")); | ||
} | ||
|
||
static Stream<Arguments> invalidDates() { | ||
return Stream.of(Arguments.of("13-01-2000", "Month must be between 1 and 12."), Arguments.of("02-30-2020", "Invalid date."), Arguments.of("00-15-2020", "Month must be between 1 and 12."), Arguments.of("01-01-0000", "Year must be between 46 and 8499."), | ||
Arguments.of("01/01/200", "Input date must be 10 characters long in the format MM-DD-YYYY or MM/DD/YYYY."), Arguments.of("01@01>2000", "Date separator must be '-' or '/'."), Arguments.of("aa-01-1900", "Invalid numeric part: aa"), | ||
Arguments.of(null, "Input date must be 10 characters long in the format MM-DD-YYYY or MM/DD/YYYY.")); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("validDates") | ||
void testValidDates(String inputDate, String expectedDay) { | ||
String result = ZellersCongruence.calculateDay(inputDate); | ||
assertEquals("The date " + inputDate + " falls on a " + expectedDay + ".", result); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("invalidDates") | ||
void testInvalidDates(String inputDate, String expectedErrorMessage) { | ||
Exception exception = assertThrows(IllegalArgumentException.class, () -> ZellersCongruence.calculateDay(inputDate)); | ||
assertEquals(expectedErrorMessage, exception.getMessage()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.