📜  Groovy-日期和时间

📅  最后修改于: 2020-11-04 06:41:23             🧑  作者: Mango


Date类表示特定的时间瞬间,精度为毫秒。 Date类具有两个构造函数,如下所示。

日期()

句法

public Date()

参数-无。

返回值

分配一个Date对象并对其进行初始化,以便它表示分配给它的时间(以最近的毫秒为单位)。

以下是此方法的用法示例-

class Example { 
   static void main(String[] args) { 
      Date date = new Date(); 
      
      // display time and date using toString() 
      System.out.println(date.toString()); 
   } 
} 

当我们运行上面的程序时,我们将得到以下结果。以下输出将为您提供当前日期和时间-

Thu Dec 10 21:31:15 GST 2015

日期(长毫秒)

句法

public Date(long millisec)

参量

Millisec –自标准基准时间以来要指定的毫秒数。

返回值-分配一个Date对象,并将其初始化为表示自标准基准时间(即“纪元”)以来的指定毫秒数,即格林尼治标准时间1970年1月1日。

以下是此方法的用法示例-

class Example {
   static void main(String[] args) {
      Date date = new Date(100);
      
      // display time and date using toString()
      System.out.println(date.toString());
   } 
}

当我们运行上面的程序时,我们将得到以下结果-

Thu Jan 01 04:00:00 GST 1970

以下是Date类的给定方法。在Date类的所有接受或返回年,月,日,时,分和秒值的方法中,使用以下表示形式-

  • 年y由整数y-1900表示。

  • 一个月由0到11之间的整数表示; 0是一月,1是二月,依此类推;因此11月是12月。

  • 日期(一个月中的一天)通常以1到31之间的整数表示。

  • 一个小时用0到23的整数表示。因此,从午夜到凌晨1点的小时是0小时,从中午到下午1点的小时是12小时。

  • 分钟通常以0到59之间的整数表示。

  • 秒由0到61之间的整数表示。

Sr.No. Methods & Description
1 after()

Tests if this date is after the specified date.

2 equals()

Compares two dates for equality. The result is true if and only if the argument is not null and is a Date object that represents the same point in time, to the millisecond, as this object.

3 compareTo()

Compares two Dates for ordering.

4 toString()

Converts this Date object to a String

5 before()

Tests if this date is before the specified date.

6 getTime()

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

7 setTime()

Sets this Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT.