Here you can find the source of compare(String date0, String date1, String format)
public static int compare(String date0, String date1, String format)
//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; } }