Java Duration toMillisPart() 方法及示例
java.time包中Duration类的toMillisPart()方法是通过用纳秒数除以1,000,000来获取该持续时间的毫秒数。
语法。
public long toMillisPart()
参数。此方法不接受任何参数。
返回值。该方法返回一个长值,即用纳秒数除以1,000,000得出的该持续时间的毫秒数。
下面的例子说明了Duration.toMillisPart()方法。
例1:
// Java code to illustrate toMillisPart() method
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
// Duration using parse() method
Duration duration = Duration.parse("P2DT3H4M");
System.out.println("Duration: "
+ duration);
// Get the number of milli-seconds
// using toMillisPart() method
System.out.println(duration.toMillisPart());
}
}
输出:
Duration: PT51H4M
0
例2:
// Java code to illustrate toMillisPart() method
import java.time.Duration;
public class GFG {
public static void main(String[] args)
{
// Duration using ofMillis() method
Duration duration = Duration.ofMillis(10);
System.out.println("Duration: "
+ duration);
// Get the number of milli-seconds
// using toMillisPart() method
System.out.println(duration.toMillisPart());
}
}
输出:
Duration: PT0.01S
10
参考资料: https://docs.oracle.com/javase/9/docs/api/java/time/Duration.html#toMillisPart-