3 Replies - 13671 Views - Last Post: 25 February 2009 - 06:39 PM Rate Topic: -----

#1 nonrevving  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 13-February 09

Number of Days between two dates

Post icon  Posted 24 February 2009 - 10:25 PM

I'm working on a java project that requires me to determine the number of days between two dates for a financial calculation. We are not allowed to use any nifty importCalendar or anything like that; we can only use if statements and loops. I've been toying with it for a couple days now, and it's pretty messy and works in some cases, but is off a few days in others. I'm just getting confused looking at my code, so could anybody test it out and let me know if you find any logic errors? Thank you so much!!
//prompt for date variables
String day1 = JOptionPane.showInputDialog(
					"<html>Interest Compounded Daily Function<br>" +
					"Please enter today's day:");
			String month1 = JOptionPane.showInputDialog(
					"<html>Interest Compounded Daily Function<br>" +
					"Please enter today's month (in number form):");
			String year1 = JOptionPane.showInputDialog(
					"<html>Interest Compounded Daily Function<br>" +
					"Please enter today's year:");
			String day2 = JOptionPane.showInputDialog(
					"<html>Interest Compounded Daily Function<br>" +
					"Please the future day:");
			String month2 = JOptionPane.showInputDialog(
					"<html>Interest Compounded Daily Function<br>" +
					"Please the future month (in number form):");
			String year2 = JOptionPane.showInputDialog(
					"<html>Interest Compounded Daily Function<br>" +
					"Please the future year:");
			//convert date variables:
			int d1 = Integer.parseInt(day1);
			int m1 = Integer.parseInt(month1);
			int y1 = Integer.parseInt(year1);
			int d2 = Integer.parseInt(day2);
			int m2 = Integer.parseInt(month2);
			int y2 = Integer.parseInt(year2);
			//declare calculation variables
			int sumyd1 = 0, summd = 0, sumdd= 0, sumnd = 0;
			int yeardiff=0;
			int monthdiff=0;
			//set yeardiff and monthdiff values
			if(m2>m1){
				monthdiff=m2-m1;
				yeardiff=y2-y1;
			}
			else if (m1>m2){
				monthdiff=(m2+12)-m1;
				yeardiff=(y2-1)-y1;
			}
			else{
				monthdiff=0;
				yeardiff=y2-y1;
			}
			int daysinamonth=0;
			//daysinamonth values, for when d1>d2
			if(((m2-1)==1)|((m2-1)==3)|((m2-1)==5)|((m2-1)==7)
					|((m2-1)==8)|((m2-1)==10)|((m2-1)==0)){
				daysinamonth=31;
			}
			else if(((m2-1)==4)|((m2-1)==6)|((m2-1)==9)|((m2-1)==11)){
				daysinamonth=30;
			}
			else if(((m2-1)==2)&((y1%4)==0)&((y1%100)!=0)
					| ((y1%400)==0)){
				daysinamonth=29;
			}
			else{
				daysinamonth=28;
			}
			//calculate number of days, for years
			if ((y2>y1)&((m2>m1)|(m2==m1))){
				for (int counter=1; counter<=yeardiff; counter++){
					if((((y1+counter)%4)==0)&(((y1+counter)%100)!=0)
						| (((y1+counter)%400)==0)){
						sumyd1=366+sumyd1;
					}
					else{
						sumyd1=365+sumyd1;
					}//end else
				}//end for loop
			}//end if statement
			else if ((y2>y1+1)&(m1>m2)){
				for (int counter=0; counter<yeardiff; counter++){
					if((((y1+counter)%4)==0)&(((y1+counter)%100)!=0)
							| (((y1+counter)%400)==0)){
					sumyd1=366+sumyd1;
					}
					else{
					sumyd1=365+sumyd1;
					}
			}//end for loop
			}//end if statement
			else if((y2==y1+1)&(m1>m2)){
				sumyd1=0;
			}
			else{
				sumyd1=0;
			}
			//calculate number of days, for months
			if((m2>m1)&(d2>d1)){
				for (int counter=1; counter<=monthdiff; counter++,m1++){
					if((m1==1)|(m1==3)|(m1==5)|(m1==7)
						|(m1==8)|(m1==10)|(m1==12)){
						summd=31+summd;
					}
					else if((m1==4)|(m1==6)|(m1==9)|(m1==11)){
						summd=30+summd;
					}
					else if ((m1==2)&((((y1)%4)==0)&(((y1)%100)!=0)
						| (((y1)%400)==0))){
						summd=29+summd;
					}
					else{
						summd=28+summd;
					}//end else
				}//end for loop
			}//end if for months m2>m1 and d2>d1
			//if m2>m1 and d1>d2
			else if((m2>m1)&(d1>d2)){
				for (int counter=1; counter<monthdiff; counter++,m1++){
					if((m1==1)|(m1==3)|(m1==5)|(m1==7)
						|(m1==8)|(m1==10)|(m1==12)){
						summd=31+summd;
					}
					else if((m1==4)|(m1==6)|(m1==9)|(m1==11)){
						summd=30+summd;
					}
					else if ((m1==2)&((((y1)%4)==0)&(((y1)%100)!=0)
						| (((y1)%400)==0))){
						summd=29+summd;
					}
					else{
						summd=28+summd;
					}//end else
				}//end for loop
			}//end if months m2>m1 and d1>d2
			//if m1>m2 and d2>d1
			else if ((m1>m2)&(d2>d1)){
				for (int counter=1; counter<=monthdiff; counter++, m1++){
					if((m1==1)|(m1==3)|(m1==5)|(m1==7)
						|(m1==8)|(m1==10)|(m1==12)){
						summd=31+summd;
					}
					else if((m1==4)|(m1==6)|(m1==9)|(m1==11)){
						summd=30+summd;
					}
					else if ((m1==2)&((((y1)%4)==0)&(((y1)%100)!=0)
						| (((y1)%400)==0))){
						summd=29+summd;
					}
					else{
						summd=28+summd;
					}
					if (m1==12){
						m1=0;
					}
					}
			}//end else
			//if m1>m2 and d1>d2
			else if ((m1>m2)&(d1>d2)){
				for (int counter=1; counter<monthdiff; counter++, m1++){
					if((m1==1)|(m1==3)|(m1==5)|(m1==7)
						|(m1==8)|(m1==10)|(m1==12)){
						summd=31+summd;
					}
					else if((m1==4)|(m1==6)|(m1==9)|(m1==11)){
						summd=30+summd;
					}
					else if ((m1==2)&((((y1)%4)==0)&(((y1)%100)!=0)
						| (((y1)%400)==0))){
						summd=29+summd;
					}
					else{
						summd=28+summd;
					}
					if (m1==12){
						m1=0;
					}
					}
			}//end else
			else{
				summd=0;
			}//end else
				//calculate number of days for days
				if ((d2>d1)){
					sumdd=d2-d1;
				}
				else if (d2==d1){
					sumdd=0;
				}
				else if (d1>d2){
					sumdd=(daysinamonth-d1)+d2;
				}
			sumnd=sumyd1+summd+sumdd;
			JOptionPane.showMessageDialog(null, sumnd);


please let me know if the code does not display properly

Is This A Good Question/Topic? 0
  • +

#3 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Number of Days between two dates

Posted 25 February 2009 - 12:58 AM

The only logic problems I see in the code are the misuse of the bitwise- | and & operators in conditional statements:

if(((m2-1)==1)|((m2-1)==3)|((m2-1)==5)|((m2-1)==7)
.

I think you meant to use || and &&.

Here is a days calculator program that I wrote if it's of any help:

/*
 * File: DaysBetweenDates.java
 *
 * Program: Days Calculator
 *
 * Purpose: This program calculates the number of days between two dates.
 *
 * Author: Brenton Andrew Saunders (brentonsaunders@hotmail.com)
 *
 * Date: 25-Feb-09
 *
 * Revision History: N/A
 *
 */

import java.io.*;
import java.util.*;

public class DaysBetweenDates
{
	public static void main(String [] args)
	{
		int startingMonth;
		int startingDay;
		int startingYear;
		int endingMonth;
		int endingDay;
		int endingYear;
		int daysInBetween;

		System.out.println("This program calculates the number of days between two dates.\n");
		System.out.println("Enter the starting date:");

		startingMonth = getMonth();
		startingDay   = getDay();
		startingYear  = getYear();

		System.out.println("Enter the ending date:");

		endingMonth = getMonth();
		endingDay   = getDay();
		endingYear  = getYear();

		daysInBetween = getDaysBetweenDates(startingMonth, startingDay, startingYear, endingMonth,
										   endingDay, endingYear);

		System.out.println("Days between " + startingMonth + "/" + startingDay + "/" + startingYear +
						   " and " + endingMonth + "/" + endingDay + "/" + endingYear + ": "
						   + daysInBetween);
	}

	private static int getMonth()
	{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int month = 0;

		try {
			do {
				System.out.print("Enter a month number (MM): ");

				month = Integer.parseInt(in.readLine());

				if(!isValidMonth(month))
					System.out.println("Invalid month.");
			} while(!isValidMonth(month));

		} catch(IOException ioe) {
		}

		return month;
	}

	private static int getDay()
	{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int day = 0;

		try {
			do {
				System.out.print("Enter a day (DD): ");

				day = Integer.parseInt(in.readLine());

				if(!isValidDay(day))
					System.out.println("Invalid day.");
			} while(!isValidDay(day));

		} catch(IOException ioe) {
		}

		return day;
	}

	private static int getYear()
	{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int year = 0;

		try {
			do {
				System.out.print("Enter a four-digit year (YYYY): ");

				year = Integer.parseInt(in.readLine());

				if(!isValidYear(year))
					System.out.println("Invalid year.");
			} while(!isValidYear(year));

		} catch(IOException ioe) {
		}

		return year;
	}

	private static int getDaysBetweenDates(int startingMonth,
										   int startingDay,
										   int startingYear,
										   int endingMonth,
										   int endingDay,
										   int endingYear)
	{
		int days = 0;

		while(startingYear < endingYear) {
			days += getDaysUntilEndOfYear(startingMonth, startingDay, startingYear);
			startingYear ++;
			startingMonth = 1;
			startingDay = 1;
		}

		while(startingMonth < endingMonth) {
			days += getDaysUntilEndOfMonth(startingMonth, startingDay, startingYear);
			startingMonth ++;
			startingDay = 1;
		}

		if(startingDay < endingDay)
			days += getDaysSinceBeginningOfMonth(endingMonth, endingDay);

		return days;
	}


	private static boolean isLeapYear(int year)
	{
		if((year % 4) == 0) {
			if((year % 100) == 0) {
				if((year % 400) == 0)
					return true;
			}
			else
				return true;
		}

		return false;
	}

	private static boolean isValidMonth(int month)
	{
		return (month >= 1 && month <= 12);
	}

	private static boolean isValidDay(int day)
	{
		return (day >= 1 && day <= 31);
	}

	private static boolean isValidYear(int year)
	{
		return (year >= 1000 && year <= 9999);
	}

	private static int getDaysUntilEndOfMonth(int month, int day, int year)
	{
		int daysInMonth;

		daysInMonth = getDaysInMonth(month, year);

		if(day == 1)
			return daysInMonth;

		return daysInMonth - day;
	}

	private static int getDaysUntilEndOfYear(int month, int day, int year)
	{
		int days = 0;

		while(month <= 12) {
			days += getDaysUntilEndOfMonth(month, day, year);
			month ++;
			day = 1;
		}

		return days;
	}

	private static int getDaysSinceBeginningOfMonth(int month, int day)
	{
		return day;
	}

	private static int getDaysSinceBeginningOfYear(int month, int day, int year)
	{
		int days = 0;
		int lastMonth = 1;

		while(lastMonth < month) {
			days += getDaysInMonth(lastMonth, year);
			lastMonth ++;
		}

		days += day;

		return days;
	}

	private static int getDaysInMonth(int month, int year)
	{
		int days = 0;

		if(month == 1  ||
		   month == 3  ||
		   month == 5  ||
		   month == 7  ||
		   month == 8  ||
		   month == 10 ||
		   month == 12)
			days = 31;
		else if(month == 4 ||
				month == 6 ||
				month == 9 ||
				month == 11)
			days = 30;
		else if(month == 2) {
			if(isLeapYear(year))
				days = 29;
			else
				days = 28;
		}

		return days;
	}
}

This post has been edited by bsaunders: 25 February 2009 - 01:04 AM

Was This Post Helpful? 1

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: Number of Days between two dates

Posted 25 February 2009 - 04:56 PM

View Postnonrevving, on 24 Feb, 2009 - 09:25 PM, said:

We are not allowed to use any nifty importCalendar or anything like that;

You mean you are paid to re-invent the wheel
What is the address so I can apply ?
Whithout using what you call "nifty" stuff you can create 2 sql.Date object
get the number of milli
make the difference
divide the difference by 1000 then 60 then 60 then 24

And by the way your isLeapYear() method does not take care of the millenium which are leap even if multiple of 400 or the contrary od.. somethink like that... or.. anyway don't remember I use the Java Calendar

This post has been edited by pbl: 25 February 2009 - 06:53 PM

Was This Post Helpful? 0
  • +
  • -

#5 nonrevving  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 13-February 09

Re: Number of Days between two dates

Posted 25 February 2009 - 06:39 PM

ah it was a project for a class. I just went with what I got. thanks to all who replied!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1