Java 日历 getTime()方法及示例

Java 日历 getTime()方法及示例

Calendar类中的 getTime() 方法用于返回一个类似于Date的对象,该对象由这个Calendar的时间值代表。

语法:

public final Date getTime()

参数: 该方法不接受任何参数。

返回值: 该方法返回日期,由该日历表示。

以下程序说明了日历类的getTime()方法的工作:

例子1:

// Java code to illustrate
// getTime() 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.getTime());
 
        // Adding few delay
        Thread.sleep(10000);
 
        // Creating another calendar
        Calendar calndr2 = Calendar.getInstance();
 
        // Displaying the upcoming time
        Date dt = calndr2.getTime();
        System.out.println("The upcoming"
                           + " time is: " + dt);
    }
}

输出

The Current Time is: Wed Feb 20 14:40:37 UTC 2019
The upcoming time is: Wed Feb 20 14:40:47 UTC 2019

例2:

// Java code to illustrate
// getTime() 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.getTime());
 
        // Adding few delay
        Thread.sleep(5000);
 
        // Creating another calendar
        Calendar calndr2 = Calendar.getInstance();
 
        // Displaying the upcoming time
        Date dt = calndr2.getTime();
        System.out.println("The upcoming"
                           + " time is: " + dt);
    }
}

输出

The Current Time is: Wed Feb 20 14:40:50 UTC 2019
The upcoming time is: Wed Feb 20 14:40:55 UTC 2019

参考资料: https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#getMaximum-int-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Java 日历