Java offers several methods to convert string to date, each with its own unique advantages.
In this comprehensive guide, we will explore different approaches to converting strings to dates in Java, complete with code examples and explanations.
By the end of this tutorial, you will have a thorough understanding of how to perform this essential task in various ways.

1. SimpleDateFormat

A SimpleDateFormat object allows you to format and parse dates using a pattern of special characters.
You can use it to convert a string to a date in Java with precision and flexibility.

To create an object of SimpleDateFormat, use its constructor that takes the format of the date to be parsed.
A string can be converted to java.util.Date object with its parse() method.

String dateString = "2022-10-15";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = formatter.parse(dateString);
System.out.println(date);
InputOutput
“Mar 22, 2023”Thu Mar 22 00:00:00 GMT 2023

Tips for Handling Common Issues

Common issues when using SimpleDateFormat include handling of time zones, ensuring thread safety, and catching ParseException.
It’s important to be aware of these issues to avoid unexpected errors in date conversion.

String dateString = "2019-02-29";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
formatter.setLenient(false);
Date date;
try {
    date = formatter.parse(dateString);
    System.out.println(date);
} catch (ParseException e) {
    System.out.println("Invalid date");
}
  • Always handle ParseException when parsing dates
  • Set leniency to false to enforce strict date validation
  • Be cautious of thread safety when using SimpleDateFormat
  • After parsing, handle the returned Date object accordingly

After parsing the string into a Date object, you can perform further operations such as date comparison, manipulation, or formatting, depending on your requirements.
It’s important to understand the nuances and best practices of using SimpleDateFormat to ensure accurate and reliable date conversions in Java.

String dateString = "2022-05-18 13:30:00";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = formatter.parse(dateString);
System.out.println(date);

2. LocalDate.parse()

To convert a string to date, you can also use java 8 LocalDate class and its parse() method.
This method allows you to parse a string representation of a date into a LocalDate object using the predefined format.

String dateStr = "2022-05-30";
LocalDate date = LocalDate.parse(dateStr);

Converting Strings with Custom Patterns

Any string that follows a specific pattern can be converted to a LocalDate using the parse() method with a DateTimeFormatter.

By providing a custom pattern to the DateTimeFormatter, you can parse strings that deviate from the standard ISO format.

String dateStr = "30-05-2022";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate date = LocalDate.parse(dateStr, formatter);
System.out.println(date); // Output: 2022-05-30

3. DateFormat.parse()

For converting a string to a date in Java, the DateFormat.parse() method is a useful tool.
This method parses the input string according to the formatting pattern specified by a DateFormat object, and returns a Date object.

String dateString = "2022-10-25";
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse(dateString);

Leveraging the DateFormat Class

To convert a string to a date using the DateFormat.parse method, you first need to create a DateFormat object with the desired date format pattern.
This pattern should match the format of the input string, such as “yyyy-MM-dd” for a string in the form of “2022-10-25”. Once the DateFormat object is created, you can use its parse() method to parse the input string and obtain a Date object.

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse("2022-10-25");

4. Joda-Time library

Joda-Time is a widely used library for date and time manipulation in Java. It provides a rich set of tools for parsing and formatting date and time objects.
To convert a string to a date using Joda-Time, you can use the DateTimeFormatter class to define a specific date format and then parse the string into a DateTime object using this format.

String dateString = "2023-04-15";
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
DateTime date = formatter.parseDateTime(dateString);
System.out.println(date);

5. Apache Commons Lang

Incorporating Apache Commons Lang for string to date conversion is another popular choice.
The DateUtils class in Apache Commons Lang provides various methods for parsing strings into Date objects, allowing for flexibility in handling different date formats.
This library is especially convenient when dealing with date manipulation in Java.

String dateString = "2023-04-15";
Date date = DateUtils.parseDate(
                  dateString, new String[] {"yyyy-MM-dd"}
            );
System.out.println(date);

Conclusion

Presently, we have explored various methods to convert a string to a date in Java.
We have learned about using the SimpleDateFormat class, the DateTimeFormatter class, and the LocalDate class to achieve this.
Apart from these, we also learned about third party libraries such as Joda-Time and Apache Commons Lang.