Calculate the difference between two dates?
-
My program can Compile and Run, but when I Run it, I always get 0 as my result? Why is this? MY PROGRAM BELOW: 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)); } /** * 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) { System.out.println("in isLeapYear"); return false; } /** * 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) { System.out.println("calculating days in a month"); return 30; } /** * 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) { System.out.println("calculating days into year"); return 1; } /** * 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:
Your program always returns 0 because in your daysBetweenDates method, you declare 'int days = 0', then you return it without doing any calculations. There are much simpler ways to go about what you are doing. You should check out the Calendar class. To find out how many days are in a month, you can do something like this: public static int daysInMonth(int month, int year) { //the Calendar class starts jan at 0, feb is 1, etc month--; Calendar calendar = Calendar.getInstance(); int day = 1; calendar.set(year, month, day); int days = calendar.getActualMaximum( Calendar.DAY_OF_MONTH ); } If you want to test if it is a leap-year, just check the number of days in February of that year. For example, public static boolean isLeapYear(int year) { return daysInMonth(2, year) == 29; } As far as actually calculating the number of days between two dates, I would recommend using the Date class to help you out. You can convert a Date to a timestamp (stored in milliseconds). Then just divide the result out to give you an accurate number of days. Here is a working example: //the try catch is necessary because DateFormat's parse method will throw a ParseException //error if the incorrect format is used. try { //get the user input System.out.println("Enter date in the format 'MM/DD/YYYY'"); Scanner console = new Scanner(System.in); String date1 = console.nextLine(); System.out.println("Enter another date in the same format."); String date2 = console.nextLine(); //create a date format to use DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); //convert the input to dates Date firstDate = dateFormat.parse(date1); Date secondDate = dateFormat.parse(date2); //extract the time from each date long time1 = firstDate.getTime(); long time2 = secondDate.getTime(); //1000ms in a second, 60 seconds in a minute, 60 minutes in an hour //24 hours in a day long days = (time1 - time2) / (1000 * 60 * 60 * 24); //you can take the absolute value so you don't have a negative difference days = Math.abs(days); System.out.printf("There are %d days between '%s' and '%s'.", days, date1, date2); } catch (ParseException ex) { System.out.println("Sorry, the date you entered was not in the right format."); }
Vincent at Yahoo! Answers Visit the source
Other answers
Your program always returns 0 because in your daysBetweenDates method, you declare 'int days = 0', then you return it without doing any calculations. There are much simpler ways to go about what you are doing. You should check out the Calendar class. To find out how many days are in a month, you can do something like this: public static int daysInMonth(int month, int year) { //the Calendar class starts jan at 0, feb is 1, etc month--; Calendar calendar = Calendar.getInstance(); int day = 1; calendar.set(year, month, day); int days = calendar.getActualMaximum( Calendar.DAY_OF_MONTH ); } If you want to test if it is a leap-year, just check the number of days in February of that year. For example, public static boolean isLeapYear(int year) { return daysInMonth(2, year) == 29; } As far as actually calculating the number of days between two dates, I would recommend using the Date class to help you out. You can convert a Date to a timestamp (stored in milliseconds). Then just divide the result out to give you an accurate number of days. Here is a working example: //the try catch is necessary because DateFormat's parse method will throw a ParseException //error if the incorrect format is used. try { //get the user input System.out.println("Enter date in the format 'MM/DD/YYYY'"); Scanner console = new Scanner(System.in); String date1 = console.nextLine(); System.out.println("Enter another date in the same format."); String date2 = console.nextLine(); //create a date format to use DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); //convert the input to dates Date firstDate = dateFormat.parse(date1); Date secondDate = dateFormat.parse(date2); //extract the time from each date long time1 = firstDate.getTime(); long time2 = secondDate.getTime(); //1000ms in a second, 60 seconds in a minute, 60 minutes in an hour //24 hours in a day long days = (time1 - time2) / (1000 * 60 * 60 * 24); //you can take the absolute value so you don't have a negative difference days = Math.abs(days); System.out.printf("There are %d days between '%s' and '%s'.", days, date1, date2); } catch (ParseException ex) { System.out.println("Sorry, the date you entered was not in the right format."); }
Steven
Related Q & A:
- How to search events between two dates in WP?Best solution by WordPress
- How to calculate the intersection of two polygons?Best solution by Stack Overflow
- What is the best way to calculate a date difference?Best solution by Stack Overflow
- What is the difference between two way and one way security systems in car alarms?Best solution by Yahoo! Answers
- What is the difference between these two snowboards?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.