Handle Date Format in Java

Step-by-step tutorial on how to handle date formats in Java:

  1. Import the java.util.Date and java.text.SimpleDateFormat packages: To handle date formats in Java, you need to import the java.util.Date package to create a Date object, and the java.text.SimpleDateFormat package to format the date. You can do this by adding the following lines at the top of your Java file:
import java.util.Date;
import java.text.SimpleDateFormat;
  1. Create a Date object: To handle the date format of a specific date, you need to create a Date object that represents that date. You can do this by using 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. Create a SimpleDateFormat object: To handle the date format of a Date object, you need to create a SimpleDateFormat object that defines the format you want to use. For example, to format the date as “dd/MM/yyyy HH:mm:ss”, you can use the following code:
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

This will create a new SimpleDateFormat object that uses the specified format string.

  1. Format the Date object: To format the Date object using the SimpleDateFormat object, you can use the format() method. For example, to format the currentDate object using the dateFormat object, you can use the following code:
String formattedDate = dateFormat.format(currentDate);

This will create a new String object that contains the formatted date value.

  1. Parse a String to a Date object: To parse a String that represents a date to a Date object, you can use the parse() method of the SimpleDateFormat class. For example, to parse the String “12/02/2023 12:34:56” to a Date object using the dateFormat object, you can use the following code:
String dateString = "12/02/2023 12:34:56";
Date parsedDate = dateFormat.parse(dateString);

This will create a new Date object that represents the parsed date value.

  1. Handle exceptions: When parsing a String to a Date object, it’s important to handle exceptions that may occur. For example, if the String doesn’t match the expected format, a ParseException will be thrown. You can handle this exception using a try-catch block. Here’s an example:
String dateString = "12/02/23 12:34:56";
try {
    Date parsedDate = dateFormat.parse(dateString);
} catch (ParseException e) {
    e.printStackTrace();
}

In this example, if the String “12/02/23 12:34:56” is parsed using the dateFormat object, a ParseException will be thrown because the year is represented with only two digits instead of four. The catch block will handle this exception and print the stack trace to the console.

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