compare Date Time in two string by format - Android java.util

Android examples for java.util:Date Compare

Description

compare Date Time in two string by format

Demo Code

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Main{

  public static final String YEAR = "yyyy";
  public static final String MONTH = "MM";
  public static final String DAY = "dd";
  public static final String FULL_TIME = "HH:mm:ss";
  public static final String YEAR_MONTH = YEAR + "-" + MONTH;
  public static final String FULL_DATE = YEAR_MONTH + "-" + DAY;
  public static final String FULL_DATE_TIME = FULL_DATE + " " + FULL_TIME;
  public static final String FULL_TIME_ZONE = FULL_DATE + "'T'"
          + FULL_TIME + "'Z'";

  /**/*from www  .  j  a  v  a 2s .  com*/
   * <br>
   * Convert date format
   * @param format
   * @param date
   * @return String
   */
  private static String format(String format, Date date) {
      SimpleDateFormat f = new SimpleDateFormat(format);
      return f.format(date);
  }
  public static String compareLastDateTime(String $format, String $date1,
      String $date2) {
  Date dt1, dt2;
  String result = null;
  try {
      dt1 = new SimpleDateFormat($format).parse($date1);
      dt2 = new SimpleDateFormat($format).parse($date2);

      if (dt1.compareTo(dt2) > 0)
          result = format($format, dt1);
      else
          result = format($format, dt2);
  } catch (Exception e) {
      e.printStackTrace();
  }
  return result;
}
}

Related Tutorials