Check if a year/date is a leap year in Java (#6086)
* Hexagonal architecture implementation in Java - upendra.chintala@gmail.com * An example program to find a leap year using java 8 java.time.Year API * Changed to add assertions * Remvoed evaluation article code * Added unit test suite for leap year testing using java.time.Year and java.util.GregorianCalendar * Update LeapYearUnitTest.java * Update LeapYearUnitTest.java
This commit is contained in:
parent
c4edb95d4e
commit
36e4892446
|
@ -0,0 +1,39 @@
|
||||||
|
package com.baeldung.leapyear;
|
||||||
|
|
||||||
|
import java.time.Year;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class LeapYearUnitTest {
|
||||||
|
|
||||||
|
//Before Java8
|
||||||
|
@Test
|
||||||
|
public void testLeapYearUsingGregorianCalendar () {
|
||||||
|
Assert.assertFalse(new GregorianCalendar().isLeapYear(2018));
|
||||||
|
}
|
||||||
|
|
||||||
|
//Java 8 and above
|
||||||
|
@Test
|
||||||
|
public void testLeapYearUsingJavaTimeYear () {
|
||||||
|
Assert.assertTrue(Year.isLeap(2012));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBCYearUsingJavaTimeYear () {
|
||||||
|
Assert.assertTrue(Year.isLeap(-4));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWrongLeapYearUsingJavaTimeYear () {
|
||||||
|
Assert.assertFalse(Year.isLeap(2018));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLeapYearInDateUsingJavaTimeYear () {
|
||||||
|
LocalDate date = LocalDate.parse("2020-01-05", DateTimeFormatter.ISO_LOCAL_DATE);
|
||||||
|
Assert.assertTrue(Year.from(date).isLeap());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue