我希望在给定日期之后显示明天日期

时间:2015-10-26 07:36:41

标签: 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("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 + ".");

3 个答案:

答案 0 :(得分: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 + ".");

答案 1 :(得分:1)

我会将JodaTime用于所有这些。那里有一个plusDays函数

DateTime dt = new DateTime(2015,9,20, 0, 0);
DateTime oneDayLater = dt.plusDays(1);

有一个DateTime ctor允许你自1970年以来传入millis,这将允许你轻松地使用java的Calendar和Date类。

答案 2 :(得分:0)

java.time

在Java 8及更高版本中,java.time框架处理此任务。

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