【问题标题】:Unable to convert date read from a file : result in java.text.ParseException: Unparseable date无法转换从文件读取的日期:导致 java.text.ParseException: Unparseable date
【发布时间】:2017-01-08 12:00:11
【问题描述】:

我正在尝试将字符串日期转换为对象日期。 如果我使用字符串,它可以完美运行,但是当我从文件中读取字符串日期时,结果是 ko。 在论坛上,我发现原因可能来自区域。还是KO了。

我无法说明为什么 dateformat 会引发异常。 :o(

我的代码的 sn-p :

package main;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;


public class dateTestConv {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    String dateOK1 = new String("21/01/2017");
    convertDateStringEuroToDateObject(dateOK1);

    String dateOK2= "21/01/2017";
    convertDateStringEuroToDateObject(dateOK2);

    //From file =KO
    collectDateFromFile();
}

public static Date convertDateStringEuroToDateObject(String date) {


    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy",Locale.FRANCE);
    Date d = null;
    try {
        dateFormat.setTimeZone(TimeZone.getTimeZone("Europe/Paris"));
        d = dateFormat.parse(date);

    } catch (ParseException ex) {
        Logger.getLogger(dateTestConv.class.getName()).log(Level.SEVERE, null, ex);
    }
    return d;
}






public static void collectDateFromFile(){
    String dirName=new String("dates.txt");
    File file = new File(dirName);
    try {
        FileInputStream fstreami = new FileInputStream(dirName);
        DataInputStream in = new DataInputStream(fstreami);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        while ((strLine = br.readLine()) != null)   {
            String dateFormatNormal = strLine;
            Date d0= convertDateStringEuroToDateObject(dateFormatNormal);
        }
        in.close();
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}




}

文件 dates.txt 只包含一行: 21/01/2017

当调用 collectDateFromFile() 方法时,会引发此异常:

SEVERE: null
java.text.ParseException: Unparseable date: "21/01/2017"
at java.text.DateFormat.parse(DateFormat.java:366)
at         main.dateTestConv.convertDateStringEuroToDateObject(dateTestConv.java:52)
at main.dateTestConv.collectDateFromFile(dateTestConv.java:75)
at main.dateTestConv.main(dateTestConv.java:42)

感谢您的帮助。

【问题讨论】:

  • 注意:按照java类名约定,你的java类dateTestConv应该以大写字母开头。

标签: java date simpledateformat parseexception


【解决方案1】:

简单地说,字符串不是你想象的那样。因此,您需要通过将接收到的值与预期值进行比较来对其进行调试。例如,如果其中隐藏了一个意外的 unicode 代码点,您可能会像这样检测它:

    String dateFormatNormal = strLine;
/* Debug: is there an unexpected unicode codepoint hiding in there? */
    System.out.println("Received: " + dateFormatNormal.codePoints()
            .mapToObj(i -> String.format("0x%04x (%s)", i, String.valueOf(Character.toChars(i))))
            .collect(Collectors.toList()));
    System.out.println("Expected: " + "21/01/2017".codePoints()
            .mapToObj(i -> String.format("0x%04x (%s)", i, String.valueOf(Character.toChars(i))))
            .collect(Collectors.toList()));
/* Remove Debug code when done */

【讨论】:

  • 谢谢!!!我花了几个小时搜索我不知道如何打印十六进制字符。太奇妙了。所以结果是:收到:[0xfeff(), 0x0032(2), 0x0031(1), 0x002f(/), 0x0030(0), 0x0031(1), 0x002f(/), 0x0032(2), 0x0030(0 ), 0x0031 (1), 0x0037 (7)] 预期:[0x0032 (2), 0x0031 (1), 0x002f (/), 0x0030 (0), 0x0031 (1), 0x002f (/), 0x0032 (2), 0x0030 (0), 0x0031 (1), 0x0037 (7)] 问题来自字节顺序标记 (BOM) 是 Unicode 字符。我已将文件转换为 AINSI 文件。它是 utf8 格式的。非常感谢
【解决方案2】:

这可能是由于字符串中的空格。尝试修剪字符串然后解析

【讨论】:

  • 这不是空格,我之前已经打印了每个字符串,因为我使用了 maptoObj 方法,所以它是一样的。谢谢。
猜你喜欢
  • 2018-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-05
相关资源
最近更新 更多