ZonedDateTime从日期字符串中获取,格式为yyyy-mm-dd。

huangapple go评论89阅读模式
英文:

ZonedDateTime from date string in yyyy-mm-dd

问题

尝试从日期字符串(例如 '2020-08-24')解析出一个 ZonedDateTime。

在使用 TemporalAccessor 和 DateTimeFormatter.ISO_OFFSET_DATE 进行解析时,我得到了一个 java.time.format.DateTimeParseException。我是否使用了错误的格式化程序?

甚至尝试在日期字符串末尾添加 'Z',以便将其理解为 UTC。

  1. private ZonedDateTime getZonedDateTime(String dateString) {
  2. TemporalAccessor parsed = null;
  3. dateString = dateString + 'Z';
  4. try {
  5. parsed = DateTimeFormatter.ISO_OFFSET_DATE.parse(dateString);
  6. } catch (Exception e) {
  7. log.error("无法使用格式化程序 DateTimeFormatter.ISO_INSTANT 解析日期 {}", dateString);
  8. }
  9. return ZonedDateTime.from(parsed);
  10. }
英文:

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

  1. private ZonedDateTime getZonedDateTime(String dateString) {
  2. TemporalAccessor parsed = null;
  3. dateString = dateString + 'Z';
  4. try {
  5. parsed = DateTimeFormatter.ISO_OFFSET_DATE.parse(dateString);
  6. } catch (Exception e) {
  7. log.error("Unable to parse date {} using formatter DateTimeFormatter.ISO_INSTANT", dateString);
  8. }
  9. return ZonedDateTime.from(parsed);
  10. }

答案1

得分: 6

  1. ## 使用 [`LocalDate#atStartOfDay`][1]
  2. 按照以下方式进行操作
  3. ```java
  4. import java.time.LocalDate;
  5. import java.time.ZoneOffset;
  6. import java.time.ZonedDateTime;
  7. import java.time.format.DateTimeFormatter;
  8. public class Main {
  9. public static void main(String[] args) {
  10. // 定义一个 DateTimeFormatter
  11. DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd");
  12. // 给定日期字符串
  13. String strDate = "2020-08-24";
  14. // 将给定的日期字符串解析为 ZonedDateTime
  15. ZonedDateTime zdt = LocalDate.parse(strDate, dtf).atStartOfDay(ZoneOffset.UTC);
  16. System.out.println(zdt);
  17. }
  18. }

输出结果:

  1. 2020-08-24T00:00Z
  1. <details>
  2. <summary>英文:</summary>
  3. ## Use [`LocalDate#atStartOfDay`][1]
  4. Do it as follows:
  5. import java.time.LocalDate;
  6. import java.time.ZoneOffset;
  7. import java.time.ZonedDateTime;
  8. import java.time.format.DateTimeFormatter;
  9. public class Main {
  10. public static void main(String[] args) {
  11. // Define a DateTimeFormatter
  12. DateTimeFormatter dtf = DateTimeFormatter.ofPattern(&quot;uuuu-MM-dd&quot;);
  13. // Given date string
  14. String strDate = &quot;2020-08-24&quot;;
  15. // Parse the given date string to ZonedDateTime
  16. ZonedDateTime zdt = LocalDate.parse(strDate, dtf).atStartOfDay(ZoneOffset.UTC);
  17. System.out.println(zdt);
  18. }
  19. }
  20. **Output:**
  21. 2020-08-24T00:00Z
  22. [1]: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#atStartOfDay-java.time.ZoneId-
  23. </details>
  24. # 答案2
  25. **得分**: 1
  26. 根据评论区链接的帖子中提到的内容:
  27. &gt; 问题在于 `ZonedDateTime` 需要构建所有的日期和时间字段(年、月、日、时、分、秒、纳秒),但格式化器 ISO_OFFSET_DATE 生成的字符串没有时间部分。
  28. 相关解决方案如下:
  29. &gt; 解决方法之一是使用 `DateTimeFormatterBuilder` 进行解析,并为时间字段定义默认值。
  30. <details>
  31. <summary>英文:</summary>
  32. As mentionned in the post linked in comment section :
  33. &gt; 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.
  34. And the related solution
  35. &gt; One alternative to parse it is to use a `DateTimeFormatterBuilder` and define default values for the time fields
  36. </details>
  37. # 答案3
  38. **得分**: 0
  39. 在 ISO_OFFSET_DATE 中,偏移量表示时区相对于 UTC 的相对偏移,
  40. 因此在您的输入末尾应该有类似于 "+01:00" 的内容。
  41. <details>
  42. <summary>英文:</summary>
  43. offset in ISO_OFFSET_DATE means the timezone is specified as a relative offset against UTC
  44. so something like &quot;+01:00&quot; is expected at the end of your input.
  45. </details>

huangapple
  • 本文由 发表于 2020年9月24日 18:55:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64044968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定