What is the best way to calculate a date difference?

Calculate the difference between two dates in Java programming?

  • I am stuck trying to figure this one out. Any help/hints/suggestions are more than appreciated. I typed the program out below all by myself and it's all that I have so far... It can compile and run, but I'm having difficulty trying to make it calculate the difference between two given dates. import java.util.Scanner; /** * Days determines the number of days between two dates. * @author Vince Luc */ public class Days { public static void main(String[] args) { String start = "02/23/2011"; // replace with input String end = "04/15/2011"; // replace with input System.out.printf("Days between %s and %s are %d.\n", start, end, daysBetweenDates(start, end)); Scanner kbd = new Scanner(System.in); Scanner input = new Scanner(System.in); int month; int day; int year; start = "02/23/2011"; end = "04/15/2011"; System.out.print("Please enter value of a year: "); year = input.nextInt(); if (isLeapYear(year)) System.out.println("The year " + year + " is a leap year."); else System.out.println("The year " + year + " is not a leap year."); } /** * isLeapYear determines whether a year is a leap year or not * @param year (int) Years since recognized beginning of C.E. * @return (boolean) true if year divisible by 4 (but not 100) */ public static boolean isLeapYear(int year) { boolean valueToReturn; if (year % 4 == 0 && year % 100 != 0) valueToReturn = true; else if (year % 400 == 0) valueToReturn = true; else valueToReturn = false; return valueToReturn; } /** * daysInMonth looks up the number of days in the specified month * @param month (int) a number between 1 and 12 indicating the desired month * @param year (int) the year is only meaningful for February (2) * @return (int) the last day of month/year (or 0 if invalid month provided) */ public static int daysInMonth(int month, int year) { int daysToReturn; System.out.println("calculating days of month " + month + "in year " + year); switch (month) { case 1: daysToReturn = 31; break; case 3: daysToReturn = 31; break; case 4: daysToReturn = 30; break; case 5: daysToReturn = 31; break; case 6: daysToReturn = 30; break; case 7: daysToReturn = 31; break; case 8: daysToReturn = 31; break; case 9: daysToReturn = 30; break; case 10: daysToReturn = 31; break; case 11: daysToReturn = 30; break; case 12: daysToReturn = 31; break; // finish code for cases 1, 3, 5, 7, 8, 10, and 12 case 2: if (isLeapYear(year)) daysToReturn = 29; else daysToReturn = 28; break; default: daysToReturn = 0; // It should not occur } return daysToReturn; } /** * daysIntoYear translates a date (month, day, year) into a single number * representing the days since 12/31/(year-1). * @param month (int) 1-12, where 1 is January and 12 is December * @param day (int) 1-31, or actually up to daysInMonth * @param year (int) years since start of C.E. * @return (int) number of days between 12/31/(year-1) and (month)/(day)/(year) */ public static int daysIntoYear(int month, int day, int year) { if ( isLeapYear(year) ) return 366; else return 365; } /** * daysBetweenDates calculates number of days between two dates. * @param start (String) starting date * @param end (String) ending date, presumably later than start * @return (int) days between start date and end date */ public static int daysBetweenDates(String start, String end) { int days = 0; System.out.println("calculating daysBetweenDates"); return days; } }

  • Answer:

    The variables need to be calculated first.

Vincent at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

The variables need to be calculated first.

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.