Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: Leap year

  1. #1
    Junior Member
    Join Date
    Apr 2022
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Leap year

    Hello everyone, and again a novice developer with you)))
    Tell me how to push my code into a loop so that it displays all the high years for the last 10 years)
    I know how to solve the problem without class Time, but the teacher asks that the class be used Time.

    import java.time.*;
     
    public class LeapYear {
     
     
        public static void main(String[] args) {
            boolean leapYearCheck = LocalDate.parse("2012-01-01").isLeapYear();
            System.out.println(leapYearCheck);
     
            }
        }
    Last edited by amnez1ya; April 11th, 2022 at 04:44 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Leap year

    What package is the Time class in?

    What have you tried?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    amnez1ya (April 10th, 2022)

  4. #3
    Junior Member
    Join Date
    Apr 2022
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Leap year

    is in the package java.time.*
    im tried

    import java.util.Scanner;
     
    public class Year {
        static Scanner in = new Scanner(System.in);
     
        public static void main(String[] args) {
     
            int startYear, endYear;
     
            System.out.print("Enter the Start Year:");
            startYear = in.nextInt();
     
            System.out.print("Enter the End Year:");
            endYear = in.nextInt();
     
            System.out.println("Leap years:");
     
            for (int i = startYear; i <= endYear; i++) {
     
                if ((0 == i % 4) && (0 != i % 100) || (0 == i % 400)) {
                    System.out.println(i);
                }
            }
        }
    }

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Leap year

    in the package java.time.*
    Sorry, I do not see a class named Time in the java.time package. Are you sure that is the correct package?

    What version of the JDK are you using?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Leap year

    I do not see a class called Time in that package. Is Time the correct name?
    What package is the Time class in that your teacher wants you to use?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Apr 2022
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Leap year

    I'm wildly sorry, use classes from the package Time
    my head is already boiling

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Leap year

    Ok, what class(es) are you allowed to use?
    What is wrong with the class and method used in the first post? It uses a class from the java.time package.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Apr 2022
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Leap year

    It is allowed to use the whole package, but I need to display all high years in the last 10 years, but using the classes of this package, and not just like I did without this package.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Leap year

    display all high years in the last 10 years
    Ok, that sounds like the code needs a loop for the last 10 years
    and a way to select the high years. What is a high year?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Apr 2022
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Leap year

    A Leap/high year has 366 days and a regular year has 365.))
    Last edited by amnez1ya; April 11th, 2022 at 09:44 AM.

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Leap year

    Ok, the posted code can detect a leap year.
    Now there needs to be a loop to cover the last 10 years.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Junior Member
    Join Date
    Apr 2022
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Leap year

    Yes, that's right, nothing comes to mind with this package ((

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Leap year

    Can you make a loop that starts with a year that is 10 years old and increments it by one year until its value is the present year?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Junior Member
    Join Date
    Apr 2022
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Leap year

    This is the question I asked you

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Leap year

    What have you tried?
    Do you know how to write a loop? Start with that. Have a year value (eg 2012) that is 10 years old and increment it until its value is this year (2022).
    If you don't understand my answer, don't ignore it, ask a question.

  18. #17
    Junior Member
    Join Date
    Apr 2022
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Leap year

    Thanks for the help, I solved the problem)

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Leap year

    What classes and methods did you use?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #19
    Junior Member
    Join Date
    Apr 2022
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Leap year

    public static void main(String[] args) {
            LocalDate leapYear = LocalDate.now();
            int i = LocalDate.now().getYear();
            while (i > 2012) {
                leapYear = leapYear.minusYears(1);
                if (leapYear.isLeapYear())
                    System.out.println(leapYear);
                i--;
            }
        }

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Leap year

    Thanks for sharing your solution.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Small webapp that determine whether a year is a leap year.
    By frankhvam in forum Object Oriented Programming
    Replies: 0
    Last Post: October 26th, 2013, 03:32 PM
  2. Leap Year/Calendar Program. toString() method error.
    By jerryg in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 20th, 2012, 06:59 PM
  3. Problem with My Leap Year Calculation
    By LittleGirl in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 7th, 2012, 10:57 AM
  4. Leap year program
    By cyspope in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 22nd, 2010, 10:20 PM