Compare two date value in string - Android java.util

Android examples for java.util:Date Compare

Description

Compare two date value in string

Demo Code

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

public class Main{
  public static final SimpleDateFormat dateFormat = new SimpleDateFormat(
      "yyyy-MM-dd HH:mm");
    public static int equals2Date(String date) {
        return equals2Date(date, null);
    }/*from w  w  w .  j a v  a2s  . c  o m*/
    public static int equals2Date(String date1, String date2) {
        long time1 = 0;
        long time2 = 0;
        if (date2 == null) {
            time2 = System.currentTimeMillis();
        } else {
            try {
                Date parse2 = dateFormat.parse(date1);
                time2 = parse2.getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        try {
            Date parse1 = dateFormat.parse(date1);
            time1 = parse1.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (time1 > time2) {
            return 1;
        } else if (time1 < time2) {
            return -1;
        } else {
            return 0;
        }
    }

}

Related Tutorials