日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

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

即時類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.

    這些方法在執(zhí)行加法時可能會引發(fā)異常。

    • ArithmeticException: This exception may throw when the calculated result exceeds the limit to represent this object.ArithmeticException :當計算結果超出表示此對象的限制時,可能引發(fā)此異常。
    • UnsupportedTemporalTypeException: This exception may throw when the given unit is unsupported.UnsupportedTemporalTypeException :當不支持給定單元時,可能引發(fā)此異常。
    • DateTimeException: This exception may throw when getting any error during addition.DateTimeException :在添加過程中出現(xiàn)任何錯誤時,可能引發(fā)此異常。
  • 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.

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

Parameter(s):

參數(shù):

  • 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的數(shù)量。
  • 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的單位數(shù)量。
    • TemporalUnit t_unit – represents the unit to measure the given amount.
    • TemporalUnit t_unit –代表測量給定數(shù)量的單位。

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

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。