Site icon Automation Dojos

Convert String to Date Using SimpleDateFormat Class

Sometimes, you may need to convert a String to a java.util.Date object. For this, you need to use the SimpleDateFormat class. You need to specify the format in which the String date is present and invoke the parse method. The following code demonstrates how this can be done:

This code creates a SimpleDateFormat instance. Since the input date is in the yyyy-MM-dd format, the SimpleDateFormat is created using this format. The parse() method is then invoked with the actual String date. This returns a java.util.Date object corresponding to the String. So, this code prints the following output:

Fri Jun 12 00:00:00 IST 2015

You can check out the API documentation for SimpleDateFormat in order to understand the patterns that need to be specified for various date formats.

Exit mobile version