- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试将 String
日期的格式从 EEEE MMMM d
更改为 MM/d/yyyy
,首先,转换将其转换为 LocalDate
,然后将不同模式的格式化程序应用于 LocalDate
,然后再次将其解析为 String
。
这是我的代码:
private String convertDate(String stringDate)
{
//from EEEE MMMM d -> MM/dd/yyyy
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(DateTimeFormatter.ofPattern("EEEE MMMM d"))
.toFormatter();
LocalDate parsedDate = LocalDate.parse(stringDate, formatter);
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM/d/yyyy");
String formattedStringDate = parsedDate.format(formatter2);
return formattedStringDate;
}
但是,我收到了一条我不太理解的异常消息:
Exception in thread "main" java.time.format.DateTimeParseException: Text 'TUESDAY JULY 25' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {DayOfWeek=2, MonthOfYear=7, DayOfMonth=25},ISO of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
最佳答案
正如其他答案所说,要创建 LocalDate
,您需要 year,它不在输入 String
中。它只有日、月和星期几。
要获得完整的LocalDate
,您需要解析日和月并在中找到年此日/月组合与星期几匹配。
当然,您可以忽略星期几,并假设日期始终是当前的年;在这种情况下,其他答案已经提供了解决方案。但是如果您想找到与星期几完全匹配的年,您必须循环直到找到它。
我还创建了一个带有 java.util.Locale
的格式化程序,以明确表示我想要月份和星期几 英文名字。如果您不指定区域设置,它将使用系统的默认设置,并且不能保证始终是英语(并且可以在不通知的情况下更改,即使在运行时也是如此)。
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(DateTimeFormatter.ofPattern("EEEE MMMM d"))
// use English Locale to correctly parse month and day of week
.toFormatter(Locale.ENGLISH);
// parse input
TemporalAccessor parsed = formatter.parse("TUESDAY JULY 25");
// get month and day
MonthDay md = MonthDay.from(parsed);
// get day of week
DayOfWeek dow = DayOfWeek.from(parsed);
LocalDate date;
// start with some arbitrary year, stop at some arbitrary value
for(int year = 2017; year > 1970; year--) {
// get day and month at the year
date = md.atYear(year);
// check if the day of week is the same
if (date.getDayOfWeek() == dow) {
// found: 'date' is the correct LocalDate
break;
}
}
在此示例中,我从 2017 年开始,并试图找到一个可以追溯到 1970 年的日期,但您可以调整适合您的用例的值。
您还可以使用 Year.now().getValue()
获取当前年份(而不是某个固定的任意值)。
关于Java:无法从 TemporalAccessor 获取 LocalDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45320971/
我收到一个未知时间(ISO 格式的日期、时间或时间戳)的请求参数,并希望将其解析为 java.time.temporal.TemporalAccessor: LocalDate 当字符串表示像 "20
这个问题在这里已经有了答案: JDK8: unable to parse LocalTime (4 个回答) DateTimeParseException: Text could not be par
我有一个以下格式的日期,我需要解析它并转换为纪元时间。 2018-11-08 08:17:18.696124 我有以下代码。 String dateString = "2018-11-08 08:17
我试图将一些日期字符串解析为日期值,但是,使用下面的代码,我得到了一个异常: DateTimeFormatter formatter = DateTimeFormatter.ofPattern("Q'
我正在尝试将 String 日期的格式从 EEEE MMMM d 更改为 MM/d/yyyy,首先,转换将其转换为 LocalDate,然后将不同模式的格式化程序应用于 LocalDate,然后再次将
使用 Java 1.8.0_51 以下代码(取自 Unable to obtain OffsetDateTime from TemporalAccessor) DateTimeFormatter fo
我正在尝试将格式为“YYYYww”(例如 201901)的简单字符串解析为 LocalDate,但我的尝试都没有成功。 我试图通过简单地使用模式“YYYYww”以及通过手动将值附加到 Formatte
当我这样做的时候 String datum = "20130419233512"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
我编写了一段简单的代码来使用 java 8 api 解析日期。我还解决了有关此主题的各种其他堆栈溢出问题,但未能解决错误。 package com.test.java8api; import java
我有一个特定的类可以解析(相当)多种日期: // imports omitted for brevity public final class DateParser { private sta
我编写了一段简单的代码来使用 java 8 api 解析日期。我还解决了有关此主题的各种其他堆栈溢出问题,但未能解决错误。 package com.test.java8api; import java
我需要将标准的长 System.currentmillis 转换为临时访问器,但不知道如何开始。 最佳答案 Instant是一个 TemporalAccessor,因此您可以从纪元以来的毫秒数创建一个
这个问题已经有答案了: Unable to obtain OffsetDateTime from TemporalAccessor (3 个回答) Unable to obtain LocalDate
我尝试使用 "yyyyMMddHHmmssZ" 格式解析 "20140726080320+0400",如下所示: System.out.println("********************" +
给定一个 DateTimeFormatter 定义为: public static final DateTimeFormatter DATE_TIME = new DateTimeFormatterB
我在我的 Maven 项目中使用 Threetenbp 版本 1.4.4。在 tomcat 容器中部署 JAR 时,出现以下异常: java.lang.NoClassDefFoundError: "o
这是我的代码: private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd HH:mm:ss");
我正在使用下面的代码来解析日期字符串: String time = "2 Jun 2019 03:51:17 PM ACST"; String pattern = "d MMM yyy
LocalDateTime.parse("2017-02-02 08:59:12", DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss")); 它打印错
解析 DateTime 时出现异常。我在这里缺少什么 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("0DDDHHmmss");
我是一名优秀的程序员,十分优秀!