我想代表一个“营业日期”,例如“2019 年 6 月 3 日”发生的交易。为此,我们主动忽略时区,因为我们完全知道日本的“2019 年 6 月 3 日”可能是美国的“2019 年 6 月 2 日”——而在“一天”内订购同样无关紧要。所有日期都是今天或之前的日期。
我显而易见的答案是,这是一个LocalDate. 但是,其他人建议将其表示为Instantof更好2019-06-03T00:00:00.000Z。
除了在 UI 中将其转换/格式化为人类可读的日期时我们必须进行的不同调用,这两种方法之间实际上有什么区别吗?
这是一个与Instant 和 LocalDateTime 之间有什么区别的不同问题?因为时间与这个问题无关,它只与过去(或当前)日期有关。
我正在使用 TrueTime 库,它Date在系统时区返回。Date当我将其转换为毫秒时,我在将其转换为 UTC 日期时遇到问题。
这是我所做的:
// getting true time in GMT [ex : 2020-07-13T18:00:57.192+03:00 [Europe/Istanbul]]
Date now = getTrueNowTimeInGMT();
// I like to use `LocalDateTime`, so I am converting `Date` to `LocalDateTime`:
LocalDateTime ldtOfSystem = LocalDateTime.ofInstant(now.toInstant(), ZoneId.systemDefault());
// Converting system default zone to UTC
ZonedDateTime zdtOfSystem = ldtOfSystem.atZone(ZoneId.systemDefault());
ZonedDateTime zdtOfSystemInUTC = zdtOfSystem.withZoneSameInstant(ZoneId.of("UTC"));
// Making sure that it is in the UTC. It is returning correct UTC time [ex: 2020-07-13T15:00:57.192 [UTC]]
System.out.println(zdtOfSystemInUTC);
// converting zdtOfSystemInUTC to …Run Code Online (Sandbox Code Playgroud) 我有一个pojo,其字段类型为Instant.我想设置瞬间从TimsStamp获取它.可能吗?
例如:我有一个java.sql.Timestamp我想要转换为java.time.Instant.可能吗?
我是Mapstruct的新手。我有一个包含LocalDateTime类型字段的模型对象。DTO包含Instant类型字段。我想将LocalDateTime类型字段映射到Instant类型字段。我有传入请求的TimeZone实例。
像这样手动设置字段;
set( LocalDateTime.ofInstant(x.getStartDate(), timeZone.toZoneId()) )
Run Code Online (Sandbox Code Playgroud)
如何使用Mapstruct映射这些字段?
datetime-conversion java-time mapstruct localdate java.time.instant
以下代码:
Instant inFourWeeks = Instant.now().plus(4L, ChronoUnit.WEEKS);
Run Code Online (Sandbox Code Playgroud)
引发异常:
java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Weeks
Run Code Online (Sandbox Code Playgroud)
为什么几周都不受支持?我理解为什么不支持数月和数年,因为它们在较小单位的持续时间可能会有所不同.但是一周有不变的持续时间(7天),我可以通过写作来实现同样的目标:
Instant inFourWeeks = Instant.now().plus(4L * 7L, ChronoUnit.DAYS);
Run Code Online (Sandbox Code Playgroud) 自 Java 9 以来 Java 日期和时间 API 发生了变化。 LocalDateTime 现在具有微秒精度。
Java 9 具有 java.time.Clock 的全新实现,能够以比毫秒(十进制小数的三位数字)更精细的分辨率捕获当前时刻。
我们从后端服务中获得以微秒为单位的时间。
System.currentTimeMillis > 1565245051795 > 2019-08-08T06:17:31.795
Service.getTime > 1565245051795306 > 2019-08-08T06:17:31.795306
Run Code Online (Sandbox Code Playgroud)
为了构造在我们的应用程序中使用的 LocalDateTime,我们执行
long timeMicros = service.getTime();
long timeMillis = timeMicros / 1000;
LocalDateTime ldt = Instant.ofEpochMilli(timeMillis).atZone(ZoneId.systemDefault()).toLocalDateTime();
Run Code Online (Sandbox Code Playgroud)
为了查询服务,我们再次需要时间微秒,然后我们做
long timeMillis = dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
long timeMicros = timeMillis * 1000;
Run Code Online (Sandbox Code Playgroud)
问题是我们没有取回时间微秒的精度。
是否可以创建具有微秒精度的 Instant ?
我们现在使用的是 Java 11。当我们的一个 JUnit 测试由于增加的微秒精度而失败时,我注意到了这一变化。
对于 JUnit 测试,我找到了一个解决方法:
private static final LocalDateTime START = LocalDateTime.now().truncatedTo(ChronoUnit.MILLIS);
Run Code Online (Sandbox Code Playgroud)
我不确定这是一种解决方法还是实际的解决方案,但添加时间戳的最后三微秒数字似乎有效。
System.currentTimeMillis > 1565245051795 > 2019-08-08T06:17:31.795
Service.getTime > …Run Code Online (Sandbox Code Playgroud) 这是我用来使用 Instant.parse 解析字符串的代码,
String date = "2018-05-01T00:00:00";
Instant.parse(date)
Run Code Online (Sandbox Code Playgroud)
并低于错误
java.time.format.DateTimeParseException: Text '2018-05-01T00:00: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)
我不能使用其他然后Instant所以只为它寻找解决方案!
我正在尝试将以下字符串解析2021-10-15T08:39:05+02:00为一个Instant可以使用 Java 15 无缝运行的字符串,但对于 Java 11 会抛出错误。
java.time.format.DateTimeParseException: Text '2021-10-19T11:06:35+02:00' could not be parsed at index 19
为什么会这样?
Instant.parse("2021-10-15T08:39:05+02:00");
Run Code Online (Sandbox Code Playgroud)
编辑:Java 15
在爪哇,
Instant表示时间点,编码为 UTC 格式的日期和时间。LocalDateTime表示一个时间点,编码为 JVM 本地时区中的日期和时间。那么为什么LocalDateTime.ofInstant()需要 aZoneId作为第二个参数呢?
这LocalDateTime不仅会造成混乱并且可能不正确,而且会使其与ZonedDateTime;相同。因为 的时区LocalDateTime可以是任何时区,就像ZonedDateTime.
Instant instant;
void updateBy(){
instant = Instant.now();
}
Run Code Online (Sandbox Code Playgroud)
如果是,如何证明 Instant 是线程安全的?