什么模式的datetimeformat需要DateTimeFormatter来解析JAN01/2020?

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

What pattern of datetimeformat is needed for DateTimeFormatter to parse JAN01/2020?

问题

我得到一个日期,格式为 - "JAN01/2020",但我似乎找不到适用于它的 DateTimeFormatter 模式。我尝试了以下这些模式(以及其他几种),但它们都导致了 DateTimeParseException 错误 -

DateTimeFormatter.ofPattern("MMMd/YYYY").parse("JAN01/2020")
DateTimeFormatter.ofPattern("MMMdd/YYYY").parse("JAN01/2020")

我还考虑了这个帖子的解决方案,下面的三行代码也导致了 DateTimeParseException 错误。看起来不是大小写敏感的问题。
https://stackoverflow.com/questions/33520491/java-8-datetimeformatter-for-month-in-all-caps-not-working

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMdd/yyyy");
LocalDateTime dateTime = LocalDateTime.parse("JAN14/2020", formatter);
System.out.println(dateTime.getYear());

我感谢任何建议!

英文:

I'm being passed a date in ths format - "JAN01/2020" but I can't seem to find the DateTimeFormatter pattern for it. I tried these (as well as several others) but they're resulting in DateTimeParseException -

 DateTimeFormatter.ofPattern("MMMd/YYYY").parse("JAN01/2020")
 DateTimeFormatter.ofPattern("MMMdd/YYYY").parse("JAN01/2020")

I also considered this post's solution and the three lines below also result in DateTimeParseException. It does not appear to be a case-sensitivity issue
https://stackoverflow.com/questions/33520491/java-8-datetimeformatter-for-month-in-all-caps-not-working

DateTimeFormatter formatter= DateTimeFormatter.ofPattern("MMMdd/yyyy");
LocalDateTime dateTime = LocalDateTime.parse("JAN14/2020", formatter);
System.out.println(dateTime.getYear());

I appreciate any suggestions!

答案1

得分: 4

# 区分大小写

你的格式主要问题在于你使用了 `YYYY` 而不是 `yyyy`。请按照以下方式进行修改

```java
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;

class Main {
    public static void main(String args[]) {
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                                            .parseCaseInsensitive()
                                            .appendPattern("MMMd/yyyy")
                                            .toFormatter();
        System.out.println(LocalDate.parse("JAN01/2020", formatter));
    }
}

输出:

2020-01-01

使用 DateTimeFormatter::parse 的演示:

import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;

class Main {
    public static void main(String args[]) {
        DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                    .parseCaseInsensitive()
                    .appendPattern("MMMd/yyyy")
                    .toFormatter();
        System.out.println(formatter.parse("JAN01/2020"));
    }
}

输出:

{},ISO resolved to 2020-01-01
英文:

Case-sensitive

The main problem with your format is, you are using YYYY instead of yyyy. Do it as follows:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;

class Main {
	public static void main(String args[]) {
		DateTimeFormatter formatter = new DateTimeFormatterBuilder()
											.parseCaseInsensitive()
											.appendPattern("MMMd/yyyy")
											.toFormatter();
		System.out.println(LocalDate.parse("JAN01/2020", formatter));
	}
}

Output:

2020-01-01

Demo of using DateTimeFormatter::parse with it:

import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;

class Main {
	public static void main(String args[]) {
		DateTimeFormatter formatter = new DateTimeFormatterBuilder()
                .parseCaseInsensitive()
                .appendPattern("MMMd/yyyy")
                .toFormatter();
		System.out.println(formatter.parse("JAN01/2020"));
	}
}

Output:

{},ISO resolved to 2020-01-01

答案2

得分: 4

  • 可以使用DateTimeFormatterBuilder来处理这种情况。

  • 对于月份,可以使用.parseCaseInsensitive(),例如,MMM 对应 Jan,而你有 JAN

  • 对于模式,可以使用.appendPattern("MMMdd/yyyy")

  • 解析后,可以得到一个LocalDate,因为没有时间部分。

DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("MMMdd/yyyy");
DateTimeFormatter dateFormat = builder.toFormatter();

LocalDate dateTime = LocalDate.parse("JAN14/2020", dateFormat);
System.out.println(dateTime); // 2020-01-14

代码演示

英文:

You may

  • handle the case with use of DateTimeFormatterBuilder

  • .parseCaseInsensitive() for the case of the month, MMM is for Jan and you have JAN

  • .appendPattern("MMMdd/yyyy") for the pattern

  • parse into a LocalDate as there is no time composant

<!-- -->

DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern(&quot;MMMdd/yyyy&quot;);
DateTimeFormatter dateFormat = builder.toFormatter();

LocalDate dateTime = LocalDate.parse(&quot;JAN14/2020&quot;, dateFormat);
System.out.println(dateTime); // 2020-01-14

CODE DEMO

答案3

得分: 0

  1. 正如您链接的问题所述,您需要使用DateTimeFormatterBuilder来进行不区分大小写的解析,以解析您的大写月份缩写。

  2. 注意格式模式字符的大小写。yyyyYYYY是不同的。例如,请参阅此问题:Can't parse String to LocalDate (Java 8)

  3. 为您的格式化程序提供区域设置。如果JAN是英语,则提供一个英语区域设置。例如:

         new DateTimeFormatterBuilder()
                 .parseCaseInsensitive()
                 .appendPattern("MMMdd/yyyy")
                 .toFormatter(Locale.ENGLISH)
    
  4. 正如azro所说,由于您没有具体的一天时间,因此解析为LocalDate,而不是LocalDateTime(或者在构建器上指定默认的一天时间,以便解析为LocalDateTime,但我看不出优势)。

英文:
  1. As the question you are linking to says, you need to use a DateTimeFormatterBuilder for case insensitive parsing of your month abbreviation in upper case.

  2. Beware of the case of format pattern letters. yyyy and YYYY are different. See for example this question: Can't parse String to LocalDate (Java 8).

  3. Supply a locale for your formatter. If JAN is English, then supply an English-speaking locale. For example:

        new DateTimeFormatterBuilder()
                .parseCaseInsensitive()
                .appendPattern(&quot;MMMdd/yyyy&quot;)
                .toFormatter(Locale.ENGLISH)
    
  4. As azro said, since you haven’t got a time of day, parse into a LocalDate, not a LocalDateTime (or specify a default hour of day on the builder to be able to parse into a LocalDateTime, but I don’t see the advantage).

huangapple
  • 本文由 发表于 2020年5月29日 04:36:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/62074063.html
匿名

发表评论

匿名网友

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

确定