Java Day Compare isSameDay(Date c1, Date c2)

Here you can find the source of isSameDay(Date c1, Date c2)

Description

is Same Day

License

Apache License

Declaration

public static boolean isSameDay(Date c1, Date c2) 

Method Source Code


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

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

public class Main {
    private static final String patternDate = "yyyy-MM-dd";
    private static final String patternDateTime = "yyyy-MM-dd HH:mm:ss";

    public static boolean isSameDay(Date c1, Date c2) {
        return formatDate(c1).equals(formatDate(c2));
    }/*  w w w . ja  v  a 2s .  c om*/

    public static String formatDate(Date myDate) {
        return formatDateTime(myDate, patternDate);
    }

    public static String formatDateTime(Date myDate, String pattern) {
        myDate = isDate(myDate);
        // SimpleDateFormat fd = new SimpleDateFormat(pattern, Locale.CHINA);
        SimpleDateFormat fd = new SimpleDateFormat(pattern);
        return (fd.format(myDate));
    }

    public static String formatDateTime(String myDate, String pattern) {
        if (myDate.length() > 10)
            myDate = myDate.substring(0, 10);
        Date date = getDateByString(myDate, "yyyy-MM-dd");
        return formatDateTime(date, pattern, Locale.US);
    }

    public static String formatDateTime(Date myDate, String pattern, Locale localcode) {
        myDate = isDate(myDate);
        SimpleDateFormat fd = new SimpleDateFormat(pattern, localcode);
        return (fd.format(myDate));
    }

    public static String formatDateTime(Date myDate) {
        return formatDateTime(myDate, patternDateTime);
    }

    public static Date isDate(Date myDate) {
        // if (myDate == null)
        // return new Date();
        return myDate;
    }

    public static Date getDateByString(String rq) {
        // DateFormat df = DateFormat.getDateInstance();
        DateFormat df = new SimpleDateFormat(patternDate);
        Date d = null;
        try {
            d = df.parse(rq);
        } catch (Exception e) {
            // e.printStackTrace();
        }
        return d;
    }

    public static Date getDateByString(String str, String pattern) {
        SimpleDateFormat sdf = null;
        try {
            sdf = new SimpleDateFormat(pattern);
        } catch (Exception ex) {
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
        try {
            return sdf.parse(str);
        } catch (Exception e) {
        }
        return null;
    }
}

Related

  1. afterNDaysDate(String theDate, int nDayNum, String format)
  2. compareDateByDay(Date date1, Date date2)
  3. compareDay(Date first, Date second)
  4. compareInSameDay(Date date1, Date date2)
  5. isSameDay(Date a, Date b)
  6. isSameDay(Date date, Date date2)
  7. isSameDay(Date date1, Date date2)
  8. isSameDay(Date date1, Date date2)
  9. isSameDay(Date date1, Date date2)