从文件中读取日期时出现ParseException

时间:2015-03-02 19:52:17

标签: java parse-error

我正在尝试使用程序从文本文件中读取的数据创建一个对象。数据由同一程序放入文本文件中。我分割线条,然后将每个线段粘贴到String ArrayList中。它一直运作良好,直到我到达日期。将字符串日期解析为Date类型会导致ParseException错误。我没有正确解析它的任何想法?谢谢!

BufferedReader bufReader = new BufferedReader(new 
FileReader("open_tickets.txt"));
String line = bufReader.readLine();    
ArrayList<String> words = new ArrayList<String>();

    while ((line = bufReader.readLine()) != null){
        String[] split = line.split("= ");
        for (int i = 0; i < split.length; i++){
            words.add(split[i]);
            if (split[i].endsWith("2015")){
                String arrayPriority = words.get(1); //"5"
                String description = words.get(3); //"Building on fire"
                String reportedBy = words.get(5); //"Tim"
                String arrayDate = words.get(7); //"Mon Mar 02 13:31:24 CST   
2015"


                int priority = Integer.valueOf(arrayPriority); //5
                SimpleDateFormat formatter = new SimpleDateFormat ("EEE MM dd HH:mm:ss z yyyy");
                try {
                    Date date = formatter.parse(arrayDate);

                } catch (ParseException e){
                    e.printStackTrace();
                }

StackTrace:java.text.ParseException:Unparseable date:&#34; Mon Mar 02 13:31:24 CST 2015&#34;     at java.text.DateFormat.parse(DateFormat.java:366)     在----。company.Main.addTicketFromFile(Main.java:257)     在----。company.Main.main(Main.java:17)

public Date parse(String source) throws ParseException
{
    ParsePosition pos = new ParsePosition(0);
    Date result = parse(source, pos);
    if (pos.index == 0)
        throw new ParseException("Unparseable date: \"" + source + "\"" ,
            pos.errorIndex);
    return result;
}

1 个答案:

答案 0 :(得分:1)

尝试

SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");