2017-02-11 32 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

非常感谢你的彻底回应!这是我第一次在编程中处理日期和时间,我很感谢你在最佳实践中指导我。 –

相关问题