Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

Unparseable date error on Java

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
", 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...