-1
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?

Ulfalizer
  • 4,664
  • 1
  • 21
  • 30
online.0227
  • 640
  • 4
  • 15
  • 29
  • 1
    It's worked for me, you error doesn't occur on my machine. What are your java version and OS that you are running the example ? – Tien Nguyen Mar 29 '15 at 04:41
  • Tien Nguyen > Ubuntu Linux 14.04 LTS, jdk1.8.0_31 (64bit i think). it is strange.. becuz it worked on my school's PC but not working on my PC. What's your jdk version? I think i need to replace.. – online.0227 Mar 29 '15 at 04:45
  • I'm running Java 1.7.0_65 and Ubuntu so I added my answer below, hopefully it works for you. – Tien Nguyen Mar 29 '15 at 04:58

3 Answers3

1

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);
Tien Nguyen
  • 4,298
  • 9
  • 30
  • 44
0

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.

sujit
  • 2,258
  • 1
  • 15
  • 24
  • Sorry, I changed to Randy Jun 07, 1996 as you said, but it still doesn't work.. same error as following: Exception in thread "main" java.text.ParseException: Unparseable date: "Jun 07, 1996" at java.text.DateFormat.parse(DateFormat.java:366) at L10C.main(L10C.java:27) – online.0227 Mar 29 '15 at 04:51
0

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.