Java Day Compare isSameDay(Date date, Date date2)

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

Description

is Same Day

License

Open Source License

Declaration

public static boolean isSameDay(Date date, Date date2) 

Method Source Code


//package com.java2s;
import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Main {
    public static final String YMD = "yyyy-MM-dd";
    private static final Map<String, ThreadLocal<DateFormat>> tlDateFormatMap = new ConcurrentHashMap<String, ThreadLocal<DateFormat>>();

    public static boolean isSameDay(Date date, Date date2) {
        if (date == null || date2 == null) {
            return false;
        }/* www . ja v  a  2  s  .  c o  m*/
        DateFormat df = getDateFormat(YMD);
        return df.format(date).equals(df.format(date2));
    }

    public static DateFormat getDateFormat(String fmt) {
        ThreadLocal<DateFormat> tl = tlDateFormatMap.get(fmt);
        if (tl == null) {
            tl = threadLocalDateFormat(fmt);
            tlDateFormatMap.put(fmt, tl);
        }
        return tl.get();
    }

    public static String format(Date date, String fmt) {
        return getDateFormat(fmt).format(date);
    }

    private static ThreadLocal<DateFormat> threadLocalDateFormat(final String pattern) {
        ThreadLocal<DateFormat> tl = new ThreadLocal<DateFormat>() {
            protected DateFormat initialValue() {
                return new SimpleDateFormat(pattern);
            }
        };
        return tl;
    }
}

Related

  1. compareDateByDay(Date date1, Date date2)
  2. compareDay(Date first, Date second)
  3. compareInSameDay(Date date1, Date date2)
  4. isSameDay(Date a, Date b)
  5. isSameDay(Date c1, Date c2)
  6. isSameDay(Date date1, Date date2)
  7. isSameDay(Date date1, Date date2)
  8. isSameDay(Date date1, Date date2)
  9. isSameDay(Date date1, Date date2)