Java中的 LocalTime adjustInto() 方法及示例
LocalTime类的adjustInto()方法用于将指定的时间对象调整为与此LocatTime 对象具有相同的时间。
句法:
public Temporal adjustInto(Temporal temporal)
参数:此方法接受单个参数temporal ,它是要调整的目标对象,而不是具体为 null。
返回值:该方法返回调整后的时间对象。
异常:该函数抛出两个异常,如下所述:
- DateTimeException – 如果无法进行调整
- ArithmeticException – 如果发生数字溢出
下面的程序说明了 adjustInto() 方法:
方案一:
// Java program to demonstrate
// LocalTime.adjustInto() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a LocalTime Object
LocalTime local
= LocalTime.parse("09:32:42");
// create Zone Time
ZonedDateTime zonetime = ZonedDateTime.now();
// print Zone Time
System.out.println("ZonedDateTime"
+ " before adjustInto():"
+ zonetime.toString());
// apply adjustInto()
zonetime = (ZonedDateTime)local
.adjustInto(zonetime);
// print Zone Time
System.out.println("ZonedDateTime"
+ " after adjustInto():"
+ zonetime.toString());
}
}
输出:
ZonedDateTime before adjustInto():2018-12-03T06:30:31.142Z[Etc/UTC]
ZonedDateTime after adjustInto():2018-12-03T09:32:42Z[Etc/UTC]
方案二:
// Java program to demonstrate
// LocalTime.adjustInto() method
import java.time.*;
import java.time.temporal.Temporal;
public class GFG {
public static void main(String[] args)
{
// create a LocalTime Object
LocalTime local = LocalTime.parse("19:52:43");
// create a Temporal object
// which is equal to OffsetDateTime object
OffsetDateTime passTemporal
= OffsetDateTime.now();
// print passed Value
System.out.println("Before adjustInto() OffsetDateTime: "
+ passTemporal);
// apply adjustInto method
// to adjust OffsetDateTime Temporal
Temporal returnTemporal
= local.adjustInto(passTemporal);
// print results
System.out.println("After adjustInto() OffsetDateTime: "
+ (OffsetDateTime)returnTemporal);
}
}
输出:
Before adjustInto() OffsetDateTime: 2018-12-03T06:30:33.927Z
After adjustInto() OffsetDateTime: 2018-12-03T19:52:43Z
参考: https: Java Java)