Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculating days between two dates with Java
    text
    copied!<p>I want a Java program that calculates days between two dates.</p> <ol> <li>Type the first date (German notation; with whitespaces: "dd mm yyyy")</li> <li>Type the second date.</li> <li>The program should calculates the number of days between the two dates.</li> </ol> <p>How can I include leap years and summertime?</p> <p>My code:</p> <pre><code>import java.util.Calendar; import java.util.Date; import java.util.Scanner; public class NewDateDifference { public static void main(String[] args) { System.out.print("Insert first date: "); Scanner s = new Scanner(System.in); String[] eingabe1 = new String[3]; while (s.hasNext()) { int i = 0; insert1[i] = s.next(); if (!s.hasNext()) { s.close(); break; } i++; } System.out.print("Insert second date: "); Scanner t = new Scanner(System.in); String[] insert2 = new String[3]; while (t.hasNext()) { int i = 0; insert2[i] = t.next(); if (!t.hasNext()) { t.close(); break; } i++; } Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(insert1[0])); cal.set(Calendar.MONTH, Integer.parseInt(insert1[1])); cal.set(Calendar.YEAR, Integer.parseInt(insert1[2])); Date firstDate = cal.getTime(); cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(insert2[0])); cal.set(Calendar.MONTH, Integer.parseInt(insert2[1])); cal.set(Calendar.YEAR, Integer.parseInt(insert2[2])); Date secondDate = cal.getTime(); long diff = secondDate.getTime() - firstDate.getTime(); System.out.println ("Days: " + diff / 1000 / 60 / 60 / 24); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload