Calendar类中的getTimeInMillis()方法用于返回此日历的当前时间(以毫秒为单位)。
用法:
public long getTimeInMillis()
参数:该方法不包含任何参数。
返回值:该方法以毫秒为单位返回此日历的当前时间。
以下示例程序旨在说明Calendar类的getTimeInMillis()方法的用法:
示例1:
// 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());
}
}
输出:
The Current Time is: 1550725664034
The Upcoming Time is: 1550725674053
示例2:
// 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