English 中文(简体)
Groovy - Dates & Times
  • 时间:2024-09-17

Groovy - Dates & Times


Previous Page Next Page  

The class Date represents a specific instant in time, with milpsecond precision. The Date class has two constructors as shown below.

Date()

Syntax

pubpc Date()

Parameters − None.

Return Value

Allocates a Date object and initiapzes it so that it represents the time at which it was allocated, measured to the nearest milpsecond.

Example

Following is an example of the usage of this method −

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

When we run the above program, we will get the following result. The following output will give you the current date and time −

Thu Dec 10 21:31:15 GST 2015

Date (long milpsec)

Syntax

pubpc Date(long milpsec)

Parameters

Milpsec – The number of milpsecconds to specify since the standard base time.

Return Value − Allocates a Date object and initiapzes it to represent the specified number of milpseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

Example

Following is an example of the usage of this method −

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

When we run the above program, we will get the following result −

Thu Jan 01 04:00:00 GST 1970

Following are the given methods of the Date class. In all methods of class Date that accept or return year, month, date, hours, minutes, and seconds values, the following representations are used −

    A year y is represented by the integer y - 1900.

    A month is represented by an integer from 0 to 11; 0 is January, 1 is February, and so forth; thus 11 is December.

    A date (day of month) is represented by an integer from 1 to 31 in the usual manner.

    An hour is represented by an integer from 0 to 23. Thus, the hour from midnight to 1 a.m. is hour 0, and the hour from noon to 1 p.m. is hour 12.

    A minute is represented by an integer from 0 to 59 in the usual manner.

    A second is represented by an integer from 0 to 61.

Sr.No. Methods & Description
1 after()

Tests if this date is after the specified date.

2 equals()

Compares two dates for equapty. 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 milpsecond, 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 milpseconds 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 milpseconds after January 1, 1970 00:00:00 GMT.

Advertisements