Search code examples
javaparse-error

ParseException When Reading Date from File


I am trying to create an object with data the program reads out of a text file. The data was put into the text file by the same program. I'm splitting the lines, then sticking each segment into a String ArrayList. It's been working well until I get to the date. Parsing the String date to a Date type results in a ParseException error. Any ideas why I'm not parsing it correctly? Thank you!

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: "Mon Mar 02 13:31:24 CST 2015" at java.text.DateFormat.parse(DateFormat.java:366) at ----.company.Main.addTicketFromFile(Main.java:257) at ----.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;
}

Solution

  • Try

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