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


Java Calendar setTimeInMillis()用法及代碼示例


Calendar類中的setTimeInMillis(long mill_sec)方法用於根據傳遞的long值設置由此Calendar表示的Calendars時間。

用法:

public void setTimeInMillis(long mill_sec)

參數:該方法采用long類型的一個參數mill_sec,表示要設置的給定日期。


返回值:該方法不返回任何值。

以下示例程序旨在說明Calendar類的setTimeInMillis()方法的用法:
示例1:

// Java code to illustrate 
// setTimeInMillis() method 
  
import java.util.*; 
  
public class Calendar_Demo { 
    public static void main(String args[]) 
    { 
  
        // Creating calendar object 
        Calendar calndr = Calendar.getInstance(); 
  
        // Getting the time in milliseconds 
        System.out.println("The Current"
                           + " Time is: "
                           + calndr.getTime()); 
  
        // Changing time to 2000 milli-second 
        calndr.setTimeInMillis(2000); 
  
        // Displaying the new time 
        System.out.println("After setting"
                           + " Time: "
                           + calndr.getTime()); 
    } 
}
輸出:
The Current Time is: Fri Feb 22 08:00:54 UTC 2019
After setting Time: Thu Jan 01 00:00:02 UTC 1970

示例2:

// Java code to illustrate 
// setTimeInMillis() method 
  
import java.util.*; 
  
public class Calendar_Demo { 
    public static void main(String args[]) 
    { 
  
        // Creating calendar object 
        Calendar calndr = Calendar.getInstance(); 
  
        // Getting the time in milliseconds 
        System.out.println("The Current"
                           + " Time is: "
                           + calndr.getTime()); 
  
        // Changing time to 8000 milli-second 
        calndr.setTimeInMillis(8000); 
  
        // Displaying the new time 
        System.out.println("After setting"
                           + " Time: "
                           + calndr.getTime()); 
    } 
}
輸出:
The Current Time is: Fri Feb 22 08:01:02 UTC 2019
After setting Time: Thu Jan 01 00:00:08 UTC 1970

參考: https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#setTimeInMillis(long)



相關用法


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