Advertisements
Write a Java method that returns the number of days in a year using the following header:
public static int numberOfDaysInAYear (int year)
Write a Java program that uses your method and displays the number of days in a year from 2013 to 2025 as a table. Include a title line for your table that looks like:
YEAR #DAYS
This is what i have so far and i have no clue what to do next
public class Daysinayear { public static void main(String[] args) { System.out.println(years) } public static int numberOfDaysInAYear (int years){ int numberofdays=0;
for(years=2013;years<=2025;years++) if(isLeapYear(years)) numberofdays= numberofdays + 366; else numberofdays= numberofdays +365; return numberofdays; } public static boolean isLeapYear(int year) { return isLeapYear = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)); } }
Hi,
Here is the example code:
package net.roseindia; import java.util.*; public class DaysInYear { static int getDaysinYear(int year) { int days = 0; Calendar fromDate = new GregorianCalendar(); Calendar toDate = new GregorianCalendar(); fromDate.set(year, 1, 1); toDate.set(year, 12, 31); long diff = toDate.getTimeInMillis() - fromDate.getTimeInMillis(); float dayCount = (float) diff / (24 * 60 * 60 * 1000); days = (int) dayCount; return days; } public static void main(String[] args) { for (int years = 2013; years <= 2025; years++) { System.out.println("No of days in the Year: " + years + " is: " + DaysInYear.getDaysinYear(years)); } } }
Read more tutorials at Java Date Examples.
Thanks