Unparseable date error on Java

2019-09-20 14:10发布

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class L10C
{

    public static void main(String[] args) throws Exception
    {
        File f = new File("src/Birthdates.txt");
        Scanner input = new Scanner(f);

        //-------------------------------------------------Read File & Create N2D Map
        Map<String, Date> n2d = new TreeMap<String, Date>();
        int n = input.nextInt();
        for (int r = 0; r < n; r++)
        {
            // String record = input.nextLine();
            // parse record into two pieces

            String name = input.next();
            String birthdateString = input.nextLine();
            SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");
            Date birthdate = sdf.parse(birthdateString.trim());
            //System.out.println(name+" - " + birthdate);
            n2d.put(name, birthdate);
        }
        System.out.println("N2D: " + n2d); // debugging output

        //-------------------------------------------------Read File & Create D2N Map
        Map<Date, String> d2n = new TreeMap<Date, String>();
        for (String s : n2d.keySet())
        {
            Date d = n2d.get(s);
            if (!d2n.containsKey(d))
            {
                d2n.put(d, s);
            }
            else
            {
                String existingName = d2n.get(d);
                if (s.compareTo(existingName) == -1) // means s < existingName
                {
                    d2n.put(d, s);
                }
            }
        }
        System.out.println("D2N: " + d2n); // debugging outpu

        //-------------------------------------------------Output D2N Formatted
        for (Date d : d2n.keySet())
        {
            System.out.printf("%tb %<td, %<tY --> %s\n", d, d2n.get(d));
        }
    }
}

Hi. I get

Exception in thread "main" java.text.ParseException: Unparseable date: "Jun 7, 1996"
    at java.text.DateFormat.parse(DateFormat.java:366)
    at L10C.main(L10C.java:27)

error, my txt file has following inputs:

7 Randy Jun 7, 1996
Omar Feb 20, 1999
Sue Sep 14, 1990
Chris Sep 14, 1990
Adam Feb 20, 1996
Jim Sep 14, 1990
Phillip Oct 27, 1994

How can I fix error?

3条回答
太酷不给撩
2楼-- · 2019-09-20 14:51

I used the following format to change the string into date

String dateInString = "Sun Mar 06 11:28:16 IST 2011";
        DateFormat df = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
        Date startDate = (Date) df.parse(dateInString);

It worked fine for me.

查看更多
3楼-- · 2019-09-20 14:58

You are working on a Java 8 machine. For Java 8, Oralce offers new API to work with date and time. Please replace your current API (line 28) with the below API to make it works.

String str = "1986-04-08 12:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
查看更多
男人必须洒脱
4楼-- · 2019-09-20 14:59

The exception is because the format expected is dd but your file entry contains otherwise. In this particular case the parse call would have worked if the entry were as below: 7 Randy Jun 07, 1996 Notice the 0 before 7 to make this a proper dd format.

Reg solution, I think you can catch the parse exception and if you are sure that this is the only case that may appear in your file which is unparsable, you can correct the string in the catch block and reparse.

查看更多
登录 后发表回答