Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

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

import android.text.TextUtils;

public class Main {

    public static int compareDateStr(String dateStr1, String dateStr2, String format) {
        int result = 0;
        if (TextUtils.isEmpty(dateStr1) && !TextUtils.isEmpty(dateStr2)) {
            return 1;
        }
        if (!TextUtils.isEmpty(dateStr1) && TextUtils.isEmpty(dateStr2)) {
            return -1;
        }
        if ((TextUtils.isEmpty(dateStr1) && TextUtils.isEmpty(dateStr2)) || TextUtils.isEmpty(format)) {
            return 0;
        }
        Date d1 = getDate(dateStr1, format);
        Date d2 = getDate(dateStr2, format);
        if (d1 == null || d2 == null) {
            return result;
        }
        result = d2.compareTo(d1);
        return result;
    }

    public static Date getDate(String dateStr, String format) {
        if (TextUtils.isEmpty(dateStr) || TextUtils.isEmpty(format)) {
            return null;
        }
        Date date = null;
        try {
            SimpleDateFormat df = new SimpleDateFormat(format);
            date = df.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}