Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Date Parse From String in Java

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:

Fig 1. Date Parse From a String
Fig 1. Get LocalDate From a String

Explanation

  1. 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.
  2. 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:

Fig 2. Get LocalDateTime From a String
Fig 2. Get LocalDateTime From a String

Explanation

  1. The parse method of the LocalDateTime 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.
  2. 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:

Fig 3. Date parse using DateTimeFormatter
Fig 3. Get LocalDate From DateTimeFormatter

Explanation

  1. The parse method is taking the string.
  2. 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.
  3. 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:

Fig 4. Get LocalDateTime From DateTimeFormatter
Fig 4. Get LocalDateTime From DateTimeFormatter

Explanation

  1. 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.
  2. 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 variable locDateTime.
  3. 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

Output

Fig 5. DateTime Parsing Example - Output
Fig 5. DateTime Parsing Example – Output

Categories: Java

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.