我有一個文本文件,我從中導入數據是關於庫中項目的信息。這些信息之一是項目借出的日期。我想不出有什麼語法我需要從文件中的文本,將在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」關鍵字; –