Date parse error java

[Solved] Date Format Error: java.text.parseexception: unparseable date

Why you are getting java.text.parseexception unparseable date?

You may have come across “java.text.parseexception: unparseable date” error message, when you try to parse the date string into another desired format. Don’t worry it’s a very common error message that users generally faced while parsing a date in java using SimpleDateFormat class.

There are mainly three reasons for this error message either your date format is wrong or you are using the wrong localization or the time zone is not correct. In this article, we are going to see all these three aspects of date parsing which may result in the “java.text.parseexception: unparseable error” message.

What is mean by java.text.parseexception unparseable date?

Let’s first try to understand the error message thoroughly. If you read the error message carefully it’s saying that SimpleDateFormat class is not able to parse the input date string in the desired format.

java.text.parseexception is a runtime exception that occurs when there is a mismatch in the Input Date String value and the Format specified for parsing.

Three Important elements you must consider while parsing a date

Before going further let’s first understand the following three important elements of parsing a date into the required format.

Date Format is very important while parsing the date string into another format. All elements of the date format are case-sensitive. A slight change in the format may result in errors or unexpected results. Refer following table to understand characters used to represent different elements of the date.

Day dd
Month MM
Year yyyy
Hour HH
Minute mm
Second ss
Читайте также:  Error verifying the account

Use the following syntax to specify the date format.

Localization is very important when using SimpleDateFormat class to parse the date format. If you don’t specify the locale then the constructor of the SimpleDateFormat class uses the default systems locale.

If your input date string is in English locale and if you tried to parse it without specifying localization on the system which is Non-English then it will result in the java.text.parseexception unparseable date error.

You can specify locale explicitly while defining the date format as shown below.

The correct time zone is also very critical while formatting a date in the desired format. The constructor of SimpleDateFormat class implicitly uses JVM default current time zone. JVM’s default current time zone can change based on the location of the user. If a thread of any other application changes the default time zone of JVM then it will result in an unexpected error or wrong output. So it’s advisable to specify Time Zone explicitly while formatting the date.

You can specify the required timezone while formatting a date as shown below.

Now we will see three different examples where you may get java.text.ParseException: Unparseable date error message.

Example 1: Invalid Input String Format



As you see in the above example, the Month of the Input date string is 3 characters (Feb) but in the input date format, a month is specified by 2 characters only (MM). That’s why we got an error message. If you use three characters (MMM) in the input date format then the code will execute without any error message.

Читайте также:  Any send error sending file

Modify code an the line number 18 as shown below:

Example 2: Invalid Locale


As you see in the above example at line number 18 instead of ENGLISH, ITALY localization is used which caused the error message. In Italian Aug is written as Agosto. If your input contains Italian characters like Agosto then only use ITALY as a locale. Here Aug is an English word hence you must use ENGLISH as a locale.

Modify code an the line number 18 as shown below:

Example 3: Invalid Time Zone



Add trailing Z represented as Time zone in the single quotes (‘Z’)

Modify line number 17 to as shown below:

Note: It may change the time based on the current time zone. To avoid this set required time zone, as explained above in the Time Zone section.


Оцените статью