如何将用户输入格式化为日期变量?

3

如何将用户输入的日期、月份和年份存储为dd-mm-yyyy格式?我能够让所有内容运行,但无法将它们组合成一个日期并将其存储在startDate变量中。由于startMonth位于switch case中,我也不认为它是可访问的,但我对java非常陌生,不确定。

public static void carBookingDates() {

    int carNumber;
    int startYear;
    int startDay;
    int startMonth;
    int daysInMonth = 0;
    int endYear;
    int endMonth;
    int endDay;
   
    Scanner input = new Scanner(System.in);

    System.out.println("To make a booking:");
    System.out.printf("        Select a car number from the car list: ");
    carNumber = input.nextInt();
    System.out.println("");
    System.out.println("Enter a booking start date.");
    System.out.printf("Please enter the year - for example '2022': ");
    startYear = input.nextInt();
    while (startYear < 2022 || startYear > 2030) {
        System.out.println("Invalid year, please try again: ");
        System.out.printf("Please enter the year - for example '2022': ");
        startYear = input.nextInt();
    }
    System.out.printf("Please enter the month - for example '6': ");
    startMonth = input.nextInt();
    while (startMonth < 1 || startMonth > 12) {
        System.out.println("Invalid month, please try again: ");
        System.out.printf("Please enter the month - for example '6': ");
        startMonth = input.nextInt();
    }
    switch (startMonth) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            daysInMonth = 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            daysInMonth = 30;
            break;
        case 2:
            if (((startYear % 4 == 0)
                    && !(startYear % 100 == 0))
                    || (startYear % 400 == 0)) {
                daysInMonth = 29;
            } else {
                daysInMonth = 28;
            }
            break;
        default:
            System.out.println("Invalid month.");
            break;
    }
    System.out.printf("Please enter the day number - for"
            + " example '18': ");
    startDay = input.nextInt();
    while (startDay > daysInMonth || startDay <= 0) {
        System.out.println("Invalid day, plese try again");
        System.out.printf("Please enter the day number - for"
                + " example '18': ");
        startDay = input.nextInt(); 
        LocalDate startDate() = LocalDate.parse(startDay + "-" + StartMonth "-" + startYear);
    }
}

dd-mm-yyyy 指定了日期的表示形式,为什么您要将其存储在任何特定格式中呢? - Maarten Bodewes
它将被用于另一个类中,所需格式为dd-mm-yyyy。 - Kris Ellison
3
Java 包含完整的 API 来处理日期。无需试图计算一个月中有多少天,这些都已为您处理好了,不需要重新发明轮子。正如@MaartenBodewes建议的那样,学习数据类型和它们的“表示”之间的差异非常重要。 - Jim Garrison
LocalDate startDate() = 无法编译,移除 () - f1sh
1个回答

3

首先,您可以使用Java 8日期时间API获取一个月中的天数,而不是使用手动切换来使代码更易读。

import java.time.YearMonth;

然后:

// 2021 is the year and 2 is the month
int daysInMonth = YearMonth.of(2021,2).lengthOfMonth());

现在要以dd-mm-yyyy的格式组成日期:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

然后:

// pass your date value here under LocalDate.of method
LocalDate.of(2021,2,16).format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));

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