【问题标题】:How do I format user input into a date variable?如何将用户输入格式化为日期变量?
【发布时间】:2022-01-26 19:19:06
【问题描述】:

如何获取用户输入的日、月和年,然后以 dd-mm-yyyy 格式存储?除了将它们组合成日期并将它们存储在 startDate 变量中之外,我可以让一切正常工作。我也不认为 startMonth 可以访问,因为它位于 switch 案例中,但我对 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 指定日期的表示,为什么要以任何特定格式存储它?
  • 在其他类中使用,格式为dd-mm-yyyy
  • Java 包含一个完整的 API 来处理日期。没有必要重新发明轮子来计算一个月的天数,这一切都为您处理。正如@MaartenBodewes 建议的那样,了解数据类型与其表示形式之间的区别很重要。
  • LocalDate startDate() = 不会编译,删除()

标签: java date datetime user-input


【解决方案1】:

首先,您可以使用 Java 8 Date Time API 来获取一个月中的天数,而不是使用手动 switch case 来使代码更具可读性。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    相关资源
    最近更新 更多