- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Date Class in Java
Java provides the Date class available in java. util package, this class encapsulates the current date and time.
The Date class supports two constructors as shown in the following table.
Sr.No. | Constructor & Description |
---|---|
1 | Date( ) This constructor initializes the object with the current date and time. |
2 | Date(long millisec) This constructor accepts an argument that equals the number of milliseconds that have elapsed since midnight, January 1, 1970. |
Following are the methods of the date class.
Sr.No. | Method & Description |
---|---|
1 | boolean after(Date date) Returns true if the invoking Date object contains a date that is later than the one specified by date, otherwise, it returns false. |
2 | boolean before(Date date) Returns true if the invoking Date object contains a date that is earlier than the one specified by date, otherwise, it returns false. |
3 | Object clone( ) Duplicates the invoking Date object. |
4 | int compareTo(Date date) Compares the value of the invoking object with that of date. Returns 0 if the values are equal. Returns a negative value if the invoking object is earlier than date. Returns a positive value if the invoking object is later than date. |
5 | int compareTo(Object obj) Operates identically to compareTo(Date) if obj is of class Date. Otherwise, it throws a ClassCastException. |
6 | boolean equals(Object date) Returns true if the invoking Date object contains the same time and date as the one specified by date, otherwise, it returns false. |
7 | long getTime( ) Returns the number of milliseconds that have elapsed since January 1, 1970. |
8 | int hashCode( ) Returns a hash code for the invoking object. |
9 | void setTime(long time) Sets the time and date as specified by time, which represents an elapsed time in milliseconds from midnight, January 1, 1970. |
10 | String toString( ) Converts the invoking Date object into a string and returns the result. |
Getting Current Date and Time
This is a very easy method to get current date and time in Java. You can use a simple Date object with toString() method to print the current date and time as follows −
Example
import java.util.Date; public class DateDemo { public static void main(String args[]) { // Instantiate a Date object Date date = new Date(); // display time and date using toString() System.out.println(date.toString()); } }
This will produce the following result −
Output
on May 04 09:51:52 CDT 2009
Date Comparison
Following are the three ways to compare two dates −
You can use getTime( ) to obtain the number of milliseconds that have elapsed since midnight, January 1, 1970, for both objects and then compare these two values.
You can use the methods before( ), after( ), and equals( ). Because the 12th of the month comes before the 18th, for example, new Date(99, 2, 12).before(new Date (99, 2, 18)) returns true.
You can use the compareTo( ) method, which is defined by the Comparable interface and implemented by Date.
- Related Articles
- How to print date using GregorianCalendar class in Java?
- Increment a Date using the Java Calendar Class
- Create a Date object using the Calendar class in Java
- Date Class in C#
- Long style format for Date with Java MessageFormat class
- Short Length format for Date with Java MessageFormat class
- Medium length style format for Date with Java MessageFormat class
- Java Program to decrement a Date using the Calendar Class
- How to set local date/time in a table using LocalDateTime class in Java?
- How to get local date in android using local date API class?
- How to get offsetdatetime local date in android using offset date time API class?
- How to parse date sting to date in Java?
- What is the class "class" in Java?
- How to use the SimpleDateFormat class to convert a Java Date to a formatted String?
- Unix date format in Java
