Java 日历 getTimeInMillis()方法及示例
Calendar类中的 getTimeInMillis() 方法用于返回该日历的当前时间,单位为毫秒。
语法:
public long getTimeInMillis()
参数: 该方法不接受任何参数。
返回值: 该方法以毫秒为单位返回该日历的当前时间。
以下程序说明了日历类的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
参考资料: https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#getTimeInMillis-