Java Day Compare isSameDay(Date preDate, Date postDate)

Here you can find the source of isSameDay(Date preDate, Date postDate)

Description

is Same Day

License

Apache License

Declaration

public static boolean isSameDay(Date preDate, Date postDate) 

Method Source Code

//package com.java2s;
/*// w  ww . j av a  2 s  . c  o m
 * Commons-Utils
 * Copyright (c) 2017.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 */

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.TimeZone;

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

    public static boolean isSameDay(Date preDate, Date postDate) {
        return dateToString(preDate, YYYY_MM_DD).equals(dateToString(postDate, YYYY_MM_DD));
    }

    public static String dateToString(Date date, String pattern) {
        return dateToString(date, pattern, TimeZone.getDefault());
    }

    public static String dateToString(Date date) {
        return dateToString(date, YYYY_MM_DD_HH_MM_SS, TimeZone.getDefault());
    }

    public static String dateToString(Date date, String pattern, TimeZone timezone) {
        SimpleDateFormat dateFormat = null;
        if (null == pattern || "".equals(pattern)) {
            dateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        } else {
            dateFormat = new SimpleDateFormat(pattern);
        }
        dateFormat.setTimeZone(timezone);
        return dateFormat.format(date);
    }
}

Related

  1. isSameDay(Date date1, Date date2)
  2. isSameDay(Date date1, Date date2)
  3. isSameDay(Date date1, Date date2)
  4. isSameDay(Date day1, Date day2)
  5. isSameDay(Date day1, Date day2)
  6. isSameDay(final Date date1, final Date date2)
  7. isSameDay(final Date date1, final Date date2)
  8. sameDay(Date d1, Date d2)
  9. sameDay(Date date1, Date date2)