Java calculate days in year, or between two dates

前端 未结 9 1655
慢半拍i
慢半拍i 2021-02-07 04:48

Is there a method in any native Java class to calculate how many days were/will be in a specific year? As in, was it a Leap year (366 days) or a normal year (365 days)?

9条回答
  •  时光说笑
    2021-02-07 05:13

    You can look at the Wikipedia page for some very nice pseudocode:

    if year modulo 400 is 0
           then is_leap_year
    else if year modulo 100 is 0
           then not_leap_year
    else if year modulo 4 is 0
           then is_leap_year
    else
           not_leap_year
    

    I'm sure you can figure out how to implement that logic in Java. :-)

提交回复
热议问题