-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeapYearsBetweenYears.java
68 lines (57 loc) · 1.42 KB
/
LeapYearsBetweenYears.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.javamultiplex.datetime;
import java.util.Scanner;
//How to find all the leap years between two given years?
public class LeapYearsBetweenYears {
public static void main(String[] args) {
Scanner input=null;
try
{
input=new Scanner(System.in);
System.out.println("Enter lower limit of year : ");
String lower=input.next();
System.out.println("Enter upper limit of year : ");
String upper=input.next();
if(isValidYear(lower) && isValidYear(upper) && (lower.compareTo(upper)<0))
{
//Converting String to Integer
int year1=Integer.parseInt(lower);
int year2=Integer.parseInt(upper);
System.out.println("Leap years : ");
for(int i=year1;i<=year2;i++)
{
if(isLeapYear(i))
{
System.out.println(i);
}
}
}
else
{
System.out.println("Please enter valid year limits.");
}
}
finally
{
if(input!=null)
{
input.close();
}
}
}
private static boolean isValidYear(String year) {
boolean result = false;
// Regular expression that matches a String contains 4 digits.
String pattern = "[0-9]{4}";
if (year.matches(pattern)) {
result = true;
}
return result;
}
private static boolean isLeapYear(int myYear) {
boolean result = false;
if ((myYear%4==0 && myYear%100!=0) || myYear % 400 == 0) {
result = true;
}
return result;
}
}