public boolean before(Date when)
returns true if this Date is before the Date argument.
public boolean after(Date when)
returns true if this Date is after the Date argument.
public boolean equals(Object obj)
returns true if the argument Date is equal to this Date.
The complete source code is given below:
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class CompareDates { public static void main(String[] args) { try { SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); Date date1 = sdf.parse("22-03-2012"); Date date2 = sdf.parse("12-11-2009"); System.out.println("Date1=" + sdf.format(date1)); System.out.println("Date2=" + sdf.format(date2)); if (date1.after(date2)) { System.out.println("Date1 is after Date2"); } if (date1.before(date2)) { System.out.println("Date1 is before Date2"); } if (date1.equals(date2)) { System.out.println("Date1 is equal Date2"); } } catch (ParseException ex) { ex.printStackTrace(); } } }
The output is
Date1=22-03-2012
Date2=12-11-2009
Date1 is after Date2
The links that may help you:
No comments:
Post a Comment