【问题标题】:Converting input from a file to a Date in java在java中将输入从文件转换为日期
【发布时间】: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


    【解决方案1】:

    没有看到错误的完整准确打印输出,我无法确定,但看起来您还没有导入 SimpleDateFormat。 在你的类声明之前,添加行import java.text.SimpleDateFormat;,你应该已经准备好了。你试过了吗?

    另外,你忘记了new。 试试这个:

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

    【讨论】:

    • 我确实使用了导入。抱歉,我在代码之前说明了导入,但没有明确说明我导入了它。我相信我所需要的只是你说的“新”!太尴尬了,哈哈。
    • 好的,我的例子中包含new 关键字怎么样?
    • 是的,我需要 a.setDateLoaned(SimpleDateFormat(dateFormat).parse(reader.nextLine())) 中的“new”关键字;
    【解决方案2】:

    避免使用旧的日期时间类

    不确定您的错误。您绝对需要有一个import 声明,以适合您要在代码中调用的类。

    更重要的是:

    • 您不恰当地尝试在日期时间对象中表示仅日期值。
    • 您正在使用麻烦的旧日期时间类,这些类现在是遗留的,被 java.time 类所取代。

    尽可能使用 java.time 类,同时避免遗留类。不需要Date 也不需要SimpleDateFormat

    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 作为截止日期。但是,如果根据合同规定,到期的确切时间很关键,或者您的应用可能跨时区使用,您应该使用日期时间值而不是仅日期值。

    在这种情况下,请在 Stack Overflow 上搜索 java.time 类 InstantZoneIdZonedDateTimeLocalDateLocalTimeLocalDateTime 的许多讨论和示例。

    【讨论】:

    • 非常感谢您的彻底回复!这是我第一次在编程中处理日期和时间问题,感谢您在最佳实践中指导我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    相关资源
    最近更新 更多