Java Today isToday(Date d1)

Here you can find the source of isToday(Date d1)

Description

test a date represents todays date

License

Apache License

Parameter

Parameter Description
d1 non-null date

Return

as above

Declaration

public static boolean isToday(Date d1) 

Method Source Code

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

import java.util.*;

public class Main {
    /**//  ww  w .j av a  2  s  .com
     * test a date represents todays date
     * @param d1  non-null date
     * @return as above
     */
    public static boolean isToday(Date d1) {
        return sameDay(d1, new Date());
    }

    /**
     * test two dates have the same day
     * @param d1  non-null date
     * @param d2   non-null date
     * @return as above
     */
    @SuppressWarnings(value = "deprecated")
    public static boolean sameDay(Date d1, Date d2) {
        if (d1.getYear() != d2.getYear())
            return false;
        if (d1.getMonth() != d2.getMonth())
            return false;
        if (d1.getDay() != d2.getDay())
            return false;
        return true;
    }

    public static int getYear(Date date) {
        GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.getInstance();
        long time = date.getTime();
        calendar.setTimeInMillis(time);
        return calendar.get(calendar.YEAR);
    }

    /**
     * @param date Date
     * @return int month number 0-based
     */
    public static int getMonth(Date date) {
        GregorianCalendar calendar = (GregorianCalendar) GregorianCalendar.getInstance();
        long time = date.getTime();
        calendar.setTimeInMillis(time);
        return calendar.get(calendar.MONTH);
    }
}

Related

  1. getYestoday()
  2. getYestoday(String sourceDate, String format)
  3. isBeforeToday(Date date)
  4. isBeforeToday(Date date)
  5. isGreaterThanToday(String dateString)
  6. isToday(Date date)
  7. isToday(Date date)
  8. isToday(Date date, TimeZone timezone)
  9. isToday(Date dateTime)