4 Replies - 1605 Views - Last Post: 11 October 2012 - 02:26 PM Rate Topic: -----

#1 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

The difference of two dates is not correct

Posted 11 October 2012 - 12:34 PM

I have to make this program that finds the difference of two dates to print out on an overdue notice. I am getting a year and more added onto the date difference for some reason. I've checked all over and I don't know what's wrong. Knowing me it's probably something simple.

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
  * @author Kenneth Clark
  * @version 10-11-2012
  * @description This program will print out a message that a library can send to someone who has an overdue book.
  */

public class Fines {
	
	public static void main(String[] args) {
		
		//Input and intro message
		Scanner in = new Scanner(System.in);
		msg("Enter the client's name (Last, First) and Social Security Number (###-##-####): ");
		
		//Get the clients name and SSN
		String clientInfo = in.nextLine();
		int apIndex = clientInfo.indexOf(",");
		int spIndex = clientInfo.lastIndexOf(" ");
		String clientFName = clientInfo.substring(apIndex + 2, spIndex);
		String clientLName = clientInfo.substring(0, apIndex);
		String clientSSN = clientInfo.substring(spIndex + 1);
		
		msg("\nEnter the title of the book: ");
		
		//Get book title
		String bookTitle = in.nextLine();
		
		msg("\nEnter the date the book was checked out (yyyy/mm/dd): ");
		
		//Date book was checked out
		DateFormat df = new SimpleDateFormat("yyyy/MM/DD");
		String dateCheckedOut = in.nextLine();
		Date originalDate = null;
		
		//Parse the date into a date object
		try {
			originalDate = df.parse(dateCheckedOut);
		} catch (ParseException e) {
			msg("Could not parse date: " + e);
			System.exit(-1);
		}
		
		//Get the days the book is late
		Date currentDate = new Date();
		long difference = currentDate.getTime() - originalDate.getTime(); //Difference of time between the day the book was checked out and today
		int daysLate = (int)((((difference / 1000l) / 60l) / 60l) / 24l); //Convert the difference from milliseconds to days
		double dailyFine = 0.12;
		double fine = dailyFine * (double)daysLate;
		
		//Print the text to be sent by e-mail
		msg("\n\nTo: " + clientFName + " " + clientLName + "\tAccount: " + clientLName + clientFName + "-" + clientSSN.substring(7));
		msg("\nFrom: Kenneth");
		msg("\nSubject: Overdue Notice");
		msg("\n==========================================================================================================");
		msg("\n" + bookTitle + " was checked out on: " + dateCheckedOut);
		msg("\nThe book is currently " + daysLate + " days late.");
		DecimalFormat f = new DecimalFormat("##.00");
		msg("\nYour fine has accumulated to: " + f.format(fine));
		
		
	} //End main
	
	//A method to shorten the print method since I do a lot of printing
	private static void msg(String msg) {
		System.out.print(msg);
	}

} //End Fine Class



Is This A Good Question/Topic? 0
  • +

Replies To: The difference of two dates is not correct

#2 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

Re: The difference of two dates is not correct

Posted 11 October 2012 - 01:00 PM

For some reason it's not parsing the month and day of the entered date.
Was This Post Helpful? 0
  • +
  • -

#3 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,599
  • Joined: 25-December 08

Re: The difference of two dates is not correct

Posted 11 October 2012 - 01:14 PM

My only guess is that your simple date format isn't being set properly. It seems after you try and parse it through the Dateformater it is always set to Sun Jan 01 00:00:00 EST 2012.

So your going to need to change your format expression to:
DateFormat df = new SimpleDateFormat("yyyy/MM/dd");

(make the D's lowercase)
Was This Post Helpful? 1
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1429
  • View blog
  • Posts: 3,173
  • Joined: 05-April 11

Re: The difference of two dates is not correct

Posted 11 October 2012 - 01:18 PM

D is day in year, d is day in month
Was This Post Helpful? 2
  • +
  • -

#5 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

Re: The difference of two dates is not correct

Posted 11 October 2012 - 02:26 PM

Thanks. I looked up the meanings of the letters and I saw that D was day in the year and I just thought that it would work. Well, thanks anyway. The program in running perfectly now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1