Q:

Date convert in Java

I want to compare two dates, for that i convert the string to date format.But during the conversion the date format changed to "02/01/2013" and "03/01/2014".It makes error in my logic.any one please tell me to how to compare two days in my date format.   
      String fdate="01/02/2012";
      String tdate="01/03/2013";
      SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
      Date frmdt=new Date(fdate);
      String s1 = sdf.format(frmdt);
      Date todt=new Date(tdate);
      String s2 = sdf.format(todt);
      Date frmdate = sdf.parse(s1);
      Date todate = sdf.parse(s2);
        if(frmdate.compareTo(todate)<=0){
             //process;      
         }

A:

Try this: 
  String fs = "01/02/2012";
  String ts = "01/03/2013";
  DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
  sdf.setLenient(false);
  Date fdate = sdf.parse(fs);
  Date tdate = sdf.parse(ts);
  if (fdate.before(tdate) || f.date.equals(tdate)) {
         //process;      
  }

You've got too much going on.  It's much simpler.

A:

It seems to me that you should be calling SimpleDateFormat.parse instead:
// Using the US locale will force the use of the Gregorian calendar, and
// avoid any difficulties with different date separator symbols etc.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("UTC")); // Avoid DST complications
sdf.setLenient(false);

Date fromDate = sdf.parse(fromDateText);
Date toDate = sdf.parse(toDateText);

// Alternatively: if (!fromDate.after(toDate))
if (fromDate.compareTo(toDate) <= 0) {
    ...
}

I'd actually suggest that you use Joda Time if at all possible, where you could use a LocalDate type to more accurately represent your data.

