📜  Java中的日历 setTimeInMillis() 方法及示例

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

Java中的日历 setTimeInMillis() 方法及示例

Calendar 类中的setTimeInMillis(long mill_sec )方法用于根据传递的 long 值设置此 Calendar 表示的 Calendars 时间。

句法:

public void setTimeInMillis(long mill_sec)

参数:该方法采用一个长数据类型参数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: Java/util/Calendar.html#setTimeInMillis(long)