我在 Spring Boot 应用程序中有一个计划任务:
@Scheduled(fixedRateString = "${scheduled.task.rate}")
public void runScheduledTask() {
// ...
}
Run Code Online (Sandbox Code Playgroud)
并进行相应的测试:
@Test
public void test_scheduledTask_runs() {
await().atMost(Duration.ofMillis(scheduledTaskRate).multipliedBy(2)).untilAsserted(() -> {
Mockito.verify(scheduledTasks, Mockito.atLeastOnce()).runScheduledTask();
});
}
Run Code Online (Sandbox Code Playgroud)
现在我想使用 cron 而不是固定速率:
@Scheduled(cron = "${scheduled.task.cron}")
Run Code Online (Sandbox Code Playgroud)
现在我需要使测试适应这一点。如何获取Duration与cron表达式频率对应的对象?
我需要从文件中解析时区,其中指定的区域如下:UTC、SAST 等。
问题:虽然ZoneId.of("UTC")效果很好,但我不能对 SAST 做同样的事情。
我明白了java.time.zone.ZoneRulesException: Unknown time-zone ID: SAST。
问题:如何将“SAST”字符串转换为 Java 字符串ZoneId?
我知道,每次收到它时我都可以用“GMT+2”替换“SAST”,但如果有更优雅的方式那就太好了。
我正在使用 Java 11、Spring Boot 2.2.6.RELEASE。如何将“2019-10-21T13:00:00+02:00”反序列化为 LocalDateTime?
到目前为止尝试过:
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@DateTimeFormat(iso = ISO.DATE_TIME)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssZ")
private LocalDateTime startTime;
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
2021-02-19 07:45:41.402 WARN 801117 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String "2019-10-21T13:00:00+02:00": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2019-10-21T13:00:00+02:00' could not be parsed, unparsed text found at index 19; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDateTime` from String "2019-10-21T13:00:00+02:00": Failed to …Run Code Online (Sandbox Code Playgroud) 我正在阅读一些不发送典型内容的 RSS 网站:
2019-06-12T07:17:47Z-Instant.parse()可以使用Wed, 12 Jun 2019 03:17:47 -0400-DateTimeFormatter.RFC_1123_DATE_TIME.parse()可以使用相反,我得到这些字符串:
Tue, 25 May 2021 00:00:00 EDT03 Jun 2021 18:35:00 HKT我已经尝试过一些自定义模式和 ZonedDateTime + OffsetDateTimeparse()方法。尽管我还没有找到一种方法来获取可以转换为Instant. 我既不能控制源,也不能修复输出格式。
我怎样才能更宽容地解析这些日期时间?
我知道这java.time.Period是一个“估计”并且java.time.Duration是准确的。我仍然想将周期转换为持续时间。或者至少声明持续时间为数周或更长时间,而不需要采取任何手段。
即使这可以编译,它也会抛出运行时异常,因为ChronoUnit.WEEKS和更大的返回 trueChronoUnit.isDurationEstimated()
var d = Duration.of(2, ChronoUnit.WEEKS);
Run Code Online (Sandbox Code Playgroud)
同样,它可以编译,但会因为同样的原因抛出:
var d = Duration.from(Period.ofWeeks(2));
Run Code Online (Sandbox Code Playgroud)
从 ISO-8601 字符串进行往返是可行的,但味道不太对:
var p = Period.ofWeeks(2);
var d = Duration.parse(p.toString());
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?
无法理解为什么 BST(UTC 偏移:UTC +1)在以下代码中得到偏移 +11。我期望+1。
final String lastUpdated = "Mon Sep 27 18:29:00 BST 2021";
final DateTimeFormatter LAST_UPDATED_FORMATTER = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z y").withLocale(Locale.UK);
ZonedDateTime zonedDateTime = ZonedDateTime.parse(lastUpdated, LAST_UPDATED_FORMATTER);
LocalDateTime localDateTime = zonedDateTime.withZoneSameInstant(ZoneId.of("UTC")).toLocalDateTime();
System.out.println(zonedDateTime.getOffset()); // +11:00; expected +1
System.out.println(localDateTime.toString());
Run Code Online (Sandbox Code Playgroud)
控制台输出:
+11:00
2021-09-27T07:29
Run Code Online (Sandbox Code Playgroud)
一旦使用 UTC 日期,一切就清楚了:
final String lastUpdated = "Mon Sep 27 18:29:00 BST 2021";
Run Code Online (Sandbox Code Playgroud) 如何将 Instant(java.time.Instant) 转换为 joda DateTime (org.joda.time.DateTime)?或者更确切地说,做到这一点的最佳方法是什么?
我遇到了一个问题,DateTimeFormatter.ISO_DATE_TIME如果年份大于 9999,则无法解析日期。我正在寻找错误报告或了解我做错了什么。另外 -JodaTime解析适用于同一字符串。Java 版本 - 8。此测试失败:
public void testParseFarInTheFuture()
{
String str = "10000-04-11T01:24:55.887-03:56";
DateTimeFormatter.ISO_DATE_TIME.parse(str);
}
Run Code Online (Sandbox Code Playgroud)
与 9999 年的测试相同。
我做错了什么还是有错误?
我从源系统获取以下字符串格式的日期时间内容以及偏移时间值。
2019-08-07T19:20-5:00
Run Code Online (Sandbox Code Playgroud)
我想使用偏移值将其转换为日期时间。我尝试了以下方法,但没有得到预期的结果。
OffsetDateTime sourceDateTime = OffsetDateTime.parse("2019-08-07T19:20-5:00");
System.out.println(sourceDateTime.getOffset());
Run Code Online (Sandbox Code Playgroud)
输出
Exception in thread "main" java.time.format.DateTimeParseException: Text '2019-08-07T19:20-5:00' could not be parsed at index 16
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
at java.base/java.time.OffsetDateTime.parse(OffsetDateTime.java:402)
at java.base/java.time.OffsetDateTime.parse(OffsetDateTime.java:387)
at com.example.demo.Testfile.main(Testfile.java:19)
Run Code Online (Sandbox Code Playgroud)
有什么简单的方法可以达到预期的输出吗?
注意:我使用的是Java 8
我正在尝试更新我现有的 elasticsearch springboot 项目,由于源代码相当旧,它仍然使用 joda 时间。现在我要把Joda时间的所有功能升级为java时间。现在在项目中我们使用 Joda Time 的 Date Time
Joda Time 的代码示例
DateTime target = new DateTime(String targetDate, UTC);
Run Code Online (Sandbox Code Playgroud)
我们当前在代码中使用此函数将字符串转换为日期。使用此函数的字符串
2022-10-01T00:00:00.000
被转换为
2022-10-01T00:00:00.000Z
我正在尝试在 java 时间中复制相同的内容。
我尝试解析targetDateusing OffsetDateTimeandZonedDateTime但都给了我错误。
无法在索引 24 处解析文本“2022-10-01T00:00:00.0000”
经过一番尝试后,我能够通过使用继续前进LocalDateTime
LocalDateTime target = LocalDateTime.parse(String targetDate);
Run Code Online (Sandbox Code Playgroud)
它能够解析字符串,但格式不正确,我得到的格式是
2022-10-01T00:00Z
我还尝试将格式化程序与 LocalDateTime 一起使用
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSS'Z'");
LocalDateTime target= LocalDateTime.parse(targetDate,formatter);
Run Code Online (Sandbox Code Playgroud)
但这仍然给了我错误
无法在索引 24 处解析文本“2022-10-01T00:00:00.0000”
现在我对此有点困惑。
任何帮助表示赞赏。如果我提出问题或格式的方式在任何时候都是错误的,请纠正我。
问候。
编辑:抱歉造成混乱,但正如所指出的,我应该提到我希望返回的值作为java.time 日期时间对象而不是字符串,以便我可以进一步对其执行一些逻辑。非常遗憾。
感谢致敬