2017-02-11 28 views
0

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

再次感謝您的任何指導!

回答

0

沒有看到錯誤的完整打印輸出,我不能確定,但​​它看起來像你沒有導入SimpleDateFormat。 在你的課堂陳述之前,添加import java.text.SimpleDateFormat;這一行,你應該全部設置。你試過這個嗎?

此外,你忘了new。 試試這個:

a.setDateLoaned((new SimpleDateFormat(dateFormat)).parse(reader.nextLine())); 
+0

我確實使用了導入。對不起,我在代碼之前說明了導入,但沒有明確說明我導入了它。我相信我所需要的只是像你說的那樣「新」!這是尷尬,大聲笑。 –

+0

好吧,我的例子包括'new'關鍵字怎麼樣? – Buhb

+0

是的,我需要a.setDateLoaned(SimpleDateFormat(dateFormat).parse(reader.nextLine()))中的「new」關鍵字; –

0

避免遺留日期時類

不知道你的錯誤。您肯定需要有一個import語句適合您要在代碼中調用的類。

更重要的是:

  • 你不適當地試圖代表的日期時間對象的日期,唯一的價值。
  • 您正在使用麻煩的舊日期時間類,它們現在是遺留的,取而代之的是java.time類。

儘可能避免遺留類使用java.time類。不需要DateSimpleDateFormat

LocalDate

LocalDate類表示沒有時間一天和不同時區的日期,唯一的價值。

重要提示:將日期時間值作爲文本序列化時,請使用標準ISO 8601格式。標準格式是合理的,實用的,易於人類跨文化閱讀,並易於通過機器解析。解析/生成字符串時,java.time類默認使用標準格式。以本地化格式呈現日期時間值以適合用戶,但以標準格式存儲和交換數據。

對於僅包含日期的值,標準格式爲YYYY-MM-DD,例如2017-01-23

定義格式模式以匹配您的輸入。

DateTimeFormatter f = DateTimeFormatter.ofPattern("dd/MM/uuuu"); 

解析輸入字符串。

LocalDate ld = LocalDate.parse("23/01/2017" , f); 

如果輸入不正確,則爲陷阱DateTimeParseException

時區

如果這個應用程序是一個小的本地庫,你可以使用LocalDate的到期日期。但是,如果簽約時間恰好到達關鍵時刻,或者您的應用可能跨時區使用,則應該使用日期時間值而不是僅包含日期的值。

在這種情況下,搜索棧溢出了java.time類InstantZoneIdZonedDateTimeLocalDateLocalTimeLocalDateTime的許多討論和例子。

+1

非常感謝你的徹底迴應!這是我第一次在編程中處理日期和時間,我很感謝你在最佳實踐中指導我。 –

相關問題