How to convert String to Date in Java

The SimpleDateFormat class in Java, which belongs to the java.text package, is a powerful tool for formatting and parsing dates according to specified patterns. It provides a straightforward and flexible way to handle date and time values in various formats.

Formatting Dates

One of the key features of SimpleDateFormat is its ability to format dates. You can specify a pattern that defines how the date should be displayed. The pattern consists of a combination of specific characters that represent different elements of a date, such as the year, month, day, hour, minute, and second. For example, the pattern "yyyy-MM-dd" represents a date in the format "year-month-day", such as "2023-06-29".

Here are some common pattern characters used in SimpleDateFormat:

  1. "y" represents the year.
  2. "M" represents the month.
  3. "d" represents the day.
  4. "H" represents the hour in 24-hour format.
  5. "h" represents the hour in 12-hour format.
  6. "m" represents the minute.
  7. "s" represents the second.

Parsing Dates

In addition to formatting, SimpleDateFormat also allows you to parse dates. Parsing refers to the process of converting a date string into a Date object, which can be used for further manipulation or calculations. You specify a pattern that matches the structure of the date string you want to parse, and SimpleDateFormat converts it into a Date object.

String format = "dd/MM/yyyy"; SimpleDateFormat dateFormat = new SimpleDateFormat(format);

String to Date conversion

Example
import java.text.SimpleDateFormat; import java.util.Date; import java.text.ParseException; import java.text.DateFormat; public class TestClass{ public static void main(String[] args) { try{ String stringDate="01/12/1995"; Date date=new SimpleDateFormat("dd/MM/yyyy").parse(stringDate); System.out.println("Date is : "+date); }catch(Exception e){ System.out.println(e); } } }

Here's an extract of all available format patterns from the javadoc:


convert String to Date in Java

Convert different types of string to Date in java

import java.text.SimpleDateFormat; import java.util.Date; import java.text.ParseException; import java.text.DateFormat; public class TestClass{ public static void main(String[] args) { String stringDate = "03/08/1995"; String stringDate2 = "02-09-1995 23:37:50"; String stringDate3 = "02-May-1995"; String stringDate4 = "04 02, 1995"; String stringDate5 = "Thu, May 02 1995"; String stringDate6 = "Thu, May 02 1995 23:37:50"; DateFormat date = new SimpleDateFormat("dd/MM/yyyy"); DateFormat date2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); DateFormat date3 = new SimpleDateFormat("dd-MMM-yyyy"); DateFormat date4 = new SimpleDateFormat("MM dd, yyyy"); DateFormat date5 = new SimpleDateFormat("E, MMM dd yyyy"); DateFormat date6 = new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss"); try{ Date nDate1 = date.parse(stringDate); System.out.println(stringDate + " : " + date.format(nDate1)); Date nDate2 = date2.parse(stringDate2); System.out.println(stringDate2 + " : " + date2.format(nDate2)); Date nDate3 = date3.parse(stringDate3); System.out.println(stringDate3 + " : " + date3.format(nDate3)); Date nDate4 = date4.parse(stringDate4); System.out.println(stringDate4 + " : " + date4.format(nDate4)); Date nDate5 = date5.parse(stringDate5); System.out.println(stringDate5 + " : " + date5.format(nDate5)); Date nDate6 = date6.parse(stringDate6); System.out.println(stringDate6 + " : " + date6.format(nDate6)); }catch(Exception e){ System.out.println(e); } } }

Custom Patterns

One of the strengths of SimpleDateFormat is its flexibility to define custom patterns. You can create patterns that suit your specific requirements and match the desired date format. For instance, if you want to display the date as "June 29, 2023," you can create a custom pattern like "MMMM dd, yyyy".

Error Handling

When working with SimpleDateFormat, it's crucial to handle potential exceptions. ParseException is a checked exception that can be thrown when parsing a date string that doesn't match the specified pattern. It's important to handle this exception to manage invalid or unexpected date formats.

Thread Safety

It's worth noting that SimpleDateFormat is not thread-safe. If you plan to use SimpleDateFormat concurrently in a multi-threaded environment, you should either synchronize access to the SimpleDateFormat instance or consider using alternative thread-safe date formatting libraries like java.time.format.DateTimeFormatter introduced in Java 8.

Conclusion

SimpleDateFormat provides a convenient way to format and parse dates in Java. It allows you to control the display and interpretation of date and time values by defining patterns that align with your desired format.