- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Date Class
Introduction
The Java Util Date class represents a specific instant in time, with millisecond precision.
Class declaration
Following is the declaration for java.util.Date class −
public class Date extends Object implements Serializable, Cloneable, Comparable<Date>
Class constructors
Sr.No. | Constructor & Description |
---|---|
1 | Date() This constructor allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond. |
2 | Date(long date) This constructor allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT. |
Class methods
Sr.No. | Method & Description |
---|---|
1 | boolean after(Date when)
This method tests if this date is after the specified date. |
2 | boolean before(Date when)
This method tests if this date is before the specified date. |
3 | Object clone()
This method return a copy of this object. |
4 | int compareTo(Date anotherDate)
This method compares two Dates for ordering. |
5 | boolean equals(Object obj)
This method compares two dates for equality. |
6 | static Date from​(Instant instant)
This method obtains an instance of Date from an Instant object. |
7 | long getTime()
This method returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object. |
8 | int hashCode()
This method returns a hash code value for this object. |
9 | void setTime(long time)
This method sets this Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT. |
10 | Instant toInstant()
This method converts this Date object to an Instant. |
11 | String toString()
This method converts this Date object to a String of the form. |
Methods inherited
This class inherits methods from the following classes −
- java.util.Object
Creating a Date instance of current date Example
This Java example demonstrates the from() method of Date class to get Date instance of current time.
package com.tutorialspoint; import java.time.Instant; // Import the Date package import java.util.Date; // Main public class public class DateDemo { public static void main(String[] args) { // create a date of current time Date date = Date.from(Instant.now()); // print the date instance System.out.println("Date: " + date.toString()); } }
Output
Let us compile and run the above program, this will produce the following result −
Date: Mon Apr 01 10:20:08 IST 2024
To Continue Learning Please Login
Login with Google