Advertisement
Guest User

Problem 7. Days between Two Dates

a guest
May 21st, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1. import java.sql.Date;
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class _07_DaysBetweenTwoDates {
  6.     public static void main (String[] args) {
  7.         Scanner sc = new Scanner (System.in);
  8.         System.out.print("Enter first date: ");
  9.         String input = sc.nextLine();
  10.         int day = Integer.parseInt(input.substring(0, input.indexOf("-")));
  11.         int month = Integer.parseInt(input.substring(input.indexOf("-") + 1, input.lastIndexOf("-")));
  12.         int year = Integer.parseInt(input.substring(input.lastIndexOf("-") + 1));
  13.         Date firstDate = new Date(year, month, day);
  14.  
  15.         System.out.print("Enter second date: ");
  16.         input = sc.nextLine();
  17.         day = Integer.parseInt(input.substring(0, input.indexOf("-")));
  18.         month = Integer.parseInt(input.substring(input.indexOf("-") + 1, input.lastIndexOf("-")));
  19.         year = Integer.parseInt(input.substring(input.lastIndexOf("-") + 1));
  20.         Date secondDate = new Date(year, month, day);
  21.        
  22.         int days = (int) ((secondDate.getTime() - firstDate.getTime()) / (1000 * 60 * 60 * 24) );
  23.         System.out.println(days);
  24.        
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement