尝试从日期字符串(例如“2020-08-24”)解析 ZonedDateTime。
使用 TemporalAccesor 和 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("Unable to parse date {} using formatter DateTimeFormatter.ISO_INSTANT", dateString);
}
return ZonedDateTime.from(parsed);
}
Run Code Online (Sandbox Code Playgroud) 我有三个 LocalDate(最后 3 天),对于每个日期,我想循环并执行一些操作:
StringBuilder result = "";
for (LocalDate currentDate = LocalDate.now(); currentDate.isAfter(LocalDate.now().minusDays(3)); currentDate = currentDate.minusDays(1)){
Page<User> users = userService.getAllUsersByRegistrationDate(currentDate);
String reportTable = reportService.getReportTable(users, tableTemplate);
result.append(reportTable);
}
Run Code Online (Sandbox Code Playgroud)
如何用 Stream API 替换此代码或提高可读性?
我试图划分两个持续时间 ( java.time.Duration) 并执行有两种方法
java.time.Duration#dividedBy(long)它返回一个本身类型的对象Duration,但由于一些 API 限制,我无法使用这个版本。
java.time.Duraiton#dividedBy(Duration)它返回 along并返回一个持续时间在其他持续时间内发生的次数,但这里我们失去了精度,因为它不关心余数。
有没有办法执行此除法并得到余数结果。
Duration.ofHours(1).dividedBy(7L); //returns 8M34.28571 seconds
Duration.ofHours(1).dividedBy(Duration.ofSeconds(7L)) // returns 514 seconds
Run Code Online (Sandbox Code Playgroud)
我有一个限制,不能使用第一种方式。我可以使用其他方式获得结果吗?
编辑:(来自评论)我不允许将 Duration 实例转换为序数值,例如毫秒。
我正在使用 Kotlin 开发一个 Android 项目,我们需要对设备和后端的日期进行验证。到目前为止,我们一直在使用 Date 类来表示日期,使用 SimpleDateFormat 类来解析字符串和格式化日期,以及使用 Calendar 类对日期执行操作,例如获取 x 天前的日期等。我发现一些资源指出处理日期的正确方法是使用 java.time 包(LocalDateTime、LocalTime 等类)。我有兴趣确切地知道处理日期的正确方法(从解析到格式化)以及是否需要考虑时区。我见过类似的问题,但没有一个真正概括了我想知道的内容。
我需要一些支持。我希望将变量字符串转换为日期。变量日期的格式应为 dd-MM-yyyy。
import java.util.Date;
....
...
String a = "2022-05-12";
Date b; // should be dd-MM-yyyy
do some to format...
return b; // return b with format dd-MM-yyyy, remember this variable is type Date no String
Run Code Online (Sandbox Code Playgroud)
我试图做一些事情,但获得的格式不是所需的格式。
我的数据库中有多次如下所示:
String summerTime = "2022-07-21T10:00:00Z";
String winterTime = "2022-11-21T10:00:00Z";
Run Code Online (Sandbox Code Playgroud)
另一方面,我想根据自定义时区获取这些日期的时间部分,我拥有的时区如下所示:
GMT-03:00
GMT-02:00
GMT-01:00
GMT+02:00
GMT+01:00
....
Run Code Online (Sandbox Code Playgroud)
当我尝试使用以下代码提取时间部分时:
ZoneId tz = ZoneId.of("GMT+01:00");
ZonedDateTime date1 = ZonedDateTime.parse(summerTime).withZoneSameInstant(tz.toZoneId());
ZonedDateTime date2 = ZonedDateTime.parse(winterTime).withZoneSameInstant(tz.toZoneId());
Run Code Online (Sandbox Code Playgroud)
我得到:
11:00
11:00
Run Code Online (Sandbox Code Playgroud)
但如果您仔细观察,即使在夏季或冬季约会,时间也是相同的。
我期望得到:
12:00 // <----- correct
11:00 // <----- correct
Run Code Online (Sandbox Code Playgroud)
我找到了解决问题的方法,即使用时区的全名而不是缩写:
ZoneId tz = ZoneId.of("Europe/Paris");
Run Code Online (Sandbox Code Playgroud)
这给了我正确的回应:
12:00
11:00
Run Code Online (Sandbox Code Playgroud)
但我不能使用它,因为我应该只使用缩写。
我的问题:
下面的代码片段
Instant.parse("2023-08-08T00:00:00+02:00")
Run Code Online (Sandbox Code Playgroud)
按照 java-17 中的预期进行编译和执行。但是用java-8执行时,抛出以下异常
java.time.format.DateTimeParseException: Text '2023-08-01T00:00:00+02:00' could not be parsed at index 19
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.Instant.parse(Instant.java:395)
...
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么?java.time api 有什么变化吗?
请注意,我确实知道解决此问题的方法,以下代码适用于 java-8
OffsetDateTime.parse("2023-08-01T00:00:00+02:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME).toInstant()
Run Code Online (Sandbox Code Playgroud)
它产生了期望的结果。我有兴趣知道在 java-time api 实现中,行为是否已经改变?
我想解析我java.util.Date发现java.time.LocalDate
这个代码,当我搜索问题时:
Date date = new SimpleDateFormat("dd.MM.yyyy").parse("01.01.0001");
date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
Run Code Online (Sandbox Code Playgroud)
如果我有01.01.0001开始日期,我会遇到问题:
date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
Run Code Online (Sandbox Code Playgroud)
==> (java.time.LocalDate) 0000-12-29
29.12.0000并且01.01.0001不一样。
LocalDate价值该怎么办01.01.0001?时区信息:
ZoneId.systemDefault()
Run Code Online (Sandbox Code Playgroud)
==> (java.time.ZoneRegion) 欧洲/柏林
date.toInstant() Date date0001 = new SimpleDateFormat("dd.MM.yyyy").parse("01.01.0001");
System.out.println(date0001.toInstant());
Run Code Online (Sandbox Code Playgroud)
==> 0000-12-29T23:00:00Z
Date date1901 = new SimpleDateFormat("dd.MM.yyyy").parse("01.01.1901");
System.out.println(date1901.toInstant());
Run Code Online (Sandbox Code Playgroud)
==> 1900-12-31T23:00:00Z
我有一个很长的格式:
Long l = 20151228;
我想将它解析为LocalDate对象.
我怎样才能做到这一点?