LocalDateTime 无法解析 "June"
LocalDateTime unable to parse "June"
有人帮助我设置了一个函数来获取两个字符串并将它们合并到一个日期对象中。
这一直工作正常,直到它尝试解析 6 月 1 日,然后我得到以下错误
Exception in thread "AWT-EventQueue-0" java.time.format.DateTimeParseException: Text '1st June' could not be parsed, unparsed text found at index 7
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1952)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDate.parse(LocalDate.java:400)
at Timetable.ClassManager.parseDate(ClassManager.java:201)
at Timetable.GoogleAPI.loadClasses(GoogleAPI.java:133)
at Timetable.ClassManager.loadClasses(ClassManager.java:58)
函数的代码是
public LocalDateTime parseDate(String strDate, String strTime) {
strTime = strTime + ":00";
System.out.println("Date: " + strDate);
System.out.println("Time: " + strTime);
LocalDate date = LocalDate.now();
DateTimeFormatter dtfForDate = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.parseDefaulting(ChronoField.YEAR, date.getYear())
.appendPattern("d['th']['st']['rd']['nd'] MMM")
.toFormatter(Locale.ENGLISH);
DateTimeFormatter dtfForTime = DateTimeFormatter.ofPattern("H:m:s", Locale.ENGLISH);
LocalDateTime ldt = LocalDate.parse(strDate, dtfForDate)
.atTime(LocalTime.parse(strTime, dtfForTime));
System.out.println("Local Date Time: " + ldt);
return ldt;
}
两个打印给我
Date: 1st June
Time: 9:15:00
我需要能够处理完整的月份名称和月份缩写,即,March 设置为 Mar,April 设置为 Apr。
使用以下模式,这将同时满足三个字母的缩写月份名称和完整月份名称:
.appendPattern("d['th']['st']['rd']['nd'] [MMMM][MMM]")
演示:
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
System.out.println(parseDate("1st June", "9:15:00"));
System.out.println(parseDate("1st Jun", "9:15:00"));
}
public static LocalDateTime parseDate(String strDate, String strTime) {
LocalDate date = LocalDate.now();
DateTimeFormatter dtfForDate = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.parseDefaulting(ChronoField.YEAR, date.getYear())
.appendPattern("d['th']['st']['rd']['nd'] [MMMM][MMM]")
.toFormatter(Locale.ENGLISH);
DateTimeFormatter dtfForTime = DateTimeFormatter.ofPattern("H:m:s", Locale.ENGLISH);
LocalDateTime ldt = LocalDate.parse(strDate, dtfForDate).atTime(LocalTime.parse(strTime, dtfForTime));
return ldt;
}
}
输出:
2021-06-01T09:15
2021-06-01T09:15
更新
此更新解决了 Meno Hochschild 提出的以下问题:
Personally, I don't like the misuse of optional sections here.
Negative example: "1th Jun" or "2st Jun" would also be successfully
parsed but should not.
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
public class Main {
public static void main(String[] args) {
System.out.println(parseDate("1st June", "9:15:00"));
System.out.println(parseDate("1st Jun", "9:15:00"));
}
public static LocalDateTime parseDate(String strDate, String strTime) {
LocalDate date = LocalDate.now();
DateTimeFormatter dtfForDate = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.parseDefaulting(ChronoField.YEAR, date.getYear())
.appendText(ChronoField.DAY_OF_MONTH, ordinalMap())
.appendPattern(" [MMMM][MMM]")
.toFormatter(Locale.ENGLISH);
DateTimeFormatter dtfForTime = DateTimeFormatter.ofPattern("H:m:s", Locale.ENGLISH);
LocalDateTime ldt = LocalDate.parse(strDate, dtfForDate).atTime(LocalTime.parse(strTime, dtfForTime));
return ldt;
}
static Map<Long, String> ordinalMap() {
String[] suffix = { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" };
Map<Long, String> map = new HashMap<>();
for (int i = 1; i <= 31; i++)
map.put((long)i, String.valueOf(i) + suffix[(i > 3 && i < 21) ? 0 : (i % 10)]);
return map;
}
}
输出:
2021-06-01T09:15
2021-06-01T09:15
礼貌: 构建 Map
的逻辑基于 this excellent answer.
有人帮助我设置了一个函数来获取两个字符串并将它们合并到一个日期对象中。
这一直工作正常,直到它尝试解析 6 月 1 日,然后我得到以下错误
Exception in thread "AWT-EventQueue-0" java.time.format.DateTimeParseException: Text '1st June' could not be parsed, unparsed text found at index 7
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1952)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDate.parse(LocalDate.java:400)
at Timetable.ClassManager.parseDate(ClassManager.java:201)
at Timetable.GoogleAPI.loadClasses(GoogleAPI.java:133)
at Timetable.ClassManager.loadClasses(ClassManager.java:58)
函数的代码是
public LocalDateTime parseDate(String strDate, String strTime) {
strTime = strTime + ":00";
System.out.println("Date: " + strDate);
System.out.println("Time: " + strTime);
LocalDate date = LocalDate.now();
DateTimeFormatter dtfForDate = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.parseDefaulting(ChronoField.YEAR, date.getYear())
.appendPattern("d['th']['st']['rd']['nd'] MMM")
.toFormatter(Locale.ENGLISH);
DateTimeFormatter dtfForTime = DateTimeFormatter.ofPattern("H:m:s", Locale.ENGLISH);
LocalDateTime ldt = LocalDate.parse(strDate, dtfForDate)
.atTime(LocalTime.parse(strTime, dtfForTime));
System.out.println("Local Date Time: " + ldt);
return ldt;
}
两个打印给我
Date: 1st June
Time: 9:15:00
我需要能够处理完整的月份名称和月份缩写,即,March 设置为 Mar,April 设置为 Apr。
使用以下模式,这将同时满足三个字母的缩写月份名称和完整月份名称:
.appendPattern("d['th']['st']['rd']['nd'] [MMMM][MMM]")
演示:
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
System.out.println(parseDate("1st June", "9:15:00"));
System.out.println(parseDate("1st Jun", "9:15:00"));
}
public static LocalDateTime parseDate(String strDate, String strTime) {
LocalDate date = LocalDate.now();
DateTimeFormatter dtfForDate = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.parseDefaulting(ChronoField.YEAR, date.getYear())
.appendPattern("d['th']['st']['rd']['nd'] [MMMM][MMM]")
.toFormatter(Locale.ENGLISH);
DateTimeFormatter dtfForTime = DateTimeFormatter.ofPattern("H:m:s", Locale.ENGLISH);
LocalDateTime ldt = LocalDate.parse(strDate, dtfForDate).atTime(LocalTime.parse(strTime, dtfForTime));
return ldt;
}
}
输出:
2021-06-01T09:15
2021-06-01T09:15
更新
此更新解决了 Meno Hochschild 提出的以下问题:
Personally, I don't like the misuse of optional sections here. Negative example: "1th Jun" or "2st Jun" would also be successfully parsed but should not.
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
public class Main {
public static void main(String[] args) {
System.out.println(parseDate("1st June", "9:15:00"));
System.out.println(parseDate("1st Jun", "9:15:00"));
}
public static LocalDateTime parseDate(String strDate, String strTime) {
LocalDate date = LocalDate.now();
DateTimeFormatter dtfForDate = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.parseDefaulting(ChronoField.YEAR, date.getYear())
.appendText(ChronoField.DAY_OF_MONTH, ordinalMap())
.appendPattern(" [MMMM][MMM]")
.toFormatter(Locale.ENGLISH);
DateTimeFormatter dtfForTime = DateTimeFormatter.ofPattern("H:m:s", Locale.ENGLISH);
LocalDateTime ldt = LocalDate.parse(strDate, dtfForDate).atTime(LocalTime.parse(strTime, dtfForTime));
return ldt;
}
static Map<Long, String> ordinalMap() {
String[] suffix = { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" };
Map<Long, String> map = new HashMap<>();
for (int i = 1; i <= 31; i++)
map.put((long)i, String.valueOf(i) + suffix[(i > 3 && i < 21) ? 0 : (i % 10)]);
return map;
}
}
输出:
2021-06-01T09:15
2021-06-01T09:15
礼貌: 构建 Map
的逻辑基于 this excellent answer.