Java Date Compare compareDate(String begingDate, String endDate, String format)

Here you can find the source of compareDate(String begingDate, String endDate, String format)

Description

compare Date

License

Open Source License

Declaration

public static boolean compareDate(String begingDate, String endDate, String format) 

Method Source Code


//package com.java2s;

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

import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {

    public static boolean compareDate(String begingDate, String endDate) {
        boolean boo = true;
        Date begin = null;/* w w w .j a  va2 s.c o m*/
        Date end = null;
        String format = "yyyy/MM/dd";
        // if(begingDate.indexOf("-") != -1) {
        // format = "yyyy-MM-dd";
        // }
        // if(endDate.indexOf("-") != -1) {
        // format = "yyyy-MM-dd";
        // }
        DateFormat df = new SimpleDateFormat(format);
        if (null == begingDate || "".equals(begingDate)) {
            boo = false;
            return boo;
        }
        if (null == endDate || "".equals(endDate)) {
            boo = false;
            return boo;
        }
        try {
            begin = df.parse(begingDate);
            end = df.parse(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (begin.compareTo(end) >= 0) {
            boo = false;
        }
        return boo;
    }

    public static boolean compareDate(String begingDate, String endDate, String format) {
        boolean boo = true;
        Date begin = null;
        Date end = null;
        if (format.isEmpty()) {
            format = format = "yyyy/MM/dd";
        }
        // if(begingDate.indexOf("-") != -1) {
        // format = "yyyy-MM-dd";
        // }
        // if(endDate.indexOf("-") != -1) {
        // format = "yyyy-MM-dd";
        // }
        DateFormat df = new SimpleDateFormat(format);
        if (null == begingDate || "".equals(begingDate)) {
            boo = false;
            return boo;
        }
        if (null == endDate || "".equals(endDate)) {
            boo = false;
            return boo;
        }
        try {
            begin = df.parse(begingDate);
            end = df.parse(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (begin.compareTo(end) >= 0) {
            boo = false;
        }
        return boo;
    }

    public static String parse(Date d) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
        String dateStr = sdf.format(d);
        return dateStr;
    }
}

Related

  1. compareDate(Date date1, Date date2)
  2. compareDate(Date date1, Date date2, String pattern)
  3. compareDate(Date first, Date second)
  4. compareDate(Date sourceDate, Date targetDate)
  5. compareDate(Date start, Date end)
  6. compareDate(String d1, String d2)
  7. compareDate(String d1, String d2)
  8. compareDate(String date)
  9. compareDate(String date0, String date1)