-3

I use javaSE 1.6 and I need to compare two dates and check if they are the same. this check needs to be independent of the year.

for example, i need to identify if today is the B-day of someone, and I need this code to be reused in the coming years. so I need to perform the check, not on the year, not even on the day-of-the-year (issue with Leap year) but I can only rely on the day-of-the-month and the month number itself.

my input data, the birthday day of the person, it is in the Java class "Date"

is there any method of javaSE 1.6 that could help me?

I checked classes "Date" and "Calendar", but so far I couldn't find any hint to solve my issue.

Ale
  • 31
  • 4
  • 3
    There was nothing on `Calendar` to help you determine the month and the day of the month? – chrylis -cautiouslyoptimistic- Apr 16 '18 at 16:15
  • https://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html#get(int), https://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html#DATE, https://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html#MONTH – JB Nizet Apr 16 '18 at 16:20
  • thanks for the hint, I didn't find it before, I was looking for a dedicated method. thanks for your help. – Ale Apr 16 '18 at 17:14
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 17 '18 at 18:46

2 Answers2

1

ThreeTen Backport

public static MonthDay toMonthDay(Date utilDate) {
    Instant inst = DateTimeUtils.toInstant(utilDate);
    ZonedDateTime dateTime = inst.atZone(ZoneId.systemDefault());
    return MonthDay.from(dateTime);
}

The MonthDay class of java.time is what you need, it’s a date without year, or conversely, a month and a day-of-month. Birthdays and other days to remember are the prime example for its use.

Like this:

    Date birthday = // ...
    Date today = new Date();
    if (toMonthDay(today).equals(toMonthDay(birthday))) {
        System.out.println("It’s her birthday");
    } else {
        System.out.println("It’s not her birthday");
    }

On Java 6 you need to use the ThreeTen-Backport, the backport of java.time to Java 6 and 7.

I am hesitatingly using ZoneId.systemDefault() to use the JVM’s time zone setting for the conversion. On one hand the Date would normally assume this time zone; on the other hand this is fragile because the setting can be changed at any time from other parts of your program or other programs running in the same JVM. If you know better, by all means give a time zone like for example ZoneId.of("America/Adak")

If using Java 8 or later and the built-in java.time, the conversion is a little bit simpler:

    ZonedDateTime dateTime = utilDate.toInstant().atZone(ZoneId.systemDefault());
    return MonthDay.from(dateTime);

Links

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

I found the way to call the Month and date-of-month in class Calendar.

here is my solution, I didn't find any other example on this topic, so I'll paste, maybe could help someone in the future.

if you have a more stylish solution, please let me know. I'll be glad to learn :)

    // date of the B-day 31st December
    int month = Calendar.DECEMBER;
    int date = 31;

    boolean status = false; 

    // Class Date already filled with the date we wanna check.
    Date beginDate;

    Calendar c = Calendar.getInstance();
    c.setTime(beginDate); 
    int dayOfMonthToCompare = c.get(Calendar.DAY_OF_MONTH);
    int monthToCompare = c.get(Calendar.MONTH);

    //perform check on day and Month
    if(dayOfMonthToCompare == date && monthToCompare == month){
        status = true;
    }
Ale
  • 31
  • 4
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 17 '18 at 18:46