Java Parse String to Date

Java Parse String to Date


Sometimes, it is also needed to convert String to Date; we have seen earlier how to convert Date to String. Now Parse String to Date is done.

Following code gets you the conversion of string to date. We use SimpleDateFormat class method parse().

Example on Parse String to Date


Parse String to Date
Output screenshot on Java Parse String to Date

In Date to String conversion, we used format() method of SimpleDateFormat. Now it is parse() method.

String stringDate = “12/06/2013”;
SimpleDateFormat sdf1 = new SimpleDateFormat(“dd/MM/yyyy”);
Date today = sdf1.parse(stringDate);

The date is available in string form as stringDate. Create a SimpleDateFormat object, sdf1, while passing the date format "dd/MM/yyyy" as parameter. Use parse() method of SimpleDateFormat and pass the string date object as parameter. The parse method parses string date into java.util.Date object.

See the output screen. The date object prints including minutes and seconds etc. which we may do not require. Use getDate(), getMonth() and getYear() methods of Date class to extract the information you require from Date object.

Would you like to know the conversion of Date to String also?

Leave a Reply

Your email address will not be published. Required fields are marked *