Code Java tính tuổi và đếm ngày đến sinh nhật

Em muốn chương trình này đếm được số ngày sinh nhật của bạn sinh 29/2. Các Anh giúp em với em cảm ơn. Dưới đây là code của em

package buoi5.j52_VuMinhLam_buoi5;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class Bai2 {
    private static int year, month, date, yearFuture, leapOrNotLeap;

    public static void main(String[] args) throws ParseException {
        /**
         * input day of birth and find the age from day of birth
         * */
        String strDayOfBirth = checkDayOfBirth();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        Date d = sdf.parse(strDayOfBirth);
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH) + 1;
        date = c.get(Calendar.DATE);
        LocalDate dob = LocalDate.of(year, month, date);
        LocalDate now1 = LocalDate.now();
        Period diff1 = Period.between(dob, now1); // find the age from day of birth

        // Birth day of the current
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        yearFuture = now1.getYear();
        LocalDate nextDOB = LocalDate.of(yearFuture, month, date);
        isLeap(yearFuture);

        //The date of birth turns into a string
        String fDOBString = dob.format(formatter);
        String fNOWString = now1.format(formatter);
        String fNextDOBString = nextDOB.format(formatter);

        // Format string into the date
        Date dateDOB;
        Date dateCurrent;
        dateDOB = sdf.parse(fNextDOBString);
        dateCurrent = sdf.parse(fNOWString);

        // Calculate the day of birth
        if (dateDOB.before(dateCurrent)) { //check date of birth before date current
            long difference = Math.abs(dateCurrent.getTime() - dateDOB.getTime());
            long differenceDates = isLeap(yearFuture) - (difference / (24 * 60 * 60 * 1000)); // if year current is leap (366 - differenceDates)

            if (differenceDates == 0) {
                System.out.println("Happy birth day !");
            } else {
                System.out.println(differenceDates + " days left is your birthday");
            }

        } else {
            long difference = Math.abs(dateDOB.getTime() - dateCurrent.getTime());
            long differenceDates = difference / (24 * 60 * 60 * 1000);

            if (differenceDates == 0) {
                System.out.println("Happy birth day !");
            } else {
                System.out.println(differenceDates + " days left is your birthday");
            }
        }
        System.out.println("You: " + diff1.getYears() + " year old");
        System.out.println("Day of birth: " + fDOBString);
        System.out.println("Today: " + fNOWString);
    }

    // check year current is leap or not leap
    private static int isLeap(int yearFutureCheck) {
        boolean checkLeap;
        if (yearFutureCheck % 4 == 0)
            if (yearFutureCheck % 100 == 0)
                if (yearFutureCheck % 400 == 0)
                    checkLeap = true;
                else checkLeap = false;
            else checkLeap = true;
        else checkLeap = false;
        if (checkLeap == true) leapOrNotLeap = 366;
        else leapOrNotLeap = 365;
        return leapOrNotLeap;
    }

    /**
     * Check regular expression in the data input
     */

    private static String checkDayOfBirth() {
        boolean check;
        String strDayOfBirth, rexgexDayOfBirth;
        Scanner scanner = new Scanner(System.in);
        do {
            rexgexDayOfBirth = "^[0-3]?[0-9]/[0-3]?[0-9]/(?:[0-9]{2})?[0-9]{2}$";
            System.out.println("Your birth is: ");
            strDayOfBirth = scanner.nextLine();
            check = strDayOfBirth.matches(rexgexDayOfBirth);
            if (!check) System.out.println("You must enter a date dd/MM/yyyy !");
        } while (!check);
        strDayOfBirth.lines();
        return strDayOfBirth;
    }
}

1 Like

Các ngày khác đều ổn phải không? Còn ngày 29/02 lại là ngày đặc biệt chỉ xuất hiện 4 năm 1 lần.
Việc của bạn là tìm ra năm nhuận kế tiếp gần nhất thôi. Tăng giá trị năm hiện tại lên lần lượt và kiểm tra, nếu đó là năm nhuận thì lần sinh nhật kế tiếp rơi vào năm đó.

// 29/02
int currentYear = ...;
int birthYear = currentYear + 1;
if(currentDay < birthDay && isLeap(currentYear)){
    birthYear = currentYear;
    // năm hiện tại là năm nhuận và chưa đến ngày 29/02.
}else{
    while(!isLeap(birthYear)){
        ++birthYear;
    }
    // lấy được năm nhuận kế tiếp rồi đó.
}
5 Likes

Vâng em cảm ơn anh nhiều lắm <3

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?