0

I am new to Java and trying some practice exercises to get a better understanding of it.

I set myself the exercise to input different Strings, integers etc through the console and write the input to a textfile. All of this works fine.

Now I want to input into the textfile when the project started and when it will end. That works without any problem.

Now here is my problem. I want to calculate the time the project lasts from the beginning to the end in days.

Currently it only calculates the days, ignoring the month and the year.

For example: (dd.mm.yyyy)
Start: 07.07.2014   End: 15.07.2014  Output: 8 (almost correct :) )
Another example:
Start 07.07.2014   End: 03.08.2014  Output: -3 (not even close)

I just cannot figure out where my problem is.

Thanks for your help! Chris

System.out.print("Start project (dd.mm.yyyy): ");
String beginn = input.nextLine();
DateFormat df = new SimpleDateFormat("dd.mm.yyyy");
Date d = null;
try {
    d = df.parse(beginn);
} catch (ParseException e) {
    System.out.println("Unable to parse " + beginn);
}
DateFormat df3 = DateFormat.getDateInstance(DateFormat.LONG);
String s3 = df3.format(d);

System.out.print("End of project (dd.mm.yyyy): ");
String ende = input.nextLine();
DateFormat df4 = new SimpleDateFormat("dd.mm.yyyy");
Date g = null;
try {
    d = df.parse(ende);
} catch (ParseException f) {
    System.out.println("Unable to parse " + ende);
}
//DateFormat df5 = DateFormat.getDateInstance(DateFormat.LONG);
//String s4 = df5.format(d);


// String dauer = input.nextLine(); 
diffDays = 0;
try {
    DateFormat dfa = new SimpleDateFormat("dd.mm.yyyy");
    Date from = dfa.parse(beginn);
    Date to = dfa.parse(ende);
    long diffMillis = to.getTime() - from.getTime();
    //diffDays = Math.round( (double)diffMillis / (24. * 60.*60.*1000.) );
    diffDays = diffMillis / (1000 * 60 * 60 * 24);
    System.out.println(diffDays);
} catch (ParseException ex) {
    ex.printStackTrace();
}
Niels Bech Nielsen
  • 4,777
  • 1
  • 21
  • 44
user3013909
  • 89
  • 3
  • 9
  • Here is a nice library to perform dates operations : [Joda-Time](http://www.joda.org/joda-time/). And here is an example : http://stackoverflow.com/a/15541322/2806497 – OlivierH Jul 08 '14 at 10:34
  • possible duplicate of [Calculating the Difference Between Two Java Date Instances](http://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances) – Jens Jul 08 '14 at 10:37
  • Dividing by `1000 * 60 * 60 * 24` will give you the wrong answer for date ranges that span the beginning of daylight savings time. Do NOT assume that every day has 24 hours. – Dawood ibn Kareem Jul 08 '14 at 10:40
  • possible duplicate of [Calculate elapsed time in Java / Groovy](http://stackoverflow.com/questions/567659/calculate-elapsed-time-in-java-groovy) and [this](http://stackoverflow.com/q/2179644/642706) and [this](http://stackoverflow.com/q/11605858/642706) and [this](http://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances) and [this](http://stackoverflow.com/questions/20808561/define-own-date-intervals-with-startdate-and-enddate-in-a-new-variable/20809335#20809335). – Basil Bourque Jul 08 '14 at 22:27

2 Answers2

0

Your format is wrong.

DateFormat df = new SimpleDateFormat("dd.MM.yyyy");

According to the documentation, the MM specifies the months, where mm specifies minutes.

Niels Bech Nielsen
  • 4,777
  • 1
  • 21
  • 44
0

Example in Java 8 :
Get the number of days between now and java.sql.Date :

LocalDate startDate = LocalDate.now();
Instant instant = dueDate.toInstant();
LocalDate endDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();
long dueDays = ChronoUnit.DAYS.between(startDate, endDate);
System.out.println("XXX is due in " + dueDays);
Hugues
  • 375
  • 1
  • 3
  • 10