Use Date in Java

Here’s a step-by-step tutorial on how to use Java’s Date class:

  1. Import the java.util.Date package: To use the Date class in your Java program, you must first import the java.util.Date package. This can be done by adding the following line at the top of your Java file:
import java.util.Date;
  1. Create a Date object: To create a Date object, you can use one of the Date class’s constructors. For example, to create a Date 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.

  1. 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 the getTime() 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();
  1. 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 the setTime() method to set the date and time value of a Date object:

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);
  1. 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 the Date object, you can use the following code:

Handle Date Format:

  • Create a SimpleDateFormat object: To specify a date format, you can create a SimpleDateFormat 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);
  1. 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 the SimpleDateFormat class. Here’s an example:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss z");
String formattedDate = sdf.format(currentDate);

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