我的 main 中有一行是这样的:
Date gameDate = DateFormat.parse(scanner.nextLine());
本质上我想用 util.Scanner 扫描一个日期
哪个命中错误:
Cannot make a static reference to the non-static method parse(String) from the type DateFormat
现在,我已经调查了这个错误,但它似乎不像这个 example 那样清晰。 .
我该如何解决这个问题?
最佳答案
parse()
不是静态方法。这是一个实例方法。您需要创建一个 DateFormat 实例,然后在该实例上调用 parse()
:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date gameDate = dateFormat.parse(scanner.nextLine());
静态方法属于一个类。调用 Person.getName()
没有意义。但是打电话是有道理的
Person pureferret = new Person("Pureferret");
String name = pureferret.getName();
关于java - 解析日期并在 Java 中遇到 'static reference to the non-static method' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11412901/