Java上无法解析的日期错误 [英] Unparseable date error on Java

查看:84
本文介绍了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\n", d, d2n.get(d));
        }
    }
}

嗨。我得到


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)

错误,我的txt文件输入以下内容:

error, my txt file has following inputs:


7
Randy 1996年6月7日

Omar 1999年2月20日

1990年9月14日星期二

克里斯1990年9月14日

亚当1996年2月20日

吉姆9月14日,1990

菲利普,1994年10月27日

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

如何解决错误?

推荐答案

您正在使用Java 8计算机。对于Java 8,Oralce提供了可用于日期和时间的新API。请使用以下API替换当前的API(第28行),以使其正常工作。

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

这篇关于Java上无法解析的日期错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆