Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读取类'的用户输入;日期';_Java - Fatal编程技术网

Java 读取类'的用户输入;日期';

Java 读取类'的用户输入;日期';,java,Java,你好,我是JAVA新手,我一直在网上搜索这个问题,但我找不到解决方案 所以我试图读取用户输入并将其存储在vector中,但其中一个包含日期输入。现在,我创建了一个名为“Date”的类,所以我必须使用Date作为类型,但是我不知道应该将什么作为nextLine或NextInt-以便读取 public static void edit(){ viewAll(); Scanner in = new Scanner (System.in); int i;

你好,我是JAVA新手,我一直在网上搜索这个问题,但我找不到解决方案

所以我试图读取用户输入并将其存储在vector中,但其中一个包含日期输入。现在,我创建了一个名为“Date”的类,所以我必须使用Date作为类型,但是我不知道应该将什么作为nextLine或NextInt-以便读取

public static void edit(){
      viewAll();
      Scanner in = new Scanner (System.in);
      int i;


      System.out.println("Enter the Animal Code you wish to edit:");
      String cd = in.nextLine();

      if (cd == code){

       System.out.print("What is the animal's type? "); 
        String type2 = in.nextLine();
        n.add(type2);

       System.out.print("Enter the animal's unique code - Format: (XXXX111)");
       String code2 = in.nextLine();
       n.add(code2);
       //unique
       //XXXX102
       System.out.print("Enter the animal's weight:" ); 
       int weight2 = in.nextInt();
       n.add(weight2);

       System.out.print("Enter the date that the animal was obtained on: (dd/mm/yyyy)");
       Date date2 = in.next();

       list.addAll(n);

       //use date class
       System.out.print("Enter the animal's room and section (location) in the park:");
       String location2 = in.nextLine();
       n.add(location2);



       System.out.println();
       System.out.println("Changes have been saved");
       System.out.println();

     }

首先导入所需的库,包括日期:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
然后,您可以将输入作为字符串,并通过执行以下操作将其转换为日期:

    DateFormat formatter = new SimpleDateFormat("dd/mm/yyyy");
    String dateString = in.nextLine();
    Date animalDate = formatter.parse(dateString);
您不需要创建日期类,只需创建java.util库即可。

tl;博士 切勿使用
Date

LocalDate.parse( "2019-01-23" )
细节 可怕的
java.util.Date
类在几年前被中定义的java.time类所取代。具体来说,
java.time.Instant
取代了
Date

如果只需要日期值,而不需要一天中的时间和时区,请使用
LocalDate

默认情况下,java.time类在解析/生成字符串时使用标准格式。仅适用于YYYY-MM-DD日期

String input = "2019-01-23" ;
LocalDate ld = LocalDate.parse( input ) ;
如果您想支持用户输入的其他格式,请了解该类及其calizeddate方法的
。已经报道过很多次了

DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.MEDIUM ).withLocale( Locale.CANADA_FRENCH ) ;
String output = LocalDate.parse( "2019-01-23" ).format( f ) ; 
11月23日。2019年


Java已经有了一个用于
Date
类型的类。另外,在Java8+中,首选的格式是使用
Java.time.LocalDate
。您需要将输入读取为
字符串
,并将其解析为
日期
本地日期
对象。多年前,JSR 310中定义的java.time类取代了这些糟糕的类。建议在2019年使用它们是一个糟糕的建议。你能解释一下为什么
Date
是一个糟糕的类吗?(除了老了)@matanper快速搜索堆栈溢出将发现这一点。从名字开始,
Date
,它实际上包含一天中的日期和时间。添加一个说谎的
toString
方法。还认为,类声称在UTC中代表一个时刻,但是在其源代码中神秘地隐藏了一个时区,它影响某些行为,但不是全部——并且对该区域缺少吸气剂/定位器以使其更加有趣。基于对面向对象编程的糟糕理解,加入一些糟糕的设计。然后在上面加上一点可变性。毛毛雨对螺纹安全性的缺乏。