📜  Java中的 TimeUnit toDays() 方法及示例

📅  最后修改于: 2022-05-13 01:55:15.032000             🧑  作者: Mango

Java中的 TimeUnit toDays() 方法及示例

TimeUnit 类toDays()方法用于获取 TimeUnit 对象表示的时间,以天数表示,从 1970 年 1 月 1 日午夜 UTC 开始。它等价于 DAYS.convert(duration, this)。

句法:

public long toDays(long duration)

参数:此方法接受一个强制参数duration ,它是以毫秒为单位的持续时间。

返回值:此方法将转换后的持续时间返回为天。

下面的程序说明了 TimeUnit toDays() 方法的实现:

方案一:

// Java program to demonstrate
// toDays() method of TimeUnit Class
  
import java.util.concurrent.*;
import java.util.Date;
  
class GFG {
    public static void main(String args[])
    {
        // Get current time in milliseconds
        long timeInMilliSec = new Date().getTime();
  
        // Create a TimeUnit object
        TimeUnit time = TimeUnit.MILLISECONDS;
  
        // Convert milliseconds to days
        // using toDays() method
        System.out.println("Time " + timeInMilliSec
                           + " milliSeconds in Days = "
                           + time.toDays(timeInMilliSec));
    }
}
输出:
Time 1539585725051 milliSeconds in Days = 17819

方案二:

// Java program to demonstrate
// toDays() method of TimeUnit Class
  
import java.util.concurrent.*;
import java.util.Calendar;
  
class GFG {
    public static void main(String args[])
    {
        // Get current time in milliseconds
        long timeInMilliSec = Calendar
                                  .getInstance()
                                  .getTimeInMillis();
  
        // Create a TimeUnit object
        TimeUnit time = TimeUnit.MILLISECONDS;
  
        // Convert milliseconds to days
        // using toDays() method
        System.out.println("Time " + timeInMilliSec
                           + " milliSeconds in Days = "
                           + time.toDays(timeInMilliSec));
    }
}
输出:
Time 1539585730178 milliSeconds in Days = 17819