Java example - determine whether a year is a leap year

Technical key:
    An integer that satisfies two conditions can be a leap year:
        1. Non-100 years can be divisible by 4;
        2. A hundred years are divisible by 400.
 
Java syntax:
year % 4 == 0 && year % 100 != 0 || year % 400 == 0
 
Implementation process:
1  package test;
 2  import java.util.Scanner;
 3  public  class LeapYear {
 4         public  static  void main(String[] args) {
 5               // TODO Auto-generated method stub 
6               Scanner scan = new Scanner(System.in);
 7               System.out.println("Please enter a year:" );
 8               long year = scan.nextLong();                                  // Receive user input 
9               if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ) {
 10                     System.out.println(year + "is a leap year!" );
 11               } else {
 12                      System.out.println(year + "is not a leap year" );
 13               }
 14         }
 15 }
Postscript: I couldn't understand why year % 100 != 0 at the beginning. After all, a hundred years divided by 100 must be equal to 0. After Baidu, I saw this sentence:
The rule for judging leap years in the Gregorian calendar is as follows: every four years, no leap year, no leap year, and every four hundred years.
A hundred years without leap! A hundred years without leap! A hundred years without leap! When you see this, you will understand.
 
In this example, there is an extended learning to determine the number of days that have passed during a specified date
 
Question: Please try to determine how many days have passed from January 1, 2000 to May 1, 2016.
 
Self-analysis:
    With the previous example to judge whether a year is a leap year, you can first judge the average leap year from 2000 to 2016, the average year is 365 days, the leap year is 366 days, and the for loop can be used to add them.
 
Implementation process:
 1 package test;
 2 
 3 import java.text.ParseException;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Calendar;
 6 import java.util.Date;
 7 
 8 public class CountDays {
 9     public static int differentDays(Date date1,Date date2) {
10         // TODO Auto-generated method stub
11         Calendar cal1 = Calendar.getInstance();
12         cal1.setTime(date1);
13         
14         Calendar cal2 = Calendar.getInstance();
15         cal2.setTime(date2);
16         
17         int day1 = cal1.get(Calendar.DAY_OF_YEAR);
18         int day2 = cal2.get(Calendar.DAY_OF_YEAR);
19         
20         int year1 = cal1.get(Calendar.YEAR);
21         int year2 = cal2.get(Calendar.YEAR);
22         
23         int days = 0;
24         for (int i = year1; i < year2; i++) {
25             if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {    //闰年
26                 days += 366;
27             } else {                                             //平年
28                 days += 365;
29             }
30         }
31         return days + (day2 - day1);
32     }
33 
34     /*
35      * 测试
36      */
37     public static void main(String[] args) {
38         String dateStr = "2000-1-1";
39         String dateStr2 = "2016-5-1";
40          SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd" );
 41          try {
 42              Date date1 = format.parse(dateStr);
 43              Date date2 = format.parse(dateStr2);
 44              System.out.println("2000 From January 1, 2016 to May 1, 2016, a total of: " + differentDays(date1,date2) + "days." );
 45          } catch (ParseException e) {
 46              e.printStackTrace();
 47          }
 48      }
 49 }
 
The running result is shown in the figure below, a total of 5965 days have passed.

I was not at ease, and used the most primitive method to list the days from 2010 to May 1, 2016 on paper. After adding them up, the result was indeed 5965 days.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325076465&siteId=291194637