What is java.util.Date Class?
import java.util.Date;
What are the java.util.Date constructors?
The java.util.Date class primarily has two constructors as described below.Date()
The first java.util.Date constructor is Date(). It initializes the object with the current date and time.
Date date = new Date();
Here, we initialize a date variable of type Date with current data and time.
import java.util.Date;
public class Example {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);
}
}
Output
Date(long milliseconds)
This java.util.Date constructor creates a date object the equals the number of milliseconds that have elapsed since January 1, 1970, 00:00:00 GMT.
long ms = System.currentTimeMillis();
Date date = new Date(ms);
Here, we have initialized the date variable with the current date and time only after getting the exact milliseconds passed till now through System.currentTimeMillis(); and passing as an argument to the constructor.
import java.util.Date;
public class Example1 {
public static void main(String[] args) {
long ms = System.currentTimeMillis();
Date date = new Date(ms);
System.out.println(date);
}
}
Output
What are the java.util.Date methods
Following are the important java.util.Date methods.boolean after(Date date): returns true if this date is after the date that is passed as an argument.
boolean before(Date date): returns true if this date is before the date that is passed as an argument.
int compareTo(Date date): compares given date with the current date.
boolean equals(Date date): compares equality between current and given date. Returns true if they are the same.
long getTime(): returns the time that this date object represents.
void setTime(long time): changes the current time to the given time.
String toString(): converts this date into a String type object.
java.util.Date Example
import java.util.Date;
public class Example2 {
public static void main(String args[]) {
long ms = 900000000;
Date date1 = new Date(ms);
System.out.println("date1 : " + date1);
Date date2 = new Date();
System.out.println("date2 : " + date2);
boolean after = date2.after(date1);
System.out.println("Is date2 after date1 : " + after);
boolean before = date2.before(date1);
System.out.println("Is date2 before date1 : " + before);
}
}
Output
Explanation
In the above code, we have defined two Date variables date1 and date2. After that, we have used the date2.after(date1) and date2.before(date1) methods. The after() method returns true because date2 comes after date1. The before() method returns false because date2 does not come before date1.Conclusion
By the end of this post, we hope you have got yourself familiarized with the java.util.Date class in Java. Keep practicing for a deeper command of the concept. Till then, keep growing and keep shining!Deprecation of java.util.Date: What You Need to Know
You've learned about java.util.Date for basic date handling, but did you know these classes are considered outdated? Let’s dive into why and explore better solutions.
Why Are Date and Calendar Considered Outdated?
Java introduced java.util.Date and Calendar a long time ago. Over the years, developers ran into several issues. One big problem is their poor handling of time zones, leading to confusion and off-by-one errors. Another is that Date stores its internal data in ways that can be unintuitive (like the months being zero-indexed), making it easy to slip up. On top of that, these classes aren’t thread-safe, so using them in concurrent applications is risky without extra synchronization. These headaches are why Java’s design experts decided to create a new, more robust time API in Java 8.
Meet the java.time Package: A Modern Approach
If Date and Calendar feel clunky, you’ll be thrilled to meet the java.time package. Introduced in Java 8, it offers classes like LocalDate, LocalTime, LocalDateTime, and ZonedDateTime. These classes tackle all the headaches of the old APIs—time zone issues, thread safety, and more—while providing a clear, modern design.
For example, a LocalDate represents just a date without time or time zone information. If you need times, LocalDateTime has your back. Handling time zones? ZonedDateTime is your hero. And all these classes are immutable and thread-safe, so they’re perfect for modern applications.
Migrating from Date or Calendar to java.time
You might be wondering how to move your existing code that uses Date and Calendar to the new API. The good news is that Java provides some handy methods to help with that. Here’s a quick example:
// Using Instant to convert from java.util.Date
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
public class MigrationExample {
public static void main(String[] args) {
// Old Date
Date oldDate = new Date();
// Convert Date to Instant
Instant instant = oldDate.toInstant();
// Convert Instant to LocalDateTime
LocalDateTime newDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
System.out.println(\"Migrated Date to LocalDateTime: \" + newDateTime);
}
}
This snippet shows how to transition from Date to LocalDateTime. You create an Instant from the old Date, then convert it to a LocalDateTime using a specific time zone.
Got a Calendar object instead? Simply call calendar.toInstant() to obtain the Instant, then follow a similar path to LocalDateTime or other classes. If you need to keep track of time zone details, ZonedDateTime might be your best bet.
GO TO FULL VERSION