- Groovy - Meta Object Programming
- Groovy - Template Engines
- Groovy - Unit Testing
- Groovy - Command Line
- Groovy - Builders
- Groovy - Database
- Groovy - DSLS
- Groovy - JSON
- Groovy - JMX
- Groovy - XML
- Groovy - Annotations
- Groovy - Closures
- Groovy - Traits
- Groovy - Generics
- Groovy - Object Oriented
- Groovy - Exception Handling
- Groovy - Regular Expressions
- Groovy - Dates & Times
- Groovy - Maps
- Groovy - Lists
- Groovy - Ranges
- Groovy - Strings
- Groovy - Numbers
- Groovy - Optionals
- Groovy - File I/O
- Groovy - Methods
- Groovy - Decision Making
- Groovy - Loops
- Groovy - Operators
- Groovy - Variables
- Groovy - Data Types
- Groovy - Basic Syntax
- Groovy - Environment
- Groovy - Overview
- Groovy - Home
Groovy Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Groovy - Dates & Times
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 | Tests if this date is after the specified date. |
2 | 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 | Compares two Dates for ordering. |
4 | Converts this Date object to a String |
5 | Tests if this date is before the specified date. |
6 | Returns the number of milpseconds since January 1, 1970, 00:00:00 GMT represented by this Date object. |
7 | Sets this Date object to represent a point in time that is time milpseconds after January 1, 1970 00:00:00 GMT. |