Calculating days between two dates in Java


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(); }


Your format is wrong.

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

According to the <a href="http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html" rel="nofollow">documentation</a>, the MM specifies the months, where mm specifies minutes.


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

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);


  • Problems to select the correct row from a second table
  • AT+CUSD command standard response
  • how to play sound in specific times?
  • Pandas Key Error: 0 while plotting a seaborn boxplot
  • how to calculate month difference in R
  • jQuery - how to validate a date of birth using jQuery Validation plugin?
  • Android App with ASP.NET WebAPi Server - send complex types
  • Filtering SPARQL results by day and month
  • d3js: time scaling and “1901”
  • A simple datepicker in VueJS
  • How to make Plotly chart with year mapped to line color and months on x-axis
  • How to parsing NSDate to RFC 822 always use in English?
  • iOS Date formatting
  • CakePHP 3: change order in dateWidget
  • How to customize whisker lines on a geom_box plot differently than the lines of the box itself
  • Android application not restoring state when installed from .apk, works fine from eclipse
  • How to get month name with year and list of years between two Date
  • Replace last two characters in column
  • cell spacing in div table
  • Get localized short date pattern as String?
  • Python pandas melting data to multiple columns and coulmn names in another column
  • XSLT foreach repeating nodes to flat
  • How to create a 2D image by rotating 1D vector of numbers around its center element?
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • Invalid Date on validation Date of js
  • Web.config system.webserver errors
  • MySQL Order by column = x, column asc?
  • Parse a date string in a specific locale (not timezone!)
  • Optimizing database types to compact database (SQLite)
  • TFS: Get latest causes slow project reloading
  • Modifying destination and filename of gulp-svg-sprite
  • Running a C# exe file
  • Deserializing XML into class C#
  • Updated Ionic CLI but shows previous version (Windows)
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • 0x202A in filename: Why?
  • File not found error Google Drive API
  • python draw pie shapes with colour filled
  • Reading document lines to the user (python)
  • How to Embed XSL into XML