英文:
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 forJan
and you haveJAN
-
.appendPattern("MMMdd/yyyy")
for the pattern -
parse into a
LocalDate
as there is no time composant
<!-- -->
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
CODE DEMO
答案3
得分: 0
-
正如您链接的问题所述,您需要使用
DateTimeFormatterBuilder
来进行不区分大小写的解析,以解析您的大写月份缩写。 -
注意格式模式字符的大小写。
yyyy
和YYYY
是不同的。例如,请参阅此问题:Can't parse String to LocalDate (Java 8)。 -
为您的格式化程序提供区域设置。如果
JAN
是英语,则提供一个英语区域设置。例如:new DateTimeFormatterBuilder() .parseCaseInsensitive() .appendPattern("MMMdd/yyyy") .toFormatter(Locale.ENGLISH)
-
正如azro所说,由于您没有具体的一天时间,因此解析为
LocalDate
,而不是LocalDateTime
(或者在构建器上指定默认的一天时间,以便解析为LocalDateTime
,但我看不出优势)。
英文:
-
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. -
Beware of the case of format pattern letters.
yyyy
andYYYY
are different. See for example this question: Can't parse String to LocalDate (Java 8). -
Supply a locale for your formatter. If
JAN
is English, then supply an English-speaking locale. For example:new DateTimeFormatterBuilder() .parseCaseInsensitive() .appendPattern("MMMdd/yyyy") .toFormatter(Locale.ENGLISH)
-
As azro said, since you haven’t got a time of day, parse into a
LocalDate
, not aLocalDateTime
(or specify a default hour of day on the builder to be able to parse into aLocalDateTime
, but I don’t see the advantage).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论