Java SQL Date Compare compareDate(java.util.Date s1, java.util.Date s2)

Here you can find the source of compareDate(java.util.Date s1, java.util.Date s2)

Description

compare Date

License

Open Source License

Declaration

public static long compareDate(java.util.Date s1, java.util.Date s2) 

Method Source Code


//package com.java2s;
import java.sql.Date;
import java.sql.Time;

import java.text.*;

public class Main {
    public static final String DEFAULT_DATE_YMD_FORMAT = "yyyy-MM-dd";
    public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

    public static long compareDate(String pattern, String s1, String s2) {

        SimpleDateFormat f = new SimpleDateFormat(pattern);

        try {//  w  w w .ja  va2  s  .  c  om
            return f.parse(s1).getTime() - f.parse(s2).getTime();
        } catch (Exception e) {
            return -1;
        }

    }

    public static long compareDate(java.util.Date s1, java.util.Date s2) {
        try {
            return compareDate(DEFAULT_DATE_YMD_FORMAT, toString(s1), toString(s2));
        } catch (Exception e) {
            return -1;
        }

    }

    public static String toString(Date date) {
        return toString((java.util.Date) date);
    }

    public static String toString(java.util.Date date) {
        return toString(date, DEFAULT_DATE_YMD_FORMAT);
    }

    public static String toString(Date date, String format) {
        return toString((java.util.Date) date, format);
    }

    public static String toString(java.util.Date date, String format) {

        if (date == null) {
            return null;
        }

        if (format == null) {
            throw new IllegalArgumentException("The value of an argument is inaccurate.");
        }

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        return sdf.format(date);
    }

    public static String toString(Time time, String format) {

        if (time == null) {
            return null;
        }

        if (format == null) {
            throw new IllegalArgumentException("The value of an argument is inaccurate.");
        }

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        return sdf.format(time);
    }

    public static String toString(Time time) {
        return toString(time, DEFAULT_TIME_FORMAT);
    }
}

Related

  1. compareDate(java.sql.Date date1, java.sql.Date date2)
  2. compareDates(Date date1, Date date2)
  3. compareSqlDate(Object left, Object right)