【发布时间】:2017-02-11 14:11:53
【问题描述】:
我有一个要从中导入数据的文本文件,该文件是有关库中项目的信息。这些信息之一是物品被借出的日期。我无法弄清楚我需要什么语法才能从文件中获取文本,该文件将以 dd/MM/yyyy 格式读入并使用
转换为日期值import java.util.Date;
就目前而言,错误是“找不到符号”
符号:方法 SimpleDateFormat(String) 位置:类数字图书馆
我不希望它寻找我写的方法......相反,我相信我想要它做的是使用
import java.text.SimpleDateFormat;
任何帮助将不胜感激
public static Item createLibrary (ArrayList <Item> library)
{
Scanner reader = null;
try {
File inputFile = new File("library.txt");
reader = new Scanner(inputFile);
} catch (FileNotFoundException ex) {
System.out.println("Could not open the file.");
System.exit(0);
}
//This loop fills the ArrayList with the input from the library.txt file
for (int i =0; reader.hasNext(); i++)
{
Item a = new Item();
a.setTitle(reader.nextLine());
a.setFormat(reader.nextLine());
a.setOnLoan(Boolean.parseBoolean(reader.nextLine()));
a.setLoanedTo(reader.nextLine());
//My problem is in the next two lines of code, specifically
//SimpleDateFormat
String dateFormat = "dd/MM/yyyy";
a.setDateLoaned(SimpleDateFormat(dateFormat).parse(reader.nextLine()));
reader.nextLine();
//This adds the item with all the attributes to the ArrayList library
library.add(a);
}
}
这是我在我的 Item 类中编写的单独代码,用于询问用户何时借出类似且有效的项目:
public Date getTheDateLoaned()
{
System.out.println("What day was it loaned out in dd/MM/yyyy format?");
String dateFormat = "dd/MM/yyyy";
Scanner scanner = new Scanner(System.in);
try
{
return new SimpleDateFormat(dateFormat).parse(scanner.nextLine());
}
catch (ParseException ex)
{
System.out.println("This was not in the right format. The format"
+ "needs to be dd/MM//yyyy");
}
return null;
}
再次感谢您的指导!
【问题讨论】:
标签: java date type-conversion string-conversion java.util.date