1. Java String to Date
Sometimes, we may need to do Date Parse from a given string in java. This is useful when we want to perform any date related calculations. For example, if we want to find number of days between two dates, it will be easy to get result if variables are date type rather than string. In this example, we will see how to parse a java date from the string. We will also study about the DateTimeFormatter class.
2. Date from String Via LocalDate.parse
The parse
method of the LocalDate class can convert the string into a LocalDate object. Here, string which we pass to the parse method should be in valid date format. The example is below:

Explanation
- Here, we pass a string to the parse method. Also note that the passed-in string rightly represents the year, month, and day. For example, passing 2022-08-317 is not a valid date in string form. The
parse
method converts the string in date format, and we stored that in a LocalDate instance date. - In this code snippet, we print the date instance and date object will automatically converted into string form before getting printed.
3. DateTime from String via LocalDateTime.parse
In the last section, we saw how to parse date string as a date object. Now, we will use LocalDateTime.parse
to covert a string representation of a date & time into LocalDateTime object. The example is below:

Explanation
- The
parse
method of theLocalDateTime
takes a string which represents a date as well as time. The date and time are separated by the letter T. In the time side, the first one is the Hour, second one is Minute and last one is the Seconds. Variable datetime stores the converted result as Java’s LocalDateTime object. - As in our previous example, we print the converted result in the console window.
4. Specify the Format using DateTimeFormatter
We used parse method of the LocalDate in section 2 of this article. The parse method overload can also accept the format of the date by taking constant from the DateTimeFormatter. Now look at the example below:

Explanation
- The parse method is taking the string.
- Second parameter states the date in first parameter is in the
ISO_LOCAL_DATE
format. This means, the string is in the YYYY-MM-DD format stating the first four digits are for year (0000-9999) and second two digits after the hyphen represents the month and final two digits represents the day of the month. Month and day can have preceding zero when the digit is single. - Here, we print the date to the console window.
5. DateTimeFormatter ofPattern Method
The ofPattern
method of the DateTimeFormatter
class tells, in what format the DateTime is stored in the string. This method is useful to convert a date-time from a string in variety of format. Now, let us have a look at the below example:

Explanation
- We have a variable theDateAndTime which is storing the Date-time in string format. The
ofPattern
method of the DateTimeFormatter tells in what format the string is representing the date. - The call to
parse
method takes the string representation of the Date-Time and the instance of DateTimeFormatter. Using the formatter, the parse method will by-part the Date-time components and gives back the instance of LocalDateTime. We store that in a variablelocDateTime
. - Finally, we will print the converted LocalDateTime instance to the console window.
Note: Here, we just took an example of how formatter works. One can specify variety of formatting using ofPattern method of the DateTimeFormatter.
6. Code Reference & Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; public class JavaDateTimeExamples { public static void main(String[] args) { //Sample 01: Parse Date From a String LocalDate date = LocalDate.parse("2022-08-14"); System.out.println("The Date is : " + date ); //Sample 02: Parse DateTime From a String LocalDateTime datetime = LocalDateTime.parse("2022-12-31T14:50:15"); System.out.println("The Date-Time is : " + datetime ); //Sample 03: Use DateTimeFormatter LocalDate iso_lacle_dt = LocalDate.parse("2022-08-14", DateTimeFormatter.ISO_LOCAL_DATE); System.out.println("The Date is : " + iso_lacle_dt ); //Sample 04: Use DateTimeFormatter to know specific date format String theDateAndTime = "01-09-2019 10:15:53"; DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); LocalDateTime locDateTime = LocalDateTime.parse(theDateAndTime, dateTimeFormat); System.out.println("The Date-Time is : " + locDateTime ); } } |
Output

Categories: Java