當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java TimeUnit toHours()用法及代碼示例


從1970年1月1日午夜UTC開始,使用TimeUnit類的toHours()方法獲取TimeUnit對象表示的時間,作為小時數。

用法:

public long toHours(long duration)

參數:此方法接受強製參數持續時間,該持續時間以毫秒為單位。


返回值:此方法將轉換後的持續時間作為小時返回。

下麵的程序演示了TimeUnit toHours()方法的實現:

示例1:

// Java program to demonstrate 
// toHours() 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 Hours 
        // using toHours() method 
        System.out.println("Time " + timeInMilliSec 
                           + " milliSeconds in Hours = "
                           + time.toHours(timeInMilliSec)); 
    } 
}
輸出:
Time 1539585595812 milliSeconds in Hours = 427662

示例2:

// Java program to demonstrate 
// toHours() 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 Hours 
        // using toHours() method 
        System.out.println("Time " + timeInMilliSec 
                           + " milliSeconds in Hours = "
                           + time.toHours(timeInMilliSec)); 
    } 
}
輸出:
Time 1539585598692 milliSeconds in Hours = 427662


相關用法


注:本文由純淨天空篩選整理自RishabhPrabhu大神的英文原創作品 TimeUnit toHours() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。