In Java, the conversion of a string representation of a date to a java.util.Date object is a common task. In this article, we’ll go over the different methods of converting a string to a date and provide code examples for each.

1. SimpleDateFormat

The SimpleDateFormat class is a format class that is used to parse and format dates. It takes a string pattern that represents the format of the string representation of a date. To convert a string to a date, the parse method can be used.

String dateString = "2022-01-01";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(dateString);

2. java.time.LocalDate

Java 8 introduced the new java.time package, which includes the LocalDate class. This class provides a more intuitive and convenient way to convert a string to a date. The LocalDate class has a static parse method that takes a string representation of a date and a DateTimeFormatter object, which is used to specify the format of the string.

String dateString = "2022-01-01";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date = LocalDate.parse(dateString, formatter);

3. Joda-Time

Joda-Time is a popular third-party library for working with dates and times in Java. It provides a DateTime class that can be used to parse a string representation of a date and time.

String dateString = "2022-01-01";
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd");
DateTime date = formatter.parseDateTime(dateString);

4. java.time.format.DateTimeFormatter

Another option in Java 8’s java.time package is the DateTimeFormatter class. This class provides a static method ofPattern which returns a formatter based on the specified pattern string. The parse method can then be used on the formatter to convert the string representation of a date to a java.time.LocalDate or java.time.LocalDateTime object.

String dateString = "2022-01-01";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date = LocalDate.parse(dateString, formatter);

5. java.sql.Date

If you need to convert a string to a java.sql.Date object, you can use the java.sql.Date class’s constructor that takes a long value representing the number of milliseconds since January 1, 1970, 00:00:00 GMT. The java.util.Date class can be used to parse the string representation of a date and then its getTime method can be used to get the number of milliseconds, which can then be passed to the java.sql.Date constructor.

String dateString = "2022-01-01";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date utilDate = sdf.parse(dateString);
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

6. Java Calendar

The java.util.Calendar class can also be used to convert a string representation of a date to a java.util.Date object. The getInstance method can be used to get a calendar instance, and then the setTime method can be used to set the calendar’s time to the parsed date. Finally, the getTime method can be used to get the java.util.Date object.

String dateString = "2022-01-01";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(dateString);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Date newDate = calendar.getTime();

Conclusion

In this article, we’ve gone over three methods of converting a string to a date in Java. The SimpleDateFormat class and the java.time.LocalDate class are included in the Java SE library, while Joda-Time is a third-party library that can be used for more advanced date and time operations. No matter which method you choose, it’s important to make sure that the string representation of the date is in the correct format and that the appropriate date format is specified when converting the string to a date.

Leave a Reply