Date
Published Jun 21, 2023
Contribute to Docs
The Date
class represents a specific point in time, down to milliseconds. It provides methods to manipulate and format dates, as well as perform arithmetic and comparison operations. Internally the class uses a base or reference date of January 1, 1970, 00:00:00 GMT (also known as the Unix epoch).
Example
import java.util.Date;public class DateExample {public static void main(String[] args) {Date currentDate = new Date();long currentTimeMillis = currentDate.getTime();System.out.println("Current date and time: " + currentDate);System.out.println("Milliseconds since Unix epoch: " + currentTimeMillis);}}
The output for the above code will resemble the following, with values for the current date:
Current date and time: Wed Jun 16 12:34:56 GMT 2023Milliseconds since Unix epoch: 1678450496000
In the above code, the Date
class is imported from the java.util
package. Inside the main method, currentDate
is assigned the current date and time. Next, the .getTime()
method is used to return the number of milliseconds since the epoch. Finally, both values are logged to the console.
Methods
Here are some important aspects and methods of the Date
class:
- Creating a
Date
object:Date()
: Creates an object representing the current date and time.Date(long millis)
: Creates aDate
object with the specified number of milliseconds since January 1, 1970, 00:00:00 GMT (the Unix epoch).
- Getting and setting date components:
.getTime()
: Returns the number of milliseconds since the Unix epoch..setTime(long time)
: Sets the time using the specified number of milliseconds.
- Formatting and parsing dates:
.toString()
: Returns a string representation. The default format is not very readable or localized.SimpleDateFormat
class (from java.text package): Allows formatting and parsing of dates using patterns.
- Comparing dates:
.before(Date when)
: Checks if the given date is before the time specified..after(Date when)
: Checks if the given date is after the time specified..compareTo(Date anotherDate)
: Returns a value based on the relative order of two dates.
Date
- .after()
- Checks if a date occurs after a specified date.
- .before()
- Checks if one date occurs before another.
- .clone()
- Clones the specified date.
- .compareTo()
- Compares two date objects.
- .equals()
- Returns a boolean based on a comparison of equivalency between two dates.
- .from()
- Returns an instant object of a given Date.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.