博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
minus_Java即时类| minus()方法与示例
阅读量:2527 次
发布时间:2019-05-11

本文共 3918 字,大约阅读时间需要 13 分钟。

minus

即时类minus()方法 (Instant Class minus() method)

Syntax:

句法:

public Instant minus(TemporalAmount t_amt);    public Instant minus(long amt, TemporalUnit t_unit);
  • minus() method is available in java.time package.

    minus()方法在java.time包中可用。

  • minus(TemporalAmount t_amt) method is used to subtract the given amount from this Instant and return the Instant.

    minus(TemporalAmount t_amt)方法用于从此Instant减去给定数量并返回Instant。

  • minus(long amt, TemporalUnit t_unit) method is used to subtract the given amount in the given unit from this Instant and return the Instant.

    minus(long amt,TemporalUnit t_unit)方法用于从此Instant减去给定单位的给定数量,并返回Instant。

  • These methods may throw an exception at the time of performing subtraction.

    这些方法在执行减法时可能会引发异常。

    • ArithmeticException: This exception may throw when the calculated result exceeds the limit to represent this object.ArithmeticException :当计算结果超出表示此对象的限制时,可能引发此异常。
    • DateTimeException: This exception may throw when getting any error during subtraction.DateTimeException :在减法过程中遇到任何错误时,都可能引发此异常。
    • UnsupportedTemporalTypeException: This exception may throw when the given unit is unsupported.UnsupportedTemporalTypeException :当不支持给定单元时,可能引发此异常。
  • These are non-static methods and it is accessible with class objects and if we try to access these methods with class name then we will get an error.

    这些是非静态方法,可通过类对象访问,如果尝试使用类名访问这些方法,则会收到错误消息。

Parameter(s):

参数:

  • In the first case, "minus(TemporalAmount t_amt)",

    在第一种情况下,“ minus(TemporalAmount t_amt)”,

    • TemporalAmount t_amt – represents the amount to be subtracted from this Instant.
    • TemporalAmount t_amt –表示要从此Instant减去的数量。
  • In the second case, "minus(long amt, TemporalUnit t_unit)",

    在第二种情况下,“减号(long amt,TemporalUnit t_unit)”,

    • long amt – represents the amount in units to be subtracted from this Instant.
    • long amt-表示要从此Instant减去的金额。
    • TemporalUnit t_unit – represents the unit to measure the given amount.
    • TemporalUnit t_unit –代表测量给定数量的单位。

Return value:

返回值:

In both the cases, the return type of the method is Instant,

在这两种情况下,方法的返回类型均为Instant 。

  • In the first case, it returns the Instant that holds the value subtracted the given amount from this Instant.

    在第一种情况下,它返回的Instant保持从该Instant减去给定值的值。

  • In the second case, it returns the Instant that holds the value subtracted the given amount in unit from this Instant.

    在第二种情况下,它返回的Instant保持从该Instant中减去以单位表示的给定值的值。

Example:

例:

// Java program to demonstrate the example // of minus() method of Instantimport java.time.*;import java.time.temporal.*;public class MinusOfInstant {
public static void main(String args[]) {
long amt = 2; // Instantiates two Instant and // a Duration object Instant ins1 = Instant.parse("2006-04-03T05:10:15.00Z"); Instant ins2 = Instant.now(); Duration du = Duration.ofSeconds(2); // Display ins1,ins2 System.out.println("Instant ins1 and ins2: "); System.out.println("ins1: " + ins1); System.out.println("ins2: " + ins2); System.out.println("amt to subtract: " + amt); System.out.println("du to subtract: " + du); System.out.println(); // Here, this method subtracts the given amount // in the given unit from this Instant i.e. here // we are subtracting 2 hours from Instant ins1 Instant minus_val = ins1.minus(amt, ChronoUnit.HOURS); // Display minus_val System.out.println("ins1.minus(amt,ChronoUnit.HOURS): " + minus_val); // Here, this method subtracts the given amount // from this Instant i.e. here we are subtracting // 2 seconds from Instant ins2 minus_val = ins2.minus(du); // Display minus_val System.out.println("ins2.minus(du): " + minus_val); }}

Output

输出量

Instant ins1 and ins2: ins1: 2006-04-03T05:10:15Zins2: 2020-05-28T01:06:04.706169Zamt to subtract: 2du to subtract: PT2Sins1.minus(amt,ChronoUnit.HOURS): 2006-04-03T03:10:15Zins2.minus(du): 2020-05-28T01:06:02.706169Z

翻译自:

minus

转载地址:http://uivzd.baihongyu.com/

你可能感兴趣的文章
秒杀多线程第四篇 一个经典的多线程同步问题
查看>>
RocketMQ配置
查看>>
vs code调试console程序报错--preLaunchTask“build”
查看>>
蚂蚁金服井贤栋:用技术联手金融机构,形成服务小微的生态合力
查看>>
手机通话记录统计分析
查看>>
富文本编辑器比较
查看>>
端口号大全
查看>>
机器学习基石笔记2——在何时可以使用机器学习(2)
查看>>
POJ 3740 Easy Finding (DLX模板)
查看>>
MySQL 处理重复数据
查看>>
关于typedef的用法总结(转)
查看>>
hibernate could not resolve property
查看>>
【strtok()】——分割字符串
查看>>
Linux下安装rabbitmq
查看>>
曹德旺
查看>>
【转】判断点在多边形内(matlab)
查看>>
java基础之集合:List Set Map的概述以及使用场景
查看>>
Python 线程 进程 协程
查看>>
骨牌覆盖问题
查看>>
iOS语言中的KVO机制
查看>>