import java.time.LocalDate;
import java.time.Month;
public class Birthday{
public static void main(String args[]) {
// declare variables for birthday
int birthDate = 23;
Month birthMonth = Month.SEPTEMBER;
// get current date
LocalDate currentDate = LocalDate.now();
System.out.println("Todays Date: " + currentDate);
// get current date and month
int date = currentDate.getDayOfMonth();
Month month = currentDate.getMonth();
if(date == birthDate && month == birthMonth) {
System.out.println("HAPPY BIRTHDAY TO YOU !!");
}
else {
System.out.println("Today is not my birthday.");
}
}
}
Your code runs successfully. See the code run live at IdeOne.com.
Since you gave few details, I will guess that your problem is due to using a very old version of Java.
The java.time classes are built into Java 8 and later, and Android 26+.
I will go on to mention a couple of other issues in your code.
Enum#equals
You should replace the use of ==
with object references such as Month
objects. While that technically works with enum objects, using ==
on objects is a bad habit, with no benefit gained over calling Enum#equals
.
if( date == birthDate && month.equals( birthMonth ) ) {
MonthDay
Make a briefer version using MonthDay
.
MonthDay currentMonthDay = MonthDay.now() ;
MonthDay birthMonthDay = MonthDay.of( Month.SEPTEMBER , 23 ) ;
if( currentMonthDay.equals( birthMonthDay ) ) {…}
Or a one-liner:
if( MonthDay.now().equals( MonthDay.of( Month.SEPTEMBER , 23 ) ) ) { … }