May 14, 2021 Groovy
Class Date represents a specific moment with millisecond accuracy. The Date class has two constructors, as shown below.
public Date()
Parameters
No.
Returns a value
Assign a Date object and initialize it so that it represents the allocated time, in the most recent milliseconds.
Here's an example of using 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 program above, we will get the following results.
The following output will give you the current date and time -
Thu Dec 10 21:31:15 GST 2015
public Date(long millisec)
Parameters
Milliseconds - The number of millisecconds because the standard base time is specified.
Return Value - Assign a Date object and initialize it to represent the number of milliseconds specified since the standard base time (called "the calendar", i.e., January 1, 1970, 00:00:00 GMT).
Here's an example of using 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 program above, we will get the following results -
Thu Jan 01 04:00:00 GST 1970
The following is a given method for the Date class.
In all methods of accepting or returning class Date for year, month, day, hour, minute, and second value, the following representation is used -
Year y is represented by the integer y-1900.
A month is represented by an integer of 0 to 11; 0 is January, 1 is February, etc. So November is December.
The date (the day in the month) is represented in the usual way by an integer of 1 to 31.
An hour is represented by an integer from 0 to 23. Therefore, the hour from midnight to 1 a.m. is 0 hours, while the hour from noon to 1 p.m. is 12 hours.
Minutes are represented by integers from 0 to 59 in the usual way.
The second is represented by an integer of 0 to 61.
Serial number | Method and description |
---|---|
1 |
after()
Test whether this date is after the specified date. |
2 |
equals()
Compare the equality of the two dates. When and only if the argument is not null, the result is true and is a Date object that represents the same point in time (milliseconds) as the object. |
3 |
compareTo()
Compare the order of the two dates. |
4 |
toString()
Convert this Date object to a string |
5 |
before()
Test whether this date is before the specified date. |
6 |
getTime()
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT, as represented by this Date object. |
7 |
setTime()
Set this Date object to represent a point in time, a millisecond after 00:00:00 GMT on January 1, 1970. |