将字符串日期与今天的日期进行比较.只比较月和日

Abb*_*hid 1 java datetime date

我有这样的格式的日期字符串:19930508.我想从这个字符串中仅使用0508这是MMdd,而不是将它与今天的MMdd进行比较.我使用的代码是:

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}
Run Code Online (Sandbox Code Playgroud)

问题是,生日形式的甲酸盐无法正常运作今天的日期.日期日期var打印没有和birthDate字符串打印0220这对我没有意义.任何人如果能够使用这种格式化并将其与今天的日期进行比较?

Vik*_*ren 5

使用Java 8中的java.time:

    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");
    }
Run Code Online (Sandbox Code Playgroud)