Java 简明教程

Java Date Class

Introduction

Java Util Date 类表示一个具体的时间点,精确到毫秒。

The Java Util Date class represents a specific instant in time, with millisecond precision.

Class declaration

以下是 java.util.Date 类的声明:

Following is the declaration for java.util.Date class −

public class Date
   extends Object
   implements Serializable, Cloneable, Comparable<Date>

Class constructors

Class methods

Methods inherited

此类从以下类中继承方法:

This class inherits methods from the following classes −

  1. java.util.Object

Creating a Date instance of current date Example

Java 示例演示了 Date class 的 from() 方法以获取当前时间的 Date 实例。

This Java example demonstrates the from() method of Date class to get Date instance of current time.

package com.tutorialspoint;

import java.time.Instant;

// Import the Date package
import java.util.Date;

// Main public class
public class DateDemo {
   public static void main(String[] args) {

      // create a date of current time
      Date date = Date.from(Instant.now());

      // print the date instance
      System.out.println("Date: " + date.toString());
   }
}

Output

让我们编译并运行上述程序,这将生成以下结果 −

Let us compile and run the above program, this will produce the following result −

Date: Mon Apr 01 10:20:08 IST 2024