在另一个类中将日期的字符串值作为日期变量调用

huangapple 未分类评论45阅读模式
英文:

calling String value of date in another class as Date variable

问题

我有一个字符串格式的出生日期,在Date类中,我想在另一个类中使用构造函数将其调用为Date变量,根据我的UML图中的HealthProfile。我该如何做呢?还有,Date的Scanner输入类型是什么?我已经完成了其他部分,下面的代码是Date类中的我的getDateString方法。

public String getDateString()
{
    String dob = String.valueOf(day) + month + String.valueOf(year);
    return dob;
}

我尝试打印的出生日期格式是,例如:2000年10月8日。我有一个包含此类数据的txt文件,并尝试从中调用。

英文:

在另一个类中将日期的字符串值作为日期变量调用

I have a date of birth in string in Date class and I want to call it as Date variable in another class using constructor, HealthProfile as per my uml diagram. How can I do that? And also, what is the Scanner input type for Date? I have done other parts and below code is my getDateString method in Date class.

public String getDateString()
	{
		String dob = String.valueOf(day) + month + String.valueOf(year);
		return dob;
	}

The date of birth format I was trying to print is, example: 8 October 2000. I have a txt file of such data and trying to call from it.

答案1

得分: 0

以下是您要求的代码的翻译部分:

Date类:

public class Date {
    int year;
    String month;
    int day;

    public Date(int year, String month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }
    
    /**
    * Getter和Setter方法
    */

    public String getDateString(){
        String dob = String.valueOf(day) + month + String.valueOf(year);
        return dob;
    }
}

测试主类:

public static void main(String[] args) throws ParseException {

    Scanner scanner = new Scanner(System.in);
    String inputstr = scanner.nextLine(); //您输入 24-07-2020

    String[] inputarr = inputstr.split(" ");
    Date date = new Date(Integer.valueOf(inputarr[2]), inputarr[1], Integer.valueOf(inputarr[0]));

    System.out.println(date.getDateString());

}
英文:

I help you to create a Date Class with a contruct method.
In main class, i help you to use System.in to input data string and return what you method want.

Date Class:

public class Date {
    int year;
    String month;
    int day;

    public Date(int year, String month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }
    
    /**
    * Getter and Setter methods
    */

    public String getDateString(){
        String dob = String.valueOf(day) + month + String.valueOf(year);
        return dob;
    }

Test main class:

    public static void main(String[] args) throws ParseException {

        Scanner scanner = new Scanner(System.in);
        String inputstr = scanner.nextLine(); //you enter 24-07-2020

        String[] inputarr = inputstr.split(" ");
        Date date = new Date(Integer.valueOf(inputarr[2]), inputarr[1], Integer.valueOf(inputarr[0]));

        System.out.println(date.getDateString());

    }

答案2

得分: 0

java.util.Scanner没有用于读取您的Date类实例的方法(这可能是显而易见的)。我建议您使用:

  • nextInt() 读取月份中的日期(例如,8)
  • next() 读取月份名称(例如,October
  • nextInt() 读取年份(例如,2000)

通过获取这三个值,您可以调用您的构造函数 Date(int day, String month, int year)。您可能知道,Scanner 可以从文本文件中读取。

除此之外(如果无法使用,请忽略):

  1. 对于将日期转换回字符串,我建议您在元素之间加入空格,以再次获得类似 8 October 2000 而不是难以阅读的 8October2000
  2. 人们可以考虑使用枚举来表示月份。这会增加输入的复杂性,但会验证输入的正确月份名称。如果这不是您的要求的一部分,您可以将此注释保留给其他读者。
  3. 在实际工作中,人们会使用来自 java.timeLocalDate,这是现代的 Java 日期和时间 API,而不是开发自己的 Date 类。

链接: Oracle 教程:日期时间 解释了如何使用 java.time。

英文:

java.util.Scanner hasn’t got a method for reading an instance of your Date class (it may be obvious). I suggest that you use

  • nextInt() for reading the day of month (e.g., 8)
  • next() for reading the month name (e.g., October)
  • nextInt() for reading the year (e.g., 2000)

With the three values thus obtained you can call your constructor Date(int day, String month, int year). As you may know, a Scanner can read from a text file.

Aside from this (ignore if you can’t use it):

  1. For the conversion back to a string I suggest that you put spaces between the elements to get like 8 October 2000 again rather than 8October2000, which is unpleasant to read.
  2. One may consider an enum for the months. It will complicate input, but will provide validation that a correct month name is entered. If this is not part of your requirement, you may leave this comment for other readers.
  3. For production work one would use LocalDate from java.time, the modern Java date and time API, and not develop one’s own Date class.

Link: Oracle tutorial: Date Time explaining how to use java.time.

huangapple
  • 本文由 发表于 2020年7月24日 13:00:41
  • 转载请务必保留本文链接:https://java.coder-hub.com/63067136.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定