我已经实现了以下代码来计算剩余的天数。我使用SimpleDateFormat将字符串转换为日期。如果输入10/02/1993(今天的日期)作为日期,则输出显示还有0天但如果我选择1993年2月11日,它显示的输出与0天后相同但如果我把月份改成1993年7月22日,它显示还有160天,而不是原来的162天。我在这里做错什么了?任何帮助都非常感谢。

private static void calculateNextBirthday() {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter your birthday(dd/MM/yyyy)");
        user_input = in.nextLine();

        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

        Calendar user_selected_day = new GregorianCalendar();

        Calendar today = new GregorianCalendar();

        Date selected_date = null;
        try {
            selected_date = formatter.parse(user_input);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        user_selected_day.setTime(selected_date);

        int user_selected_month = user_selected_day.get(Calendar.MONTH);
        int CMonth = today.get(Calendar.MONTH);

        int user_selected_date = user_selected_day.get(Calendar.DAY_OF_MONTH);
        int CDate = today.get(Calendar.DAY_OF_MONTH);

        user_selected_day.set(Calendar.YEAR, today.get(Calendar.YEAR));

        user_selected_day.set(Calendar.DAY_OF_WEEK,today.get(Calendar.DAY_OF_WEEK));

        if (user_selected_month < CMonth) {
            user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);
        }
        else if (user_selected_month == CMonth){
            if(user_selected_date < CDate){
                user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);
            }
        }

        long millis = user_selected_day.getTimeInMillis() - today.getTimeInMillis();
        long days;
        days = (millis / (24 * 60 * 60 * 1000));

        System.out.println(String.valueOf(days) + " days left!!");
    }

更新:我已经更新了下面的代码,它工作正常。希望对将来的人有帮助
`       SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

        Calendar user_selected_day = new GregorianCalendar();

        Calendar today = new GregorianCalendar();

        Date selected_date = null;

        try {
            selected_date = formatter.parse(plainDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        user_selected_day.setTime(selected_date);

        int user_selected_month = user_selected_day.get(Calendar.MONTH);
        int CMonth = today.get(Calendar.MONTH);

        int user_selected_date = user_selected_day.get(Calendar.DAY_OF_MONTH);
        int CDate = today.get(Calendar.DAY_OF_MONTH);

        user_selected_day.set(Calendar.YEAR, today.get(Calendar.YEAR));

        user_selected_day.set(Calendar.HOUR_OF_DAY,today.get(Calendar.HOUR_OF_DAY));
        user_selected_day.set(Calendar.MINUTE,today.get(Calendar.MINUTE));
        user_selected_day.set(Calendar.SECOND,today.get(Calendar.SECOND));
        user_selected_day.set(Calendar.MILLISECOND,today.get(Calendar.MILLISECOND));


        if (user_selected_month < CMonth) {
            user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);
        }
        else if (user_selected_month == CMonth){
            if(user_selected_date < CDate){
                user_selected_day.set(Calendar.YEAR,today.get(Calendar.YEAR) + 1);
            }

        }

        long millis = user_selected_day.getTimeInMillis() - today.getTimeInMillis();
        long days;
        if(user_selected_day.getTime()==today.getTime()){
            days = 0;
        }
        else{
            days = (millis / (24 * 60 * 60 * 1000));
        }`

最佳答案

问题出在线路上

user_selected_day.set(Calendar.YEAR, today.get(Calendar.YEAR));

这将把今天的年份设置为user_selected天,并且由于月是相等的,它将进入elseif中,并在不做任何更改的情况下离开它。
还要检查您提到的if-else条件。

10-08 04:14