java.util.Date class in Java
Java.lang.Date Class
The class Date represents a specific instant in time, with millisecond precision.
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; the values 60 and 61 occur only for leap seconds and even then only in Java implementations that actually track leap seconds correctly.
Creating Objects
// Creating a Date object with the current date and time
Date currentDate = new Date();
// Creating a Date object with specific date and time
// Date(year - 1900, month (0-11), day, hour, minute, second)
Date specificDate = new Date(121, 0, 1, 10, 30, 0);
// January 1, 2021 10:30:00
Date Class Methods
public boolean after(Object when)
Returns whether this Calendar represents a time after the time represented by the specified Object. This method is equivalent to: compareTo(when) > 0 if and only if when is a Calendar instance. Otherwise, the method returns false.
public boolean before(Object when)
Returns whether this Calendar represents a time before the time represented by the specified Object. This method is equivalent to: compareTo(when) < 0 if and only if when is a Calendar instance. Otherwise, the method returns false.
public int compareTo(Calendar anotherCalendar)
Compares the time values (millisecond offsets from the Epoch) represented by two Calendar objects.
Returns:the value 0 if the time represented by the argument is equal to the time represented by this Calendar; a value less than 0 if the time of this Calendar is before the time represented by the argument; and a value greater than 0 if the time of this Calendar is after the time represented by the argument.
Date Class Example
import java.util.Date;
public class DateExample {
public static void main(String[] args) {
// Creating a Date object with the current date and time
Date currentDate = new Date();
System.out.println("Current Date and Time: " + currentDate);
// Creating a Date object with a specific date and time (since Date is deprecated, we should use SimpleDateFormat to format the date properly)
Date specificDate = new Date(121, 0, 1);
System.out.println("Specific Date: " + specificDate);
// Getting and setting time in milliseconds
long currentTimeInMillis = currentDate.getTime();
System.out.println("Current Time in milliseconds: " + currentTimeInMillis);
// Example of deprecated methods that can be used to obtain the date and time
int year = currentDate.getYear() + 1900;
int month = currentDate.getMonth() + 1; // Month is zero-indexed, so we add 1
int day = currentDate.getDate();
int hours = currentDate.getHours();
int minutes = currentDate.getMinutes();
int seconds = currentDate.getSeconds();
System.out.println("Year: " + year);
System.out.println("Month: " + month);
System.out.println("Day of Month: " + day);
System.out.println("Hours: " + hours);
System.out.println("Minutes: " + minutes);
System.out.println("Seconds: " + seconds);
}
}
Output:
Current Date and Time: Mon Jul 08 17:13:25 UTC 2023
Specific Date: Fri Jan 01 00:00:00 UTC 2021
Current Time in milliseconds: 1720458805797
Year: 2024
Month: 7
Day of Month: 8
Hours: 17
Minutes: 13
Seconds: 25