英文:
ZonedDateTime from date string in yyyy-mm-dd
问题
尝试从日期字符串(例如 '2020-08-24')解析出一个 ZonedDateTime。
在使用 TemporalAccessor 和 DateTimeFormatter.ISO_OFFSET_DATE 进行解析时,我得到了一个 java.time.format.DateTimeParseException。我是否使用了错误的格式化程序?
甚至尝试在日期字符串末尾添加 'Z',以便将其理解为 UTC。
private ZonedDateTime getZonedDateTime(String dateString) {
TemporalAccessor parsed = null;
dateString = dateString + 'Z';
try {
parsed = DateTimeFormatter.ISO_OFFSET_DATE.parse(dateString);
} catch (Exception e) {
log.error("无法使用格式化程序 DateTimeFormatter.ISO_INSTANT 解析日期 {}", dateString);
}
return ZonedDateTime.from(parsed);
}
英文:
Trying to parse a ZonedDateTime from a date string e.g '2020-08-24'.
Upon using TemporalAccesor, and DateTimeFormatter.ISO_OFFSET_DATE to parse, I am getting a java.time.format.DateTimeParseException. Am I using the wrong formatter?
Even tried adding 'Z' at the end of date string for it to be understood as UTC
private ZonedDateTime getZonedDateTime(String dateString) {
TemporalAccessor parsed = null;
dateString = dateString + 'Z';
try {
parsed = DateTimeFormatter.ISO_OFFSET_DATE.parse(dateString);
} catch (Exception e) {
log.error("Unable to parse date {} using formatter DateTimeFormatter.ISO_INSTANT", dateString);
}
return ZonedDateTime.from(parsed);
}
答案1
得分: 6
## 使用 [`LocalDate#atStartOfDay`][1]
按照以下方式进行操作:
```java
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// 定义一个 DateTimeFormatter
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd");
// 给定日期字符串
String strDate = "2020-08-24";
// 将给定的日期字符串解析为 ZonedDateTime
ZonedDateTime zdt = LocalDate.parse(strDate, dtf).atStartOfDay(ZoneOffset.UTC);
System.out.println(zdt);
}
}
输出结果:
2020-08-24T00:00Z
<details>
<summary>英文:</summary>
## Use [`LocalDate#atStartOfDay`][1]
Do it as follows:
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// Define a DateTimeFormatter
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd");
// Given date string
String strDate = "2020-08-24";
// Parse the given date string to ZonedDateTime
ZonedDateTime zdt = LocalDate.parse(strDate, dtf).atStartOfDay(ZoneOffset.UTC);
System.out.println(zdt);
}
}
**Output:**
2020-08-24T00:00Z
[1]: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#atStartOfDay-java.time.ZoneId-
</details>
# 答案2
**得分**: 1
根据评论区链接的帖子中提到的内容:
> 问题在于 `ZonedDateTime` 需要构建所有的日期和时间字段(年、月、日、时、分、秒、纳秒),但格式化器 ISO_OFFSET_DATE 生成的字符串没有时间部分。
相关解决方案如下:
> 解决方法之一是使用 `DateTimeFormatterBuilder` 进行解析,并为时间字段定义默认值。
<details>
<summary>英文:</summary>
As mentionned in the post linked in comment section :
> The problem is that `ZonedDateTime` needs all the date and time fields to be built (year, month, day, hour, minute, second, nanosecond), but the formatter ISO_OFFSET_DATE produces a string without the time part.
And the related solution
> One alternative to parse it is to use a `DateTimeFormatterBuilder` and define default values for the time fields
</details>
# 答案3
**得分**: 0
在 ISO_OFFSET_DATE 中,偏移量表示时区相对于 UTC 的相对偏移,
因此在您的输入末尾应该有类似于 "+01:00" 的内容。
<details>
<summary>英文:</summary>
offset in ISO_OFFSET_DATE means the timezone is specified as a relative offset against UTC
so something like "+01:00" is expected at the end of your input.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论