Convert String to Date in Java

Sometimes we need to Convert String to Date in Java. In this tutorial we will learn about how to convert String to Date in Java.

SimpleDateFormat is a concrete class for formatting and parsing dates. SimpleDateFormat class in Java provides a method parse() to parse the date in String format and format() method to convert Date in the required date format.

Syntax of parse() method:

This method parses text from the beginning of the given string to produce a date.

Parameters:
str – String which needs to be parsed/to be converted to date format.

Returns:
A Date parsed from the string.

Throws:
ParseException – if the specified string cannot be parsed.

Syntax of format() method:

this method formats a Date into a date/time string.

Parameters:
date – the time value to be formatted into a time string.

Returns:
the formatted time string.

Example:

Let’s see how to Convert String to Date with an example.

Let’s run this program and check the output.

Output:

In the above program, we have taken the date in string format and parsed it using parse() method and formatted in the required date format using format() method.

You may also learn about how to Convert String to Char Array in Java at tutorial Convert String to Char Array in Java.

Related posts

Leave a Comment