1

I have string of date with format like this: 19930508. I want to use only 0508 from this string which is MMdd and than compare it with today's MMdd. The code i am using is:

Date todaysDate = new Date();
String dateTest = "19930508";
SimpleDateFormat df = new SimpleDateFormat("MMdd");
Date date = df.parse(dateTest);
String birthDate = df.format(date);

if(birthDate.equals(df.format(todaysDate))){do something}

The problem is the that birthdate formate is not working correctly neither todaysDate. Date date var print nothing and birthDate string print 0220 which makes no sense to me. Anyone with anyidea how can i work with this kind of formating and compare it with todaysdate ?

Abbas Zahid
  • 177
  • 1
  • 2
  • 15
  • Yeah you should be using Calendar object,this is really the best library for date handling. – juju May 08 '17 at 14:14
  • @juju no it definitily is not the best API for date handling, but until Java 8 it is the only integrated API – Jens May 08 '17 at 14:19
  • Calendar is long winded, especially if you have to include Timestamps, but it works.......just, provided you're only programming in one time zone. – juju May 08 '17 at 14:21
  • `Date` and `Calendar` are definitely **not** the best lib for date handling: see http://stackoverflow.com/questions/1969442/whats-wrong-with-java-date-time-api, http://www.jroller.com/cpurdy/entry/the_seven_habits_of_highly and https://blog.codecentric.de/en/2014/01/time/ –  May 08 '17 at 18:48

2 Answers2

5

Using java.time from Java 8:

    MonthDay now = MonthDay.now();

    String dateTest = "19930508";
    DateTimeFormatter yearMonthDayFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
    MonthDay birthDay = MonthDay.parse(dateTest, yearMonthDayFormatter);

    if (birthDay.equals(now)) {
        System.out.println("same MMdd");
    } else {
        System.out.println("different MMdd");
    }
Viktor Mellgren
  • 4,318
  • 3
  • 42
  • 75
  • 1
    Updated using MonthDay rather than LocalDate – Viktor Mellgren May 08 '17 at 14:29
  • This java.time is not available in java 7 ? this solutions seems workable to me. – Abbas Zahid May 08 '17 at 14:33
  • If you're stuck with Java 7, you might want to use Joda Time (a library very similar to java.time in Java 8). – C-Otto May 08 '17 at 14:34
  • No, not available, so either upgrade to Java 8 or use joda-time as suggested. (Syntax is probalby almost identical). – Viktor Mellgren May 08 '17 at 14:35
  • 1
    @AbbasZahid Much of the java.time functionality is back-ported to Java 6 and Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Built by the same folks who built java.time. – Basil Bourque May 08 '17 at 15:25
  • From what I hear and read, you should prefer ThreeTen Backport over JodaTime (haven’t got experience with either myself, but I’m quite a fan of `java.time`). – Ole V.V. May 08 '17 at 15:56
  • @ViktorMellgren i did not able to find MonthDay with joda time, can you tell me the alternate of Monthday in joda time ? – Abbas Zahid May 08 '17 at 16:49
  • JodaTime's `MonthDay`: http://www.joda.org/joda-time/apidocs/org/joda/time/MonthDay.html –  May 08 '17 at 17:35
  • @Hugo the lib of JodaTime i have in my project doesn't support this MonthDay functionality. I believe the lib is quite old – Abbas Zahid May 08 '17 at 18:16
  • Indeed, `MonthDay` exists only in jodatime >= 2.0 –  May 08 '17 at 18:21
1
// works with java6
public class AnniversaryChecker {
    private final String dayInYear;

    public AnniversaryChecker() {
        this(new SimpleDateFormat("MMdd").format(new Date()));
    }

    // VisibleForTesting
    public AnniversaryChecker(String mmdd) {
        this.dayInYear = mmdd;
    }

    public boolean isAnniversary(String yyyyMMdd) {
        return yyyyMMdd.endsWith(dayInYear);
    }
}
Frank Neblung
  • 3,047
  • 17
  • 34
  • FYI The troublesome old date-time classes such as SimpleDateFormat and Date are now legacy, supplanted by the java.time classes. – Basil Bourque May 08 '17 at 15:29
  • 1
    It’s probably the best you can do if you neither want to move to Java 8 nor want to depend on an external library (like ThreeTen Backport). It’s funnily backward, isn’t it? Not comparing the dates, but their string representations. Also you get no validation of the yyyyMMdd string; if by accident you got a completely irrelevant string, you wouldn’t notice, you’d just be told it isn’t your anniversary (of course validation can be added, but it will be extra). – Ole V.V. May 08 '17 at 16:01
  • 1
    I really don't want to use external library and java 8 as per project restrictions. i am trying to understand this example provided by @Frank and trying to see how can i validate it with todays date – Abbas Zahid May 08 '17 at 16:07
  • @AbbasZahid If you have all those restrictions (can't use external libs and java < 8), you could update the question with it. This would prevent people from writing "incorrect" answers –  May 08 '17 at 18:24