The problem Write a method that returns the number of days in a year using the following header public static int numberOfDaysInAYear(int year)
or use the java date time library. Write a test program that displays the number of days in year from 2000 to 2020.
Breaking it down public static void main ( String [] args ) {
IntStream . rangeClosed ( 2000 , 2020 ). forEach (
i -> {
int numberOfDaysInYear = 0 ;
if ( Year . of ( i ). isLeap ()) {
numberOfDaysInYear = 366 ;
} else {
numberOfDaysInYear = 365 ;
}
System . out . println ( i + " has " + numberOfDaysInYear
+ " total number of days" );
});
}
Output 2000 has 366 total number of days
2001 has 365 total number of days
2002 has 365 total number of days
2003 has 365 total number of days
2004 has 366 total number of days
2005 has 365 total number of days
2006 has 365 total number of days
2007 has 365 total number of days
2008 has 366 total number of days
2009 has 365 total number of days
2010 has 365 total number of days
2011 has 365 total number of days
2012 has 366 total number of days
2013 has 365 total number of days
2014 has 365 total number of days
2015 has 365 total number of days
2016 has 366 total number of days
2017 has 365 total number of days
2018 has 365 total number of days
2019 has 365 total number of days
2020 has 366 total number of days
Number of days in a year posted by Justin Musgrove on 23 April 2016
Tagged: java, java-exercises-beginner, intro-to-java-10th-edition, and ch6
Share on: Facebook Google+