热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Java中的即时调整()方法,示例

Java中的即时调整()方法,示例原文:https://www

Java 中的即时调整()方法,示例

原文:https://www . geesforgeks . org/instant-adjustin to-method-in-Java-with-example/

即时类调整(时态时态)方法调整传递的时态对象,使其具有应用该方法的瞬间。

语法:

public Temporal adjustInto(Temporal temporal)

参数:该方法接受一个参数时间,该参数是要调整的目标时间对象。它不应为空。

返回值:该方法返回调整后的时态对象。

异常:此方法抛出以下异常:


  • datetimeexception If this method cannot be adjusted.

  • Arithmetic exception If there is a numerical overflow during adjustment.

下面的程序说明了 Instant.adjustInto()方法:

程序 1:

// Java program to demonstrate
// Instant.adjustInto() method
import java.time.*;
import java.time.temporal.Temporal;
public class GFG {
    public static void main(String[] args)
    {
        // create an instance object
        Instant instant
            = Instant.parse("2018-11-20T16:55:30.00Z");
        // create a Temporal object
        // which is equal to OffsetDateTime object
        OffsetDateTime passTemporal
            = OffsetDateTime.now();
        // print passed Value
        System.out.println("Passed Value: "
                           + passTemporal);
        // apply adjustInto method
        // to adjust OffsetDateTime Temporal
        // to instant object
        Temporal returnTemporal
            = instant.adjustInto(passTemporal);
        // print results
        System.out.println("Returned Value: "
                           + (OffsetDateTime)returnTemporal);
    }
}

输出:

Passed Value: 2018-11-22T09:22:17.297Z
Returned Value: 2018-11-20T16:55:30Z

程序二:

// Java program to demonstrate
// Instant.adjustInto() method
import java.time.*;
import java.time.temporal.Temporal;
public class GFG {
    public static void main(String[] args)
    {
        // create an instance object
        Instant instant
            = Instant.parse("2018-11-17T06:50:39.00Z");
        // create a Temporal object
        // which is equal to ZonedDateTime object
        ZonedDateTime passTemporal
            = ZonedDateTime.now();
        // print passed Value
        System.out.println("Passed Value: "
                           + passTemporal);
        // apply adjustInto method
        // to adjust ZonedDateTime Temporal
        // to instant object
        Temporal returnTemporal
            = instant.adjustInto(passTemporal);
        // print results
        System.out.println("Returned Value: "
                           + (ZonedDateTime)returnTemporal);
    }
}

输出:

Passed Value: 2018-11-22T09:22:20.995Z[Etc/UTC]
Returned Value: 2018-11-17T06:50:39Z[Etc/UTC]

程序 3:

// Java program to demonstrate
// Instant.adjustInto() method
import java.time.*;
import java.time.temporal.Temporal;
public class GFG {
    public static void main(String[] args)
    {
        // create an instance object
        Instant instant
            = Instant.parse("2017-11-01T16:25:00.00Z");
        // create a Temporal object
        // which is equal to Instant object
        // with current Instant
        Temporal passTemporal
            = Instant.now();
        // print passed Value
        System.out.println("Passed Value: "
                           + passTemporal);
        // apply adjustInto method to adjust Temporal
        // to this instant object
        Temporal returnTemporal
            = instant.adjustInto(passTemporal);
        // print results
        System.out.println("Returned Value: "
                           + returnTemporal);
    }
}

输出:

Passed Value: 2018-11-22T09:22:23.298Z
Returned Value: 2017-11-01T16:25:00Z

参考文献:https://docs . Oracle . com/javase/10/docs/API/Java/time/instant . html # adjustInto(Java . time . temporal . temporal)


推荐阅读
author-avatar
磊磊860219
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有