compare Date in yyyy-MM-dd HH:mm:ss format - Android java.util

Android examples for java.util:Date Compare

Description

compare Date in yyyy-MM-dd HH:mm:ss format

Demo Code

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

public class Main {

  /**//from   w  w  w  . j  a v a 2 s  .  co m
   * "yyyy-MM-dd HH:mm:ss"
   * 
   * @param date1String
   * @param date2String
   * @return date1String == date2String return 0; date1String < date2String return
   *         <0; date1String > date2String return >0;
   */
  public static boolean compareDate(String date1String, String date2String) {

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date1 = null;
    Date date2 = null;
    try {
      date1 = dateFormat.parse(date1String);
      date2 = dateFormat.parse(date2String);
    } catch (ParseException e) {
      e.printStackTrace();

    }
    boolean result = date1.before(date2);
    return result;
  }

}

Related Tutorials