Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

public class Main {

    public static int compareDate(String strDate1, String strDate2) {
        int value = 0;
        Date d1 = stringToDate(strDate1, "yyyy-MM-dd HH:mm:ss");
        Date d2 = stringToDate(strDate2, "yyyy-MM-dd HH:mm:ss");
        if (d1 == null || d2 == null) {
            return value;
        }
        value = d2.compareTo(d1);
        return value;
    }

    public static Date stringToDate(String str) {
        Date date = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = formatter.parse(str);
            return date;
        } catch (Exception e) {
            return null;
        }
    }

    public static Date stringToDate(String str, String format) {
        Date date = null;
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        try {
            date = formatter.parse(str);
            return date;
        } catch (Exception e) {
            return null;
        }
    }

    public static Date stringToDate(String str, String format, Locale locale) {
        Date date = null;
        SimpleDateFormat formatter = new SimpleDateFormat(format, locale);
        try {
            date = formatter.parse(str);
            return date;
        } catch (Exception e) {
            return null;
        }
    }
}