简体   繁体   English

如何在 java.time.Instant 中获取和设置指定时间?

[英]How to get and set specified time in java.time.Instant?

I have two java.time.Instant objects我有两个 java.time.Instant 对象

Instant dt1;
Instant dt2;

I want to get time (only hours and minutes without date) from dt2 and set it to dt1.我想从 dt2 获取时间(只有小时和分钟,没有日期)并将其设置为 dt1。 What is the best way to to this?最好的方法是什么? Using使用

dt2.get(ChronoField.HOUR_OF_DAY) 

throws java.time.temporal.UnsupportedTemporalTypeException抛出 java.time.temporal.UnsupportedTemporalTypeException

You have to interpret the Instant at some time zone to get ZonedDateTime .您必须在某个时区解释 Instant 才能获得ZonedDateTime As an Instant measures the ellapsed seconds and nano seconds from epoch 1970-01-01T00:00:00Z you should use UTC to get the same time as the Instant would print.当 Instant 测量从纪元1970-01-01T00:00:00Z的经过秒数和纳秒时,您应该使用UTC来获得与 Instant 打印的时间相同的时间。 ( Z ≙ Zulu Time ≙ UTC ) ( Z ≙ 祖鲁时间 ≙ UTC )

Getting the time把握时机

Instant instant;

// get overall time
LocalTime time = instant.atZone(ZoneOffset.UTC).toLocalTime();
// get hour
int hour = instant.atZone(ZoneOffset.UTC).getHour();
// get minute
int minute = instant.atZone(ZoneOffset.UTC).getMinute();
// get second
int second = instant.atZone(ZoneOffset.UTC).getSecond();
// get nano
int nano = instant.atZone(ZoneOffset.UTC).getNano();

There are also methods to get days, month and year ( getX ).还有一些方法可以获取天、月和年 ( getX )。

Setting the time设置时间

Instants are immutable so you can only "set" the time by creating a copy of your instant with the given time change.瞬间是不可变的,因此您只能通过创建具有给定时间更改的瞬间的副本来“设置”时间。

instant = instant.atZone(ZoneOffset.UTC)
        .withHour(hour)
        .withMinute(minute)
        .withSecond(second)
        .withNano(nano)
        .toInstant();

There are also methods to alter days, month and year ( withX ) as well as methods to add ( plusX ) or subtract ( minusX ) time or date values.还有一些方法可以更改天、月和年 ( withX ) 以及添加 ( plusX ) 或减去 ( minusX ) 时间或日期值的方法。

To set the time to a value given as a string use: .with(LocalTime.parse("12:45:30"))要将时间设置为作为字符串使用的值: .with(LocalTime.parse("12:45:30"))

Instant does not have any hour / minute. Instant没有任何小时/分钟。 Please read the documentation of Instant class : https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html请阅读 Instant 类的文档: https : //docs.oracle.com/javase/8/docs/api/java/time/Instant.html

If you use System Timezone to convert the Instant , you can use something like this :如果您使用系统时区来转换 Instant ,您可以使用以下内容:

LocalDateTime ldt1 = LocalDateTime.ofInstant(dt1, ZoneId.systemDefault());
        LocalDateTime ldt2 = LocalDateTime.ofInstant(dt2, ZoneId.systemDefault());

        ldt1 = ldt1
            .withHour(ldt2.getHour())
            .withMinute(ldt2.getMinute())
            .withSecond(ldt2.getSecond());

        dt1 = ldt1.atZone(ZoneId.systemDefault()).toInstant();

Convert first the Instant to LocalDateTime , and use UTC as its timezone, then you can get its hours.首先将Instant转换为LocalDateTime ,并使用 UTC 作为其时区,然后您可以获得它的小时数。

import java.time.*
LocalDateTime.ofInstant(Instant.now(), ZoneOffset.UTC).getHour()

While the upper answer is a good, I used it but in Kotlin.虽然上面的答案很好,但我在 Kotlin 中使用了它。 Thankyou @frido谢谢@frido

while (startDate.isBefore(endDate)) {
        val year: Int = startDate.atZone(ZoneOffset.UTC).year
        val month: Int = startDate.atZone(ZoneOffset.UTC).monthValue
        val day: Int = startDate.atZone(ZoneOffset.UTC).dayOfMonth
        System.out.printf("%d.%d.%d\n", day, month, year)

        startDate = startDate.atZone(ZoneOffset.UTC).withDayOfMonth(
            day + 1
        ).toInstant()
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM