How to get difference between two dates in years, months and days?

How do I calculate the number of days between two given dates? (Program provided)?

  • 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:

    This will make it VERY hard for you to find the exact amount of days between a date. You'd have to manually keep track of which months have 30 days and which months have 31 days. You'd have to keep track of leap-years manually. I don't recommend you to do this WHATSOEVER. What I do recommend you to do is using the calendar class, which I provided an example for: public static void main(String[] args) { //Make an instance of a calendar Calendar start = Calendar.getInstance(); //Set the time of the calendar. Remember though, in the calendar class, //you'll have to watch out with months. Because January would be 0, //February would be 1, March would be 2, etc. That's why I've set the //month to 01 instead of 02 like you did. start.set(2011, 01, 23); //Creating another instance of the calendar. This would be //your specified ending date Calendar end = Calendar.getInstance(); //Setting this ending date end.set(2011, 03, 15); //This is the calendar which we use to copy the starting calendar, because //contrary to the starting calendar, we need to alter this one so we clone start. Calendar dayCalculator = (Calendar) start.clone(); //The long in which we're gonna save the amount of days long amountOfDays = 0; //This part of the program checks if dayCalculator comes before end. If it does, it's going to //add 1 day to the dayCalculator, and 1 to the amountOfDays. It will keep adding 1 day to //our start clone, and 1 to our long amountOfDays until dayCalculator has reached end, and //they are now both equal dates. That ways you will have counted all the days from start //to end without using any kind of elaborate String techniques while (dayCalculator.before(end)) { dayCalculator.add(Calendar.DAY_OF_MO... 1); amountOfDays++; } //Here it will print the amount of days. You can alter this code to your liking. //I would suggest changing it into a separate method for convenience. System.out.println(amountOfDays); } Hope I helped. Good luck. EDIT: It's supposed to be Calendar.add(Calendar.DAY_OF_MONTH, 1) I don't know why Yahoo is changing the "MONTH" part into dots...

Vincent at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

This will make it VERY hard for you to find the exact amount of days between a date. You'd have to manually keep track of which months have 30 days and which months have 31 days. You'd have to keep track of leap-years manually. I don't recommend you to do this WHATSOEVER. What I do recommend you to do is using the calendar class, which I provided an example for: public static void main(String[] args) { //Make an instance of a calendar Calendar start = Calendar.getInstance(); //Set the time of the calendar. Remember though, in the calendar class, //you'll have to watch out with months. Because January would be 0, //February would be 1, March would be 2, etc. That's why I've set the //month to 01 instead of 02 like you did. start.set(2011, 01, 23); //Creating another instance of the calendar. This would be //your specified ending date Calendar end = Calendar.getInstance(); //Setting this ending date end.set(2011, 03, 15); //This is the calendar which we use to copy the starting calendar, because //contrary to the starting calendar, we need to alter this one so we clone start. Calendar dayCalculator = (Calendar) start.clone(); //The long in which we're gonna save the amount of days long amountOfDays = 0; //This part of the program checks if dayCalculator comes before end. If it does, it's going to //add 1 day to the dayCalculator, and 1 to the amountOfDays. It will keep adding 1 day to //our start clone, and 1 to our long amountOfDays until dayCalculator has reached end, and //they are now both equal dates. That ways you will have counted all the days from start //to end without using any kind of elaborate String techniques while (dayCalculator.before(end)) { dayCalculator.add(Calendar.DAY_OF_MO… 1); amountOfDays++; } //Here it will print the amount of days. You can alter this code to your liking. //I would suggest changing it into a separate method for convenience. System.out.println(amountOfDays); } Hope I helped. Good luck. EDIT: It's supposed to be Calendar.add(Calendar.DAY_OF_MONTH, 1) I don't know why Yahoo is changing the "MONTH" part into dots...

Ahmed

as usual

Saif-ul

as usual

Saif-ul

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.