- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试将字符串 FEBRUARY 2019
解析为 LocalDate
。
这是我的方法:
LocalDate month = LocalDate.parse("FEBRUARY 2019", DateTimeFormatter.ofPattern("MMMM yyyy"));
或者设置Locale.US
:
LocalDate month = LocalDate.parse("FEBRUARY 2019", DateTimeFormatter.ofPattern("MMMM yyyy", Locale.US));
但我得到的只是以下异常:
Exception in thread "main" java.time.format.DateTimeParseException: Text 'FEBRUARY 2019' could not be parsed at index 0
最佳答案
首先,我建议您输入的不是日期 - 它是年和月。因此,解析为 YearMonth
,然后根据需要从中创建一个 LocalDate
。我发现最简单的做法是让文本处理代码仅处理文本处理,并在您已经在日期/时间域中时单独执行任何其他转换。
要处理区分大小写的问题,您可以创建一个不区分大小写解析的 DateTimeFormatter
。这是一个完整的例子:
import java.time.*;
import java.time.format.*;
import java.util.*;
public class Test {
public static void main(String[] args) {
// Note: this would probably be a field somewhere so you don't need
// to build it every time.
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("MMMM yyyy")
.toFormatter(Locale.US);
YearMonth month = YearMonth.parse("FEBRUARY 2019", formatter);
System.out.println(month);
}
}
如果您有不同的表示,作为一种可能有用的替代方法,您可以构建一个 map 并将其传递给 DateTimeFormatterBuilder.appendText
。 (我只是在以某种方式弄乱代码时才发现这一点。)
import java.time.*;
import java.time.format.*;
import java.time.temporal.*;
import java.util.*;
public class Test {
public static void main(String[] args) {
// TODO: Build this map up programmatically instead?
Map<Long, String> monthNames = new HashMap<>();
monthNames.put(1L, "JANUARY");
monthNames.put(2L, "FEBRUARY");
monthNames.put(3L, "MARCH");
monthNames.put(4L, "APRIL");
monthNames.put(5L, "MAY");
monthNames.put(6L, "JUNE");
monthNames.put(7L, "JULY");
monthNames.put(8L, "AUGUST");
monthNames.put(9L, "SEPTEMBER");
monthNames.put(10L, "OCTOBER");
monthNames.put(11L, "NOVEMBER");
monthNames.put(12L, "DECEMBER");
// Note: this would probably be a field somewhere so you don't need
// to build it every time.
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendText(ChronoField.MONTH_OF_YEAR, monthNames)
.appendLiteral(' ')
.appendPattern("yyyy")
.toFormatter(Locale.US);
YearMonth month = YearMonth.parse("FEBRUARY 2019", formatter);
System.out.println(month);
}
}
关于java - 将大写日期解析为 LocalDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54590045/
我正在尝试计算 2 个 localDates 之间的天数 我从这个答案中获得灵感:https://stackoverflow.com/a/325964对于这个问题Determine Whether T
我在使用 Joda daysBetween 函数时遇到问题。它一直在告诉我 The method daysBetween(ReadableInstant, ReadableInstant) in th
初始问题: 在Scala中,我想使用隐式 Ordering[T]#Ops比较两个LocalDate . 它只是使用像 > 这样的“运算符”而不是isAfter . 它应该只是一个导入:import s
如何删除 localDate 中的 T? 我需要删除“T”以匹配数据库中的数据。 这是我的代码 DateTimeFormatter formatter = DateTimeFormatter.ofPa
这个问题已经有答案了: How to format LocalDate object to MM/dd/yyyy and have format persist (4 个回答) How can you
这个问题在这里已经有了答案: What is the difference between public, protected, package-private and private in Jav
这个问题已经有答案了: SimpleDateFormat ignoring month when parsing (4 个回答) Java date format conversion - getti
这个问题已经有答案了: SimpleDateFormat ignoring month when parsing (4 个回答) Java date format conversion - getti
这个问题在这里已经有了答案: Localdate.format, format is not applied (2 个答案) How to format LocalDate to string? (
这个问题在这里已经有了答案: Importing two classes with same name. How to handle? (11 个回答) 7年前关闭。 我正在练习如何处理日期。但是当我
根据 Joda 中的文档: public LocalDate(int year, int monthOfYear, int dayOfMonth, Chronology chron
我正在尝试开发一个独立于 GWT 的通用模块,直到它实际在 GWT 应用程序中使用为止。问题是 GWT 不支持 joda 并且我的模块不使用 GWT。 我想要实现的目标(这行不通): 最
问题:Spring 似乎对 LocalDate 使用不同的反序列化方法,具体取决于它是出现在 @RequestBody 还是请求 @ReqestParam -这是正确的吗?如果是这样,是否有办法将它们
我想使用 Java8 DateTime API 中的 LocalDate 和 LocalDateTime,并且在值为 null 时无法坚持到 postgresql date 和 timestamp。
我正在做以下编程练习:Unlucky Days 。声明如下: Friday 13th or Black Friday is considered as unlucky day. Calculate h
这个问题已经有答案了: Java 8 LocalDateTime is parsing invalid datetime (5 个回答) How to sanity check a date in J
我有一个包含以下字段的员工类。 class Employee { final int id; final String name; final LocalDate update
这个问题已经有答案了: If statement gives condition is always true (4 个回答) 已关闭 3 年前。 我试图将两个日期与日期列表进行比较(列表元素已被删除
被这个问题困扰了一段时间,希望得到一些意见。我想验证用户输入的日期,以便我可以使用 LocalDate 对象执行计算。但是,当输入有效日期时,返回的日期是前一个无效日期,这会引发异常并崩溃。我错过了什
这个问题已经有答案了: How can I parse/format dates with LocalDateTime? (Java 8) (11 个回答) 已关闭 3 年前。 我有一个日期时间信息字
我是一名优秀的程序员,十分优秀!