Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我想在给定日期后显示明天的日期_Java_Date_If Statement_Calendar - Fatal编程技术网

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

Java 我想在给定日期后显示明天的日期,java,date,if-statement,calendar,Java,Date,If Statement,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("Ent

我想显示给定日期(月日期年)之后的明天的日期 我没有编程经验,但我正在努力习惯这样做。我写了这个,但它甚至不工作,我被困在这个。有人能看一下吗

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 + ".");

为此使用
日历
类:

    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 + ".");
所有这些我都会用到。这里有一个函数

DateTime dt = new DateTime(2015,9,20, 0, 0);
DateTime oneDayLater = dt.plusDays(1);
自1970年以来,有一个DateTime允许您以毫秒为单位传递,这将允许您轻松使用java的日历和日期类

java.time 在Java8和更高版本中,框架处理此任务

LocalDate dayAfter = LocalDate.of( 2012 , 3 , 4 ).plusDays( 1 );

允许您使用Java日期API吗?您正在使用Java8吗?有足够多的方法可以设置日期并执行如下操作:addDays(1);以JodaTime为例,了解如何使用java 8 date api(这将解决您的问题),当您说它不起作用时,这是什么意思?您尝试了哪些输入,得到了哪些输出?另外,尝试在不同的步骤中通过代码添加调试语句,以检查它是否符合您期望的路径(即使用
System.out.println()
来打印过程中的值…)小的细微更正OP在输入日期+1天之后,因此您需要修复示例代码…@Nim,您可以解释更多细节吗。我没听懂,很抱歉你现在的代码加了一个月。。。对于输入日,op希望添加1天