simplified the leap year check logic
This commit is contained in:
parent
439a37301b
commit
7e843727b9
@ -1,11 +1,16 @@
|
|||||||
package com.baeldung.multireleaseapp;
|
package com.baeldung.multireleaseapp;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(App.class);
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
String dateToCheck = args[0];
|
String dateToCheck = args[0];
|
||||||
boolean isLeapYear = DateHelper.checkIfLeapYear(dateToCheck);
|
boolean isLeapYear = DateHelper.checkIfLeapYear(dateToCheck);
|
||||||
System.out.println("Date given " + dateToCheck + " is leap year: " + isLeapYear);
|
logger.info("Date given " + dateToCheck + " is leap year: " + isLeapYear);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,23 +2,21 @@ package com.baeldung.multireleaseapp;
|
|||||||
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public class DateHelper {
|
public class DateHelper {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(DateHelper.class);
|
||||||
|
|
||||||
public static boolean checkIfLeapYear(String dateStr) throws Exception {
|
public static boolean checkIfLeapYear(String dateStr) throws Exception {
|
||||||
System.out.println("Checking for leap year using Java 1 calendar API ");
|
logger.info("Checking for leap year using Java 1 calendar API");
|
||||||
boolean isLeapYear = false;
|
|
||||||
Calendar cal = Calendar.getInstance();
|
Calendar cal = Calendar.getInstance();
|
||||||
cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(dateStr));
|
cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(dateStr));
|
||||||
int year = cal.get(Calendar.YEAR);
|
int year = cal.get(Calendar.YEAR);
|
||||||
if (year % 4 == 0) {
|
return (new GregorianCalendar()).isLeapYear(year);
|
||||||
if (year % 100 == 0) {
|
|
||||||
isLeapYear = (year % 400 == 0) ? true : false;
|
|
||||||
} else {
|
|
||||||
isLeapYear = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return isLeapYear;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,15 @@ package com.baeldung.multireleaseapp;
|
|||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public class DateHelper {
|
public class DateHelper {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(DateHelper.class);
|
||||||
|
|
||||||
public static boolean checkIfLeapYear(String dateStr) throws Exception {
|
public static boolean checkIfLeapYear(String dateStr) throws Exception {
|
||||||
System.out.println("Checking for leap year using Java 9 Date Api");
|
logger.info("Checking for leap year using Java 9 Date Api");
|
||||||
return LocalDate.parse(dateStr)
|
return LocalDate.parse(dateStr)
|
||||||
.isLeapYear();
|
.isLeapYear();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user