English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In diesem Beispiel werden wir lernen, wie man den Geburtstag mit dem aktuellen Datum überprüft und eine 'Herzlichen Glückwunsch zum Geburtstag'-Nachricht in Java ausdrückt.
import java.time.LocalDate; import java.time.Month; public class Main { public static void main(String args[]) { //Erklärung der Geburtsdatumsvariable int birthDate = 23; Month birthMonth = Month.SEPTEMBER; //Erhalten Sie das aktuelle Datum LocalDate currentDate = LocalDate.now(); System.out.println("Today's date: ") + currentDate); //Get the 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."); } } }
Output1
Today's date: 2020-08-28 Happy Birthday to You!!
In the above example,
LocalDate.now() - Return the current date
getDayOfMonth() - Return the current date
getMonth() - Return the current month
Here, we useif ... elseThe following statement is used to check if the current date matches the birthday. If it is true, then printHappy BirthdayMessage.