-3

See the example at this link To check leap year between two years

public class LeapYears {
   public static void main(String[] args) {
 
      GregorianCalendar cal = new GregorianCalendar();
 
      System.out.println("Leap years between 1975 and 2015:");
      for (int year = 1975; year < 2015; year++) {
         if (cal.isLeapYear(year)) {
            System.out.println(year);
         }
      }
   }
}
Questioner
  • 31
  • 3
  • 3
    Didn't you [just ask this same question](https://stackoverflow.com/q/68336754/522444), one that was answered as well, an answer that you simply ignored, and then deleted the question. Why should anyone else help if you will just delete your question and any answers provided? – Hovercraft Full Of Eels Jul 11 '21 at 15:51

2 Answers2

1

java.time

The java.util Date-Time API is outdated and error-prone. It is recommended to stop using it completely and switch to the modern Date-Time API*.

  1. You can use java.time.Year#isLeap to check if the year is a leap year.
  2. For testing, you can get the input from the keyboard using the Scanner class.

Demo:

import java.time.Year;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the first year: ");
        int first = scanner.nextInt();

        System.out.print("Enter the last year: ");
        int last = scanner.nextInt();

        System.out.println("Leap years between 1975 and 2015:");
        for (int year = first; year <= last; year++) {
            if (Year.of(year).isLeap()) {
                System.out.println(year);
            }
        }
    }
}

A sample run:

Enter the first year: 1975
Enter the last year: 2012
Leap years between 1975 and 2015:
1976
1980
1984
1988
1992
1996
2000
2004
2008
2012

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

Please note that I have written this program using Streams and tried to use the logic in filter. I hope the program is not too convoluted.

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.IntStream;
import java.util.stream.Collectors;

class Solution {

    static IntStream createYearStream(LocalDateTime from, LocalDateTime to) {
       int yearStart= LocalDateTime.from(from).getYear();
        int yearEnd=LocalDateTime.from(to).getYear();
        //TODO - Create years stream

        return IntStream.range(yearStart,yearEnd);
    }

    static List<Integer> getAllLeapYear(IntStream yearRange) {
        List<Integer> leapYears = new ArrayList<Integer>();
        leapYears= yearRange.filter(x->(x%4==0||(x%100==0&&x%400==0) )).boxed().collect(Collectors.toList());

        return leapYears;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String from = sc.next();//From Year
        String to = sc.next(); //To Year
        LocalDateTime fromDate = LocalDateTime.parse(from);
        LocalDateTime toDate = LocalDateTime.parse(to);
        List<Integer> leapYears = getAllLeapYear(createYearStream(fromDate, toDate));
        System.out.println(leapYears.size());
    }
}