我想在给定日期后显示明天的日期

3

我想在给定日期(月日年)后显示明天的日期。虽然我没有编程经验,但我正在努力适应这一点。我编写了以下代码,但它甚至无法工作,我卡在这里了。有人能帮忙看一下吗?

int Year, Month, Day;
int tYear, tMonth, tDay;

System.out.print("Enter the month [1 to 12]: ");
System.out.print("Enter the day of the month [1 to 31]: ");
System.out.print("Enter the year: ");

tDay = Day + 1;
tMonth = Month;
tYear = Year;

if (Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12) {

    if (tDay > 31) {
        tMonth = Month + 1;
        tDay = 1;
    }
} else if (Month == 4 || Month == 6 || Month == 9 || Month == 11) {

    if (tDay > 30) {
        tMonth = Month + 1;
        tDay = 1;
    }
} else {

    if ((Year % 4 == 0) && (!(Year % 100 == 0) || (Year % 400) == 0)) {

        if (tDay > 29) {
            tMonth = 3;
            tDay = 1;
        }
    } else {
        if (tDay > 28) {
            tMonth = 3;
            tDay = 1;
        }
    }
}

if (tMonth == 13) {
    tMonth = 1;
    tYear = Year + 1;
}

System.out.println("Today's date is: " + Month +
    "/" + Day + "/" + Year + ".");
System.out.println("Tomorrow's date will be: " + tMonth +
    "/" + tDay + "/" + tYear + ".");

1
你可以使用Java Date API吗?你正在使用Java 8吗? - Andreas
1
有很多不错的方法可以设置日期并执行类似于addDays(1)的操作。例如,可以查看JodaTime库。 - Stultuske
一个使用Java 8日期API的示例(可以解决你的问题):https://dzone.com/articles/deeper-look-java-8-date-and - Emerson Cod
当你说它根本不起作用时,你指的是什么意思 - 你尝试了什么输入,得到了什么输出?此外,尝试在代码的不同步骤中添加调试语句,以检查它是否按照你预期的路径进行(例如,使用System.out.println()打印沿途的值)。 - Nim
3个回答

1
使用 Calendar 类来实现这个功能:
    int year, month, day;
    int tYear, tMonth, tDay;

    System.out.print("Enter the month [1 to 12]: ");
    System.out.print("Enter the day of the month [1 to 31]: ");
    System.out.print("Enter the year: ");

    // read values

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month - 1);
    calendar.set(Calendar.DATE, day);

    calendar.add(Calendar.DATE, 1);

    tYear = calendar.get(Calendar.YEAR);
    tMonth = calendar.get(Calendar.MONTH) + 1;
    tDay = calendar.get(Calendar.DATE);

    System.out.println("Today's date is: " + month +
            "/" + day + "/" + year + ".");
    System.out.println("Tomorrow's date will be: " + tMonth +
            "/" + tDay + "/" + tYear + ".");

我建议您使用DateFormat来格式化或解析Date

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

    int year, month, day;

    System.out.print("Enter the month [1 to 12]: ");
    System.out.print("Enter the day of the month [1 to 31]: ");
    System.out.print("Enter the year: ");

    // read values

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month - 1);
    calendar.set(Calendar.DATE, day);

    String today = format.format(calendar.getTime());

    calendar.add(Calendar.DATE, 1);

    String tomorrow = format.format(calendar.getTime());

    System.out.println("Today's date is: " + today + ".");
    System.out.println("Tomorrow's date will be: " + tomorrow + ".");

小的修正,OP想要的是输入日期+1天,所以你需要修复你的示例代码... - Nim
@Nim,你能详细解释一下吗?我没听清楚,抱歉。 - Igor Tyulkanov
您当前的代码是将一个月添加到输入的天数上,但用户希望添加1天。 - Nim

1
我会使用JodaTime来完成这个任务。那里有一个plusDays函数。
DateTime dt = new DateTime(2015,9,20, 0, 0);
DateTime oneDayLater = dt.plusDays(1);

有一个DateTime ctor,它允许您传递自1970年以来的毫秒数,这将使您能够轻松地使用Java的Calendar和Date类。


1

java.time

在Java 8及更高版本中,java.time框架负责处理此任务。
LocalDate dayAfter = LocalDate.of( 2012 , 3 , 4 ).plusDays( 1 );

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接