Back to: Java Tutorials
Here’s a step-by-step tutorial on how to use Java’s Date
class:
- Import the
java.util.Date
package: To use theDate
class in your Java program, you must first import thejava.util.Date
package. This can be done by adding the following line at the top of your Java file:
import java.util.Date;
- Create a
Date
object: To create aDate
object, you can use one of theDate
class’s constructors. For example, to create aDate
object representing the current date and time, you can use the following code:
Date currentDate = new Date();
This will create a new Date
object that represents the current date and time.
- Get the date and time values: Once you have a
Date
object, you can use available various methods to get the date and time values. For example, to get the number of milliseconds since the Unix epoch, you can use thegetTime()
method, as shown below:
long milliseconds = currentDate.getTime();
You can also use the toString()
method to get a string representation of the date and time value:
String dateString = currentDate.toString();
- Manipulate the date and time values: The
Date
class also provides several methods to manipulate the date and time values. For example, you can use thesetTime()
method to set the date and time value of aDate
object:
currentDate.setTime(milliseconds);
You can also use the before()
and after()
methods to compare Date
objects:
Date otherDate = new Date();
boolean isBefore = currentDate.before(otherDate);
boolean isAfter = currentDate.after(otherDate);
- Display the date and time values: To display the date and time values, you can use Java’s
System.out.println()
method, which prints the value to the console. For example, to print the string representation of theDate
object, you can use the following code:
System.out.println(dateString);
Handle Date Format:
- Create a
SimpleDateFormat
object: To specify a date format, you can create aSimpleDateFormat
object and specify the format using a format string. Here’s an example:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
In this example, the format string “dd/MM/yyyy” represents a date format in the form of “day/month/year”.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String formattedDate = sdf.format(currentDate);
System.out.println(formattedDate);
- Handle timezones: By default, the
Date
class uses the system’s default timezone to represent date and time values. However, you can also set a specific timezone by using theSimpleDateFormat
class. Here’s an example:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss z");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
String formattedDate = sdf.format(currentDate);
System.out.println(formattedDate);
In this example, we have created a SimpleDateFormat
object with a specific format string that includes the timezone ("z"
). We then set the timezone to “GMT” using the setTimeZone()
method. Finally, we format the Date
object using the format()
method of the SimpleDateFormat
class.
Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.
Follow us on social media
Follow Author