patternjavaMajor
Leap year check in Java
Viewed 0 times
leapcheckyearjava
Problem
I'm looking for 3 different answers:
I am looking for improving my code (as per the above 3 conditions for 3 answers) taken from my repository here.
- Using Java predefined functions
- Converting from string to number
- Without the above two (any other types of answers are also welcome)
I am looking for improving my code (as per the above 3 conditions for 3 answers) taken from my repository here.
/*
* Checks if the year passed is a leap year
*
* @param year; requires the year to have 4 digits
* @return true if leap, else false
*
*/
public static boolean isLeapYear(int year) {
String testYear = String.valueOf(year);
if (testYear.charAt(2) == '1' || testYear.charAt(2) == '3' ||
testYear.charAt(2) == 5 || testYear.charAt(2) == '7' ||
testYear.charAt(2) == '9') {
//If the third digit is odd
if (testYear.charAt(3)=='2'||testYear.charAt(3)=='6'){
return true;
}
else{
return false;
}
}
else{
if (testYear.charAt(2) == '0' && testYear.charAt(3) == '0') {
if(testYear.charAt(0) == '1' || testYear.charAt(0) == '3' ||
testYear.charAt(0) == 5 || testYear.charAt(0) == '7' ||
testYear.charAt(0) == '9'){
//If first digit is odd
if(testYear.charAt(1)=='2'||testYear.charAt(1)=='6'){
return true;
}
else{
return false;
}
}
//If second digit is not odd
else if(testYear.charAt(1)=='0'||testYear.charAt(1)=='4'||
testYear.charAt(1)=='8'){
return true;
}
else
return false;
}
else if (testYear.charAt(3)=='0'||testYear.charAt(3)=='4'||
testYear.charAt(3)=='8'){
//If fourth digit is not odd
return true;
}
else
return false;
}
}Solution
Processing the number as a string
Don't. In general, converting a numerical value to a string and using some string processing on what was once a number is a code smell. You of course often need to convert numbers to strings for output and formatting, but mathematical operations should be done mathematically.
If you for instance wanted to check whether the second-last digit is even, you can divide your number by 10 and check the oddity of that result. That's an arithmetic check without involving any string processing.
The correct algorithm
Whenever you approach a problem, you should think about the correct algorithm to solve it, and do some research, if applicable. Leap years for instance can be detected by a much simpler algorithm. Leap years in the Gregorian calendars are years that are divisible by 4 but not 100, unless they are also divisible by 400. So:
If it's not divisible by 4, it's not a leap year.
If it's divisible by 4, it's a leap year if: it's not divisible by 100, or it's divisible by 400.
Now that looks almost like an algorithm in code. Which you can write along the lines of
Of course that could also be a one-liner, but this perhaps makes the decision logic a bit more clear.
Java 8
In Java 8, a lot of functionality for handling calendars has been added, such as the
General
It's hard not to notice that your formatting is inconsistent. Sometimes space before opening braces, sometimes not. Sometimes you surround a single statement with braces, sometimes not. Those are largely up to individual preference (although I think space before braces is a must, as is enclosing single statements in braces), but consistency is definitely very important.
Also, as the answer by RubberDuck notes, it's not needed or a good practice to write code of the
Don't. In general, converting a numerical value to a string and using some string processing on what was once a number is a code smell. You of course often need to convert numbers to strings for output and formatting, but mathematical operations should be done mathematically.
If you for instance wanted to check whether the second-last digit is even, you can divide your number by 10 and check the oddity of that result. That's an arithmetic check without involving any string processing.
The correct algorithm
Whenever you approach a problem, you should think about the correct algorithm to solve it, and do some research, if applicable. Leap years for instance can be detected by a much simpler algorithm. Leap years in the Gregorian calendars are years that are divisible by 4 but not 100, unless they are also divisible by 400. So:
If it's not divisible by 4, it's not a leap year.
If it's divisible by 4, it's a leap year if: it's not divisible by 100, or it's divisible by 400.
Now that looks almost like an algorithm in code. Which you can write along the lines of
if (year % 4 == 0) {
if (year % 100 != 0) {
return true; //Divisible by 4 and not by 100, leap year
}
return (year % 400 == 0); //Divisible by 4 and by 100. Leap year if divisible by 400.
}
return false; //Not divisible by 4 - not a leap year.Of course that could also be a one-liner, but this perhaps makes the decision logic a bit more clear.
Java 8
In Java 8, a lot of functionality for handling calendars has been added, such as the
Year class. The following would work:return Year.isLeap(year);General
It's hard not to notice that your formatting is inconsistent. Sometimes space before opening braces, sometimes not. Sometimes you surround a single statement with braces, sometimes not. Those are largely up to individual preference (although I think space before braces is a must, as is enclosing single statements in braces), but consistency is definitely very important.
Also, as the answer by RubberDuck notes, it's not needed or a good practice to write code of the
if (something) return true; else return false type when you can just return the condition directly.Code Snippets
if (year % 4 == 0) {
if (year % 100 != 0) {
return true; //Divisible by 4 and not by 100, leap year
}
return (year % 400 == 0); //Divisible by 4 and by 100. Leap year if divisible by 400.
}
return false; //Not divisible by 4 - not a leap year.return Year.isLeap(year);Context
StackExchange Code Review Q#96460, answer score: 24
Revisions (0)
No revisions yet.