本文介绍了DateTimeFormatter无法解析日期字符串,但是SimpleDateFormat能够的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用LocalDate解析方法来解析此示例日期字符串-表示"2015年1月3日"的"312015".可以请人帮忙.

I am unable to parse this sample date string - "312015" representing "3rd Jan 2015" using LocalDate parse method. Could someone please assist.

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class TestDOB {

    public static void main(String[] args) throws ParseException {
        // TODO Auto-generated method stub

        String dateOfBirth = "312015";
        SimpleDateFormat sdf = new SimpleDateFormat("dMyyyy");
        System.out.println(sdf.parse(dateOfBirth));
        // The above outputs Sat Jan 03 00:00:00 CST 2015

        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dMyyyy").withLocale(Locale.getDefault());
        LocalDate dateTime = LocalDate.parse(dateOfBirth, dateFormatter);

        // The above parsing statement with LocalDate parse method runs into below error -


    }

}

Error on console-

Exception in thread "main" java.time.format.DateTimeParseException: Text '312015' could not be parsed at index 6
    at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
    at java.time.format.DateTimeFormatter.parse(Unknown Source)
    at java.time.LocalDate.parse(Unknown Source)
    at TestDOB.main(TestDOB.java:30)

推荐答案

DateTimeFormatterBuilder.appendValue(TemporalField,int)

我很惊讶地发现这是可能的(对我来说这没有多大意义).

DateTimeFormatterBuilder.appendValue(TemporalField, int)

I was surprised to find out that this is possible (it doesn’t make much sense to me).

    DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
            .appendValue(ChronoField.DAY_OF_MONTH, 1)
            .appendValue(ChronoField.MONTH_OF_YEAR, 1)
            .appendValue(ChronoField.YEAR, 4)
            .toFormatter();

    String dateOfBirth = "312015";
    LocalDate dateTime = LocalDate.parse(dateOfBirth, dateFormatter);
    System.out.println(dateTime);

此代码段的输出为:

它有一些局限性,我认为这很严重:它只能解析1月至9月(非10月至12月)的月份1–9天(非10–31)之内的日期.坚持每月的月份和月份分别只有1位数.

It has some limitations that I would consider quite serious, though: It can only parse dates that are within days 1–9 of the month (not 10–31) in months January through September (not October–December) because it insists on day of month and month being only 1 digit each.

您可以通过在第一次调用 appendValue()时省去第二个参数 1 来消除月份某天的限制.代码>.类似的技巧在这个月内 无效,因此一年的最后一个季度遥不可及.

You may get rid of the limitation on day of month by leaving out the second argument, 1, from the first call to appendValue(). A similar trick will not work for the month, so the last quarter of the year is out of reach.

对于 DateTimeFormatter ,数字字段的一个模式字母被认为是尽可能多的位数"(而不是预期的"1位数").它没有很好的记录.文档中的这句话有一个提示:

To DateTimeFormatter one pattern letter for a numeric field is taken to mean "as many digits as it takes" (not "1 digit" as might have been expected). It’s not very well documented. There’s a hint in this sentence from the documentation:

发生的事情是,解析在一个月的一天中会吃掉尽可能多的数字(内部上限为19).在这种情况下,整个字符串.现在它无法解析任何月份.这是错误消息的原因:

What happens is that parsing eats as many digits as it can for day of month (up to an internal limit of 19). In this case the entire string. Now it is unable to parse any month. This is the reason for the error message:

索引6是字符串的结尾.

Index 6 is the end of the string.

DateTimeFormatter

这篇关于DateTimeFormatter无法解析日期字符串,但是SimpleDateFormat能够的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-20 14:58