Android Date Compare compare(String date0, String date1, String format)

Here you can find the source of compare(String date0, String date1, String format)

Description

compare

License

Open Source License

Declaration

public static int compare(String date0, String date1, String format) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

import java.util.Date;

import java.util.HashMap;
import java.util.Map;
import android.text.TextUtils;

public class Main {
    private static final Map<String, DateFormat> DFS = new HashMap<String, DateFormat>();

    public static int compare(String date0, String date1, String format) {
        SimpleDateFormat sd = new SimpleDateFormat(format);
        try {/*from w  w w. java2 s  .com*/
            long diff = sd.parse(date0).getTime()
                    - sd.parse(date1).getTime();
            if (diff > 0) {
                return 1;
            } else if (diff < 0) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        boolean b1 = false, b2 = false;
        if (TextUtils.isEmpty(date0)) {
            b1 = true;
        }
        if (TextUtils.isEmpty(date1)) {
            b2 = true;
        }
        if (b1 && b2) {
            return 0;
        } else if (b1) {
            return -1;
        } else {
            return 1;
        }
    }

    /**
     * @param source
     * @param pattern
     * @return Date
     */
    public static Date parse(String source, String pattern) {
        if (source == null) {
            return null;
        }
        Date date;
        try {
            date = getFormat(pattern).parse(source);
        } catch (ParseException e) {
            return null;
        }
        return date;
    }

    public static DateFormat getFormat(String pattern) {
        DateFormat format = DFS.get(pattern);
        if (format == null) {
            format = new SimpleDateFormat(pattern);
            DFS.put(pattern, format);
        }
        return format;
    }
}

Related

  1. isSameDay(Date dateSource, Date dateDesti)
  2. isSameInstant(Date date1, Date date2)
  3. needsMidnightCountdown()
  4. dateDiffer(Date date1, Date date2)
  5. compare(Date date1, Date date2)
  6. isDateTimeAfter(String startDate, String startTime, String endDate, String endTime)
  7. isDateNew(String YearMonthDayHourMinuteSeconds)
  8. isSameDay(Date d1, Date d2)
  9. isSameDay(Date date1, Date date2)