Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法转换从文件读取的日期:结果为java.text.ParseException:不可解析的日期_Java_Date_Simpledateformat_Parseexception - Fatal编程技术网

无法转换从文件读取的日期:结果为java.text.ParseException:不可解析的日期

无法转换从文件读取的日期:结果为java.text.ParseException:不可解析的日期,java,date,simpledateformat,parseexception,Java,Date,Simpledateformat,Parseexception,我正在尝试将字符串日期转换为对象日期。 如果我使用一个字符串,它工作得很好,但是当我从文件中读取字符串日期时,结果是ko。 在论坛上,我发现原因可能来自区域。它仍然是高 我说不出为什么dateformat会出现异常o( 我的代码片段: package main; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; i

我正在尝试将字符串日期转换为对象日期。 如果我使用一个字符串,它工作得很好,但是当我从文件中读取字符串日期时,结果是ko。 在论坛上,我发现原因可能来自区域。它仍然是高

我说不出为什么dateformat会出现异常o(

我的代码片段:

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只包含一行: 2017年1月21日

调用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)

感谢您的帮助。

这可能是因为字符串中有空格。请尝试对字符串进行修剪,然后进行分析。

简单地说,字符串不是您认为的字符串。因此,您需要通过将收到的字符串与预期值进行比较来对其进行调试。例如,如果其中隐藏了意外的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 */

注意:按照java类名约定,您的java类
dateTestConv
应该以大写字母开头。谢谢!!!我花了几个小时搜索,不知道如何打印十六进制字符。这太神奇了。结果是:收到:[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格式。Big thanksIt没有空格,我以前打印过每个字符串,因为我使用maptoObj方法。谢谢。