/** * Days Between Dates: Calculate days between dates * * COURSE: COMP 1010 SECTION: D01-OL * INSTRUCTOR: Mohammad Shorfuzzaman * ASSIGNMENT: 3 QUESTION: 2 * @author Richard Mitsuk Lavitt, 7607877 * @version 2.28.08 */ // allow input import java.util.Scanner; public class daysBetweenDates { public static void main(String[] args) { // Print application information System.out.println("---------------------------------------- "); System.out.println("COURSE: COMP 1010 SECTION: D01-OL"); System.out.println("INSTRUCTOR: Mohammad Shorfuzzaman "); System.out.println("ASSIGNMENT: 3 QUESTION: 2 "); System.out.println("AUTHOR: Richard Mitsuk Lavitt, 7607877"); System.out.println("VERSION: 2.28.08 "); System.out.println("---------------------------------------- "); String dateone; // these two are input String datetwo; int dayone; // for manipulation of our dates int monthone; int yearone; int daytwo; int monthtwo; int yeartwo; int daysone; int daystwo; int difference; // the difference between our two dates Scanner keyboard = new Scanner(System.in); // set up keyboard input System.out.println("Please input first date in 8 digit MMDDYYYY format."); dateone = keyboard.nextLine(); System.out.println("Please input second date in 8 digit MMDDYYYY format."); datetwo = keyboard.nextLine(); // extract dates from input monthone = Integer.parseInt(dateone.substring(0,2)); dayone = Integer.parseInt(dateone.substring(2,4)); yearone = Integer.parseInt(dateone.substring(4,8)); monthtwo = Integer.parseInt(datetwo.substring(0,2)); daytwo = Integer.parseInt(datetwo.substring(2,4)); yeartwo = Integer.parseInt(datetwo.substring(4,8)); // allow us to manipulate them daysone = convertDateToDays(dayone,monthone,yearone); daystwo = convertDateToDays(daytwo,monthtwo,yeartwo); // do our calculation difference = daystwo - daysone; // make it positive //if(difference < 0) //difference = difference * -1; // Output System.out.println("Date one: " + monthone + " " + dayone + " " + yearone); System.out.println("Date two: " + monthtwo + " " + daytwo + " " + yeartwo); System.out.println("The difference is " + difference); // Confirm success System.out.println("End of processing."); } /* Calculate days from January 1 */ public static int convertDateToDays (int day, int month, int year) { final int LONGMONTH = 31; final int SHORTMONTH = 30; int FEB = 28; int days = day; int iteration; int loophold; if(isLeapYear(year)) FEB = 29; iteration = month; // initialize our loop variable while (iteration > 0) { if(month == 2 || month == 4 || month == 6 || month == 8 || month == 9 || month == 11) days = days + LONGMONTH; if(month == 3) days = days + FEB; if(month == 5 || month == 7 || month == 10 || month == 12) days = days + SHORTMONTH; if(month == 1) break; iteration--; } return(days); } /* Check if it is a leap year */ public static boolean isLeapYear (int year) { boolean status = false; final int DIVISOR_ONE = 4; // these numbers fit into the year in different ways depending on its status final int DIVISOR_TWO = 100; final int INDIVISOR = 400; if(year % DIVISOR_ONE == 0) { if(year % DIVISOR_TWO == 0 && year % INDIVISOR != 0) status = false; else status = true; } return(status); } }