我有一个文本文件,我从中导入数据是关于库中项目的信息。这些信息之一是项目借出的日期。我想不出有什么语法我需要从文件中的文本,将在DD/MM/YYYY格式的读取和使用将输入从文件转换为java中的日期
import java.util.Date;
因为它目前是转换为日期值,误差“找不到符号
符号:方法的SimpleDateFormat(字符串) 位置:类数字图书馆
我不想它来寻找我写了一个方法......相反,我相信我想要它做的是使用
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);
}
}
这是单独的代码,我已经写了我的项目类中,当我问什么时候该项目被租借类似和工程用户:
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;
}
再次感谢您的任何指导!
我确实使用了导入。对不起,我在代码之前说明了导入,但没有明确说明我导入了它。我相信我所需要的只是像你说的那样“新”!这是尴尬,大声笑。 –
好吧,我的例子包括'new'关键字怎么样? – Buhb
是的,我需要a.setDateLoaned(SimpleDateFormat(dateFormat).parse(reader.nextLine()))中的“new”关键字; –