使用java从文件中读取日期

时间:2015-02-21 17:26:24

标签: java date filereader

我很想让程序从txt文件读取日期到Date对象。这是我正在使用的代码:

reader = new FileReader(fich); 
rd = new Scanner(reader);
DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy");
Date tmpdate= formatter.parse(rd.nextLine());

这就是我想要阅读的内容: 2月20日星期五01:23:35 GMT 2015

然后它会显示此错误: 线程“main”中的异常java.text.ParseException:Unparseable date:“Fri Feb 20 01:23:35 GMT 2015”

出了什么问题?

1 个答案:

答案 0 :(得分:2)

似乎FriFeb在您的语言环境中不是正确的名称。尝试指定一个正确的地方,如Locale.ENGLISH

所以而不是

new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy");

使用

new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy", Locale.ENGLISH);