이 기사에서는 생일을 확인하고 생일 축하 메시지를 인쇄하는 방법을 이해합니다. 생일 확인은 현재 날짜와 주어진 생일을 비교하여 수행됩니다.
아래는 동일한 데모입니다 -
입력
입력이 -
라고 가정합니다.Birthday Date: 15 July
출력
원하는 출력은 -
Today’s Date is 20-12-2021 Today is not my birthday
알고리즘
Step 1 - START Step 2 - Declare variables for month and dates values namely month_of_birth and date_of_birth Step 3 - Read the required values from the user/ define the values Step 4 - Use LocalDate.now() function to get the current date and store it in the variable. Step 5 – Using an if loop, compare the current month and date value with the input date and month values respectively. If the values match, the result is true. Step 5- Display the result Step 6- Stop
예시 1
import java.time.LocalDate;
import java.time.Month;
public class HappyBirthday {
public static void main(String args[]) {
int date_of_birth = 15;
Month month_of_birth = Month.JULY;
System.out.println("The required packages have been imported");
LocalDate current_date = LocalDate.now();
System.out.println("Today's Date is " + current_date);
System.out.println("The birthday is defined as : " +date_of_birth + " " +month_of_birth);
int date = current_date.getDayOfMonth();
Month month = current_date.getMonth();
if(date == date_of_birth && month == month_of_birth) {
System.out.println("HAPPY BIRTHDAY TO YOU !!");
} else {
System.out.println("Your birthday is not today ");
}
}
} 출력
The required packages have been imported Today's Date is 2022-02-09 The birthday is defined as : 15 JULY Your birthday is not today
예시 2
여기에서 정수는 이전에 정의되었으며 그 값은 콘솔에 액세스되어 표시됩니다.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Birthday {
public static void main(String[] args) throws ParseException {
SimpleDateFormat s = new SimpleDateFormat("MM-dd");
Date today = s.parse("10-15");
Date my_birthday_date = s.parse("10-15");
System.out.println("The birthday date is defined as October 15th" );
if (today.compareTo(my_birthday_date) == 0) {
System.out.println("Happy Birthday!!");
} else {
System.out.println("Today is not tour birthday");
}
}
} 출력
The birthday date is defined as October 15th Happy Birthday!!