日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java即时类| plus()方法与示例

發布時間:2025/3/11 java 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java即时类| plus()方法与示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

即時類plus()方法 (Instant Class plus() method)

Syntax:

句法:

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

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

  • plus(TemporalAmount t_amt) method is used to add the given amount to this Instant and return the Instant.

    plus(TemporalAmount t_amt)方法用于將給定金額添加到此Instant并返回Instant。

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

    plus(long amt,TemporalUnit t_unit)方法用于將給定單位的給定金額添加到此Instant并返回Instant。

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

    這些方法在執行加法時可能會引發異常。

    • ArithmeticException: This exception may throw when the calculated result exceeds the limit to represent this object.ArithmeticException :當計算結果超出表示此對象的限制時,可能引發此異常。
    • UnsupportedTemporalTypeException: This exception may throw when the given unit is unsupported.UnsupportedTemporalTypeException :當不支持給定單元時,可能引發此異常。
    • DateTimeException: This exception may throw when getting any error during addition.DateTimeException :在添加過程中出現任何錯誤時,可能引發此異常。
  • These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.

    這些是非靜態方法,可通過類對象訪問,如果嘗試使用類名訪問這些方法,則會收到錯誤消息。

Parameter(s):

參數:

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

    在第一種情況下,“ plus(TemporalAmount t_amt)”

    • TemporalAmount t_amt – represents the amount to be added to this Instant.
    • TemporalAmount t_amt –表示要添加到此Instant的數量。
  • In the second case, "plus(long amt, TemporalUnit t_unit)",

    在第二種情況下,“ plus(long amt,TemporalUnit t_unit)”,

    • long amt – represents the amount in units to be added to 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 added the given amount to this Instant.

    在第一種情況下,它返回包含將給定金額添加到此Instant的值的Instant。

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

    在第二種情況下,它會返回即時值,該值保存將給定值添加到此即時值中的值。

Example:

例:

// Java program to demonstrate the example // of plus() method of Instantimport java.time.*; import java.time.temporal.*;public class PlusOfInstant {public static void main(String args[]) {long amt = 2;// Instantiates two Instant and// a Duration objectInstant ins1 = Instant.parse("2006-04-03T05:10:15.00Z");Instant ins2 = Instant.now();Duration du = Duration.ofSeconds(2);// Display ins1,ins2System.out.println("Instant ins1 and ins2: ");System.out.println("ins1: " + ins1);System.out.println("ins2: " + ins2);System.out.println("amt to add: " + amt);System.out.println("du to add: " + du);System.out.println();// Here, this method adds the given amount// in the given unit with this Instant and// returns the Instant i.e. here we are// adding 2 hours with Instant ins1Instant plus_val = ins1.plus(amt, ChronoUnit.HOURS);// Display plus_valSystem.out.println("ins1.plus(amt,ChronoUnit.HOURS): " + plus_val);// Here, this method adds the given amount// with this Instant and returns the Instant // i.e. here we are adding 2 seconds// with Instant ins2plus_val = ins2.plus(du);// Display plus_valSystem.out.println("ins2.plus(du): " + plus_val);} }

Output

輸出量

Instant ins1 and ins2: ins1: 2006-04-03T05:10:15Z ins2: 2020-05-28T07:28:15.161687Z amt to add: 2 du to add: PT2Sins1.plus(amt,ChronoUnit.HOURS): 2006-04-03T07:10:15Z ins2.plus(du): 2020-05-28T07:28:17.161687Z

翻譯自: https://www.includehelp.com/java/instant-plus-method-with-example.aspx

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Java即时类| plus()方法与示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。