如何在 Java 5 中从文本文件中读取时间和日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6768405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:11:30  来源:igfitidea点击:

How to Read Time and Date from Text File in Java 5?

javaparsingdatetimefile-iotext-files

提问by dvanaria

I'm trying to read data from a plain text file using Java 5 SE. The data is in the following formats:

我正在尝试使用 Java 5 SE 从纯文本文件中读取数据。数据采用以下格式:

10:48 AM
07/21/2011

I've looked into DateFormat and SimpleDateFormat, but I can't figure out the most straight-forward way of reading this data into a Date object.

我已经研究了 DateFormat 和 SimpleDateFormat,但我无法找出将这些数据读入 Date 对象的最直接方法。

Here's what I have so far:

这是我到目前为止所拥有的:

import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

class Pim {

    File dataFile;
    BufferedReader br;
    String lineInput;
    Date inputTime;
    Date inputDate;

    public Pim() {

        dataFile = new File("C:\Data.txt");

        try {

            br = new BufferedReader(new FileReader(dataFile));

            lineInput = br.readLine();
            inputTime = new Date(lineInput);

            lineInput = br.readLine();
            inputDate = new Date(lineInput);            

            br.close();

        } catch (IOException ioe) {

            System.out.println("\n An error with the Data.txt file occured.");
        }
    }
}

Am I on the right track here? What's the best way to do this?

我在正确的轨道上吗?做到这一点的最佳方法是什么?

回答by Marsellus Wallace

First concat the two lines to have something like this: String date = "07/21/2011 10:48 AM"

首先将两行合并为如下内容: String date = "07/21/2011 10:48 AM"

DateFormat formatter = new SimpleDateFormat("MM/dd/yy h:mm a");
Date date = (Date)formatter.parse(date);

This should work, you can refer to SimpleDateFormat APIfor more options.

这应该有效,您可以参考SimpleDateFormat API以获取更多选项。

回答by Kumar Bibek

http://www.kodejava.org/examples/19.html

http://www.kodejava.org/examples/19.html

Change the SimpleDateFormat param as per your format.

根据您的格式更改 SimpleDateFormat 参数。

回答by Jonik

Using a library such as Guavainstead of writing your own file reading boilerplate code, you could get away with something like:

使用诸如Guava 之类的库而不是编写自己的文件读取样板代码,您可以使用以下内容:

 List<String> lines = 
     Files.readLines(new File("C:\Data.txt"), Charset.forName("UTF-8"));

 DateFormat timeFormat = new SimpleDateFormat("hh:mm a");
 Date time = timeFormat.parse(lines.get(0));

 DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
 Date date = dateFormat.parse(lines.get(1));

(Handling of IOException and ParseException omitted in above example.)

(上例中省略了 IOException 和 ParseException 的处理。)

回答by Shiva

Try this,

试试这个,

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class StringToDateDemo
{
   public static void main(String[] args) throws ParseException  
   {
      String strDate = "05/23/14 5:10 am";
      SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy h:mm a");
      Date dt = sdf.parse(strDate);
      System.out.println(dt);
   }
}

For more refer links below,

如需更多参考链接,请参阅以下链接,

https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html

https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html

http://www.flowerbrackets.com/java-convert-string-date/

http://www.flowerbrackets.com/java-convert-string-date/