数据解析时抛出的异常

huangapple go评论60阅读模式
英文:

Exception thrown when parsing data

问题

抱歉,这是一个初学者的问题,但我遇到了以下问题:每次我尝试将一个字符串解析为LocalDate类型,使用特定格式(ddMMyyy)时,我都会收到以下消息:

Exception in thread "main" java.time.format.DateTimeParseException: Text '06071994' could not be parsed at index 2
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
    at java.base/java.time.LocalDate.parse(LocalDate.java:428)
    at jujj.main(jujj.java:7)

Process finished with exit code 1

起初,我以为可能是代码的其他部分出了问题,所以我尝试隔离只进行解析的部分进行测试,但没有成功。这是测试代码:

String in = "06071994";
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd MM yyyy");
LocalDate BirthDay = LocalDate.parse(in, format);
System.out.println(in);

稍后编辑:我尝试了不同的格式:"dd/MM/yyyy","dd-MM-yyyy","ddMMyyyy",它们仍然不起作用。

英文:

Sorry if this is a rookie question, but I have the following problem: every time I try to parse a string into a LocalDate type, with a specific format (ddMMyyy) I get the following message:

Exception in thread "main" java.time.format.DateTimeParseException: Text '06071994' could not be parsed at index 2
	at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
	at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
	at java.base/java.time.LocalDate.parse(LocalDate.java:428)
	at jujj.main(jujj.java:7)

Process finished with exit code 1

At first I thought that maybe I did something wrong in a different part of the code and I tried to isolate just the part where I'm doing the parsing to test it, but no luck. This is the test code:

String in = "06071994";
DateTimeFormatter format = DateTimeFormatter.ofPattern ( "dd MM yyyy" );
LocalDate BirthDay = LocalDate.parse ( in, format );
System.out.println ( in );

Later Edit: I tried different formats: "dd/MM/yyyy" , "dd-MM-yyyy", "ddMMyyyy", they still didn't work.

答案1

得分: 2

你的模式与字符串不匹配。

你的字符串不包含空格,而你的模式包含空格。

你的字符串包含一个两位数的月份,而你的模式期望的是月份名称的三个字母缩写。

尝试以下代码:

String in = "06071994";
DateTimeFormatter format = DateTimeFormatter.ofPattern("ddMMyyyy");
LocalDate BirthDay = LocalDate.parse(in, format);
System.out.println(BirthDay);

所有有效的模式都在 DateTimeFormatter 类的 javadoc 中详细解释。

英文:

Clearly, your pattern does not match your string.

Your string contains no spaces while your pattern does.

Your string contains a two digit month while your pattern is expecting a three letter abbreviation of the month name.

Try the following code:

String in = "06071994";
DateTimeFormatter format = DateTimeFormatter.ofPattern ( "ddMMyyyy" );
LocalDate BirthDay = LocalDate.parse ( in, format );
System.out.println ( BirthDay );

All the valid patterns are detailed and explained in the javadoc of class DateTimeFormatter

答案2

得分: 0

import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class FormatDate {

    public static void main(String... args) throws ParseException {
        String in = "06071994";
        DateTimeFormatter format = DateTimeFormatter.ofPattern("ddMMyyyy");
        LocalDate BirthDay = LocalDate.parse(in, format);
        System.out.println(BirthDay);
    }
}

Edit 1: 代码的问题在于输入的格式为ddMMyyyy(06071994),而格式应为ddMMyyyy(不应该有空格)。因此,现在解析器看到输入和要解析的格式不一样,因此会抛出错误。

英文:
import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class FormatDate {

public static void main(String... args) throws ParseException {
	String in = "06071994";
	DateTimeFormatter format = DateTimeFormatter.ofPattern("ddMMyyyy");
	LocalDate BirthDay = LocalDate.parse(in, format);
	System.out.println(BirthDay);
  }
}

Edit 1: Problem with the code was that the input was in the format ddMMyyyy(06071994) and the format was dd MM yyyy (should have been ddMMyyyy). So now parser sees that input and format to parse are not same hence it throws error.

huangapple
  • 本文由 发表于 2020年7月30日 18:59:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63171727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定