Java instance--determining whether a year is a leap years

Source: Internet
Author: User
Tags date1 stub

Technical key: integers that meet both conditions can become leap years: 1, not the whole hundred years can be divisible by 4;2, the whole hundred years can be divisible by 400. Java syntax: Year% 4 = = 0 && Year% 100! = 0 | | Year% 400 = = 0 Implementation process:
1  Packagetest;2 ImportJava.util.Scanner;3  Public classLeapyear {4         Public Static voidMain (string[] args) {5              //TODO auto-generated Method Stub6Scanner scan =NewScanner (system.in);7System.out.println ("Please enter a year:");8              LongYear = Scan.nextlong ();//Receive user input9              if(year% 4 = = 0 && year%! = 0 | | year% 400 = = 0) {TenSYSTEM.OUT.PRINTLN (Year + "is a leap year!) "); One}Else { ASYSTEM.OUT.PRINTLN (year + "not a leap year"); -              } -        } the}
PostScript: At first can not understand why year% 100! = 0 This condition, after all the whole hundred years divided by 100 certainly equals 0 ah, Baidu a bit, see this phrase:The rule of judging the Gregorian leap year is as follows: Four years a leap, a century does not leap, 400 years again leap. Hundred years does not leap! Hundred years does not leap! Hundred years does not leap! See this to understand. There is an extended learning in this instance,determine the number of days elapsed between a specified dateQuestion: Try to determine how many days you have experienced from January 1, 2000 to May 1, 2016. The book did not give the answer, but I myself analysis: With the previous instance to determine whether a year is a leap years before, you can judge 2000-2016 years flat leap, common year is 365 days, leap year is 366 days, with a for loop Add. Implementation process:
1  Packagetest;2 3 Importjava.text.ParseException;4 ImportJava.text.SimpleDateFormat;5 ImportJava.util.Calendar;6 Importjava.util.Date;7 8  Public classCountdays {9      Public Static intdifferentdays (Date date1,date date2) {Ten         //TODO auto-generated Method Stub OneCalendar Cal1 =calendar.getinstance (); A Cal1.settime (date1); -          -Calendar Cal2 =calendar.getinstance (); the Cal2.settime (date2); -          -         intDay1 =Cal1.get (calendar.day_of_year); -         intDay2 =Cal2.get (calendar.day_of_year); +          -         intYEAR1 =Cal1.get (calendar.year); +         intYEAR2 =Cal2.get (calendar.year); A          at         intDays = 0; -          for(inti = year1; i < year2; i++) { -             if(i% 4 = = 0 && i%! = 0 | | I% 400 = = 0) {//Leap Year -Days + = 366; -}Else{//Common year -Days + = 365; in             } -         } to         returnDays + (Day2-day1); +     } -  the     /* * * Test $      */Panax Notoginseng      Public Static voidMain (string[] args) { -String datestr = "2000-1-1"; theString dateStr2 = "2016-5-1"; +SimpleDateFormat format =NewSimpleDateFormat ("Yyyy-mm-dd"); A         Try { theDate date1 =Format.parse (DATESTR); +Date Date2 =Format.parse (DATESTR2); -System.out.println ("January 1, 2000 to May 1, 2016 altogether experienced:" + differentdays (date1,date2) + "days. "); $}Catch(ParseException e) { $ e.printstacktrace (); -         } -     } the}
The results of the operation were as shown, which lasted 5,965 days altogether.

I am not at ease, and in the most primitive way, the number of days from 2010 to 2016 May 1 is listed on the paper, and the result is really 5,965 days.

Java instance--determining whether a year is a leap years

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.