Compare two dates in java - Java @ Desk

Thursday, May 16, 2013

Compare two dates in java














import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateComparision {
    public static void main(String args[]) throws ParseException {
        Date date = new Date();
        String format = "dd-MM-yyyy";
        DateFormat dateFormat = new SimpleDateFormat(format);
        String input = "10-12-2010";
        Date newDate = dateFormat.parse(input);
        System.out.println("Input string in date format : "+newDate);
        System.out.println("Input date before today's date : "+newDate.before(date));
        System.out.println("Input date after today's date : "+newDate.after(date));
    }
}

Output :

Input string in date format : Fri Dec 10 00:00:00 IST 2010
Input date before today's date : true
Input date after today's date : false






No comments:

Post a Comment