Java Day Compare isSameDay(Date date1, Date date2)

Here you can find the source of isSameDay(Date date1, Date date2)

Description

is Same Day

License

Open Source License

Declaration

public static boolean isSameDay(Date date1, Date date2) 

Method Source Code


//package com.java2s;

import java.text.DateFormat;

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

public class Main {

    public static boolean isSameDay(Date date1, Date date2) {
        int flag = compareDate(date1, date2, "yy-MM-dd");
        return flag == 0 ? true : false;
    }/*ww  w .ja v  a  2  s  .com*/

    public static boolean compareDate(String strDate1, String strDate2, String format) {
        Date date1 = convertStrToDate(strDate1, format);
        Date date2 = convertStrToDate(strDate2, format);
        if (date1.getTime() >= date2.getTime())
            return true;
        return false;
    }

    /**
     * 
     * 
     * @param date1
     * @param date2
     * @param format
     * @return
     */
    public static int compareDate(Date date1, Date date2, String format) {
        String dateStr1 = convertDateToStr(date1, format);
        String dateStr2 = convertDateToStr(date2, format);
        return dateStr1.compareTo(dateStr2);
    }

    public static int compareDate(Date date, Date startDate, Date endDate) {
        if (date == null || startDate == null || endDate == null)
            return -2;
        if (date.getTime() >= startDate.getTime() && date.getTime() <= endDate.getTime())
            return 0;
        else if (date.getTime() > endDate.getTime())
            return 1;
        else
            return -1;
    }

    public static Date convertStrToDate(String s) {
        try {
            DateFormat dateformat = DateFormat.getDateInstance();
            Date date = dateformat.parse(s);
            return date;
        } catch (Exception exception) {
            exception.printStackTrace();
            Calendar cal = Calendar.getInstance();
            cal.set(1900, 0, 1);
            return cal.getTime();
        }
    }

    public static Date convertStrToDate(String s, String format) {
        SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
        try {
            Date date = simpledateformat.parse(s);
            return date;
        } catch (Exception exception) {
            exception.printStackTrace();
            Calendar cal = Calendar.getInstance();
            cal.set(1900, 0, 1);
            return cal.getTime();
        }
    }

    public static String convertDateToStr(Date d, String format) {
        SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
        String s;
        try {
            s = simpledateformat.format(d).toString();
            return s;
        } catch (Exception e) {
            s = "1900-01-01";
        }
        return s;
    }
}

Related

  1. isSameDay(Date a, Date b)
  2. isSameDay(Date c1, Date c2)
  3. isSameDay(Date date, Date date2)
  4. isSameDay(Date date1, Date date2)
  5. isSameDay(Date date1, Date date2)
  6. isSameDay(Date date1, Date date2)
  7. isSameDay(Date date1, Date date2)
  8. isSameDay(Date date1, Date date2)
  9. isSameDay(Date day1, Date day2)