Write a java program to compare two dates(Date Format)

Write a program to read two String variables in DD-MM-YYYY.Compare the two dates and return the older date in ‘MM/DD/YYYY’ format.

Input and Output Format:
Input consists of two string.
The output consists of a string.
Refer sample output for formatting specifications.

Sample Input 1:

8-11-2010
05-12-1987
Sample Output 1:
12/05/1987

Compare two dates(Date Format) in Java.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
 
public class Main {
 
                /**
                 * @param args
                 * @throws ParseException
                 */
                public static void main(String[] args) throws ParseException {
                                // TODO Auto-generated method stub
                	Scanner sc = new Scanner(System.in);
                                String s1=sc.nextLine();
                                String s2=sc.nextLine();
                                SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy");
                                SimpleDateFormat sdf1=new SimpleDateFormat("MM/dd/yyyy");
                                Date d1=sdf.parse(s1);
                                Date d2=sdf.parse(s2);
                                Calendar cal=Calendar.getInstance();
                                cal.setTime(d1);
                                long y=cal.getTimeInMillis();
                                cal.setTime(d2);
                                long y1=cal.getTimeInMillis();
                                String s3=sdf1.format(d1);
                                String s4=sdf1.format(d2);
                                if(y<y1)
                                                System.out.println(s3);
                                else
                                                System.out.println(s4);
                               
                }
}

You May Also Like These Posts:

Leave a Reply

Your email address will not be published.

ten + 11 =