Back to: Java Tutorials
Here’s a step-by-step tutorial on how to use Java’s Date class:
- Import the
java.util.Datepackage: To use theDateclass in your Java program, you must first import thejava.util.Datepackage. This can be done by adding the following line at the top of your Java file:
import java.util.Date;
- Create a
Dateobject: To create aDateobject, you can use one of theDateclass’s constructors. For example, to create aDateobject 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
Dateobject, 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
Dateclass 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 aDateobject:
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 theDateobject, you can use the following code:
System.out.println(dateString);
Handle Date Format:
- Create a
SimpleDateFormatobject: To specify a date format, you can create aSimpleDateFormatobject 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
Dateclass uses the system’s default timezone to represent date and time values. However, you can also set a specific timezone by using theSimpleDateFormatclass. 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
