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()方法与示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: scala中def_def关键字以及Sc
- 下一篇: gettimezone_Java日历ge