📜  Java中的 Calendar getTimeInMillis() 方法及示例

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

Java中的 Calendar getTimeInMillis() 方法及示例

Calendar 类中的getTimeInMillis()方法用于返回此 Calendar 的当前时间(以毫秒为单位)。
句法:

public long getTimeInMillis()

参数:该方法不带任何参数。
返回值:该方法以毫秒为单位返回此日历的当前时间。
下面的程序说明了 Calendar 类的 getTimeInMillis() 方法的工作:
示例 1:

Java
// Java code to illustrate
// getTimeInMillis() method
 
import java.util.*;
 
public class Calendar_Demo {
 
    public static void main(String args[])
        throws InterruptedException
    {
 
        // Creating a calendar
        Calendar calndr1 = Calendar.getInstance();
 
        // Displaying the current time
        System.out.println("The Current Time"
                           + " is: "
                           + calndr1.getTimeInMillis());
 
        // Adding few delay
        Thread.sleep(10000);
 
        // Creating another calendar
        Calendar calndr2 = Calendar.getInstance();
 
        // Displaying the upcoming time
        System.out.println("The Upcoming Time"
                           + " is: "
                           + calndr2.getTimeInMillis());
    }
}


Java
// Java code to illustrate
// getTimeInMillis() method
 
import java.util.*;
 
public class Calendar_Demo {
 
    public static void main(String args[])
        throws InterruptedException
    {
 
        // Creating a calendar
        Calendar calndr1 = Calendar.getInstance();
 
        // Displaying the current time
        System.out.println("The Current Time"
                           + " is: "
                           + calndr1.getTimeInMillis());
 
        // Adding few delay
        Thread.sleep(5000);
 
        // Creating another calendar
        Calendar calndr2 = Calendar.getInstance();
 
        // Displaying the upcoming time
        System.out.println("The Upcoming Time"
                           + " is: "
                           + calndr2.getTimeInMillis());
    }
}


输出:
The Current Time is: 1550725664034
The Upcoming Time is: 1550725674053

示例 2:

Java

// Java code to illustrate
// getTimeInMillis() method
 
import java.util.*;
 
public class Calendar_Demo {
 
    public static void main(String args[])
        throws InterruptedException
    {
 
        // Creating a calendar
        Calendar calndr1 = Calendar.getInstance();
 
        // Displaying the current time
        System.out.println("The Current Time"
                           + " is: "
                           + calndr1.getTimeInMillis());
 
        // Adding few delay
        Thread.sleep(5000);
 
        // Creating another calendar
        Calendar calndr2 = Calendar.getInstance();
 
        // Displaying the upcoming time
        System.out.println("The Upcoming Time"
                           + " is: "
                           + calndr2.getTimeInMillis());
    }
}
输出:
The Current Time is: 1550725683182
The Upcoming Time is: 1550725688208

参考: https: Java/util/Calendar.html#getTimeInMillis–