duration java_Java Duration类| 带示例的get()方法
duration java
持續(xù)時(shí)間類get()方法 (Duration Class get() method)
get() method is available in java.time package.
get()方法在java.time包中可用。
get() method is used to return the value for the given unit.
get()方法用于返回給定單位的值。
get() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
get()方法是一個(gè)非靜態(tài)方法,只能通過類對(duì)象訪問,如果嘗試使用類名稱訪問該方法,則會(huì)收到錯(cuò)誤消息。
get() method may throw an exception at the time of returning value for the given unit.
get()方法在返回給定單元的值時(shí)可能會(huì)引發(fā)異常。
- DateTimeException: This exception may throw when the given amt couldn’t convert into a Duration.DateTimeException :當(dāng)給定的amt無法轉(zhuǎn)換為Duration時(shí),可能引發(fā)此異常。
- UnsupportedTemporalTypeException: This exception may throw when the given unit is unsupported.UnsupportedTemporalTypeException :當(dāng)不支持給定單元時(shí),可能引發(fā)此異常。
Syntax:
句法:
public long get(TemporalUnit t_unit);Parameter(s):
參數(shù):
TemporalUnit t_unit – represents the temporal unit of the returned value.
TemporalUnit t_unit –表示返回值的時(shí)間單位。
Return value:
返回值:
The return type of this method is long, it returns the value for the given temporal unit.
此方法的返回類型為long ,它返回給定時(shí)間單位的值。
Example:
例:
// Java program to demonstrate the example // of long get(TemporalUnit t_unit) method of Durationimport java.time.*; import java.time.temporal.*;public class GetOfDuration {public static void main(String args[]) {// Instantiates two Duration objectsDuration du1 = Duration.ofHours(1);Duration du2 = Duration.ofMinutes(5);// Display du1 and du2System.out.println("du1: " + du1);System.out.println("du2: " + du2);// gets the value of the given unit i.e. // here we are requesting the value of // du1 in SECONDS unitlong get_val = du1.get(ChronoUnit.SECONDS);// Display get_valSystem.out.println("du1.get(ChronoUnit.SECONDS): " + get_val);// gets the value of the given unit i.e. // here we are requesting the value of // du2 in SECONDS unitget_val = du2.get(ChronoUnit.SECONDS);// Display get_valSystem.out.println("du2.get(ChronoUnit.SECONDS): " + get_val);} }Output
輸出量
du1: PT1H du2: PT5M du1.get(ChronoUnit.SECONDS): 3600 du2.get(ChronoUnit.SECONDS): 300翻譯自: https://www.includehelp.com/java/duration-get-method-with-example.aspx
duration java
總結(jié)
以上是生活随笔為你收集整理的duration java_Java Duration类| 带示例的get()方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 检查不孕不育去吴忠哪家医院好
- 下一篇: 地下城与勇士,收材料,怎么自动喊话,总不