简体   繁体   English

日期作为用户输入使用我自己的日期 class

[英]Date as a user input using my own Date class

I'm developing an application, and in there I have to take the date from the user, and it's only date, time is not required.我正在开发一个应用程序,在那里我必须从用户那里获取日期,它只是日期,不需要时间。 And I was asked to use a Date class to represent the date.我被要求使用Date class 来表示日期。 But I don't have any idea on how to get the date from the user as a normal input using scanner class.但是我不知道如何使用扫描仪 class 从用户那里获取日期作为正常输入。

The date class is shown below, so how can I get the input as a date?日期 class 如下所示,那么如何将输入作为日期?

I'm new to programming so please help me to solve this issue.我是编程新手,所以请帮我解决这个问题。

public class Date {

    private int day;
    private int month;
    private int year;

    public Date(int day, int month, int year) {
        this.day = day;
        this.month = month;
        this.year = year;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    @Override
    public String toString() {
        return "Date{" +
                "day=" + day +
                ", month=" + month +
                ", year=" + year +
                '}';
    }
}

This is an assignment, and it asks us to implement a Date class like this, and it asks us to avoid using any predefined libraries in Java.这是一个任务,它要求我们像这样实现一个Date class,它要求我们避免使用 Java 中的任何预定义库。

java.time.LocalDate

We have a class for that: LocalDate .我们有一个 class : LocalDate Do not reinvent the wheel.不要重新发明轮子。

And be aware that date-time handling is surprisingly tricky work — do not attempt roll-your-own solutions.请注意,日期时间处理是一项非常棘手的工作——不要尝试自行解决。 Always use the industry-leading java.time classes.始终使用行业领先的java.time类。

LocalDate ld = LocalDate.of( 2021 , 1 , 23 ) ;  // January 23, 2021.
String output = ld.toString() ;

2021-01-23 2021-01-23

In a console app, using the Scanner class, ask the user to input a date in ISO 8601 format YYYY-MM-DD.在控制台应用程序中,使用Scanner class,要求用户输入 ISO 8601 格式 YYYY-MM-DD 的日期。

LocalDate ld = LocalDate.parse( "2021-01-23" ) ;

Or, ask for the year, month, and day inputs separately.或者,分别要求输入年、月和日。 Parse each as an integer , and pass the three int values to LocalDate.of as seen above. 将每个解析为 integer ,并将三个int值传递给LocalDate.of ,如上所示。

Tips:提示:

  • Organize your thinking and code to work with greater scope first, and detailed scope later.整理您的思维和代码,以便首先使用更大的 scope,然后再详细使用 scope。 So year-month-day rather than day-month-year.所以年月日而不是日月年。
  • Learn about ISO 8601 standard for formatting date-time values as text when exchanging data.了解在交换数据时将日期时间值格式化为文本的ISO 8601标准。
  • Study the java.time tutorial by Oracle .学习 Oracle 的java.time教程

I understand that this is homework and that you are not allowed to use LocalDate from the standard library (as one would unconditionally do in production code).我知道这是家庭作业,并且不允许您使用标准库中的LocalDate (就像在生产代码中无条件地做的那样)。

Options include (and are not limited to):选项包括(但不限于):

  1. Read day of month, month and year as three numbers in turn using three calls to Scanner.nextInt() .使用对Scanner.nextInt()的三个调用依次将月、月和年的日期读取为三个数字。 Pass the three numbers to your Date constructor.将这三个数字传递给您的Date构造函数。
  2. Read the date as for example 29-05-2020 from the user using either Scanner.next() or Scanner.nextLine() .使用Scanner.next()Scanner.nextLine()从用户那里读取日期,例如29-05-2020 Split at hyphens.在连字符处拆分。 Parse each number to an int using Integer.parseInt() .使用Integer.parseInt()将每个数字解析为一个int Pass the three parsed numbers to the constructor.将三个解析的数字传递给构造函数。

Doc links:文档链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM