Advertisement
coasterka

#3DaysBetweenTwoDates

May 15th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. import java.util.Date;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Scanner;
  5. import java.util.concurrent.TimeUnit;
  6.  
  7. public class DaysBetweenTwoDates {
  8.  
  9.     public static void main(String[] args) throws ParseException {
  10.        
  11.         try {
  12.             Scanner scan = new Scanner(System.in);
  13.             String firstDateStr = scan.nextLine();
  14.             String secondDateStr = scan.nextLine();
  15.             SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
  16.             Date firstDate = dateFormat.parse(firstDateStr);
  17.             Date secondDate = dateFormat.parse(secondDateStr);
  18.             long diffInMilli = secondDate.getTime() - firstDate.getTime();
  19.             long diffInDays = TimeUnit.MILLISECONDS.toDays(diffInMilli);
  20.             System.out.println(Math.abs(diffInDays));
  21.         }
  22.         catch(ParseException e){
  23.             e.printStackTrace();
  24.         }      
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement