[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.

java.text.parseexception: unparseable date

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

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.

Daydd
MonthMM
Yearyyyy
HourHH
Minutemm
Secondss

Use the following syntax to specify the date format.

SimpleDateFormat dateFormat = new SimpleDateFormat("your-date-format");

Example:

SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-YYYY HH:mm:ss");
  • Locale

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.

SimpleDateFormat dateFormat = new SimpleDateFormat("your-date-format", Locale.English)
  • Time Zone

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.

 dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

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

Example 1: Invalid Input String Format

package com.technolads;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
 
public class ChangeDateFormat {
 
    public static void main(String[] args) 
    {
    	try 
    	{
    		// Input Date String
            String inputDateString = "Feb 25 02:10:36 2021";
    		
            // Input Date String Format
            SimpleDateFormat inputDateFormat = new SimpleDateFormat("MM dd HH:mm:ss yyyy", Locale.ENGLISH);
        	
        	Date inputDate = inputDateFormat.parse(inputDateString);
        	
            //Required output date string Format
        	SimpleDateFormat outputDateFormat = new SimpleDateFormat("dd MMMMMM yyyy HH:mm:ss", Locale.ENGLISH);
        	
			String outputDateString = outputDateFormat.format(inputDate);
			
			System.out.println("inputDateString: "+inputDateString);
        	System.out.println("outputDateString: " +outputDateString);
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}

Output:

java.text.ParseException: Unparseable date: "Feb 25 02:10:36 2021"
	at java.base/java.text.DateFormat.parse(DateFormat.java:399)
	at com.technolads.ChangeDateFormat.main(ChangeDateFormat.java:20)

Solution:

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.

Modify code an the line number 18 as shown below:

SimpleDateFormat inputDateFormat = new SimpleDateFormat("MMM dd HH:mm:ss yyyy", Locale.ENGLISH);

Example 2: Invalid Locale

package com.technolads;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
 
public class ChangeDateFormat {
 
    public static void main(String[] args) 
    {
    	try 
    	{
    		// Input Date String
            String inputDateString = "Aug 25 02:10:36 2021";
    		
            // Input Date String Format
            SimpleDateFormat inputDateFormat = new SimpleDateFormat("MMM dd HH:mm:ss yyyy", Locale.ITALY); //Wrong Localization 
        	
        	Date inputDate = inputDateFormat.parse(inputDateString);
        	
            //Required output date string Format
        	SimpleDateFormat outputDateFormat = new SimpleDateFormat("dd MMMMMM yyyy HH:mm:ss", Locale.ENGLISH);
        	
			String outputDateString = outputDateFormat.format(inputDate);
			
			System.out.println("inputDateString: "+inputDateString);
        	System.out.println("outputDateString: " +outputDateString);
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}

Output:

java.text.ParseException: Unparseable date: "Aug 25 02:10:36 2021"
	at java.base/java.text.DateFormat.parse(DateFormat.java:399)
	at com.technolads.ChangeDateFormat.main(ChangeDateFormat.java:20)

Solution:

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:

SimpleDateFormat inputDateFormat = new SimpleDateFormat("MMM dd HH:mm:ss yyyy", Locale.ENGLISH);

Example 3: Invalid Time Zone

package com.technolads;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
 
public class ChangeDateFormat {
 
    public static void main(String[] args) 
    {
    	try 
    	{
    		// Input Date String
    		
            String inputDateString = "2021-08-25T09:18:25.758Z";
    		
            // Input Date String Format
            SimpleDateFormat inputDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        	 
        	Date inputDate = inputDateFormat.parse(inputDateString);
        	
            //Required output date string Format
        	SimpleDateFormat outputDateFormat = new SimpleDateFormat("dd MMMMMM yyyy HH:mm:ss", Locale.ENGLISH);
        	
			String outputDateString = outputDateFormat.format(inputDate);
			
			System.out.println("inputDateString: "+inputDateString);
        	System.out.println("outputDateString: " +outputDateString);
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}

Output:

java.text.ParseException: Unparseable date: "2021-08-25T09:18:25.758Z"
	at java.base/java.text.DateFormat.parse(DateFormat.java:399)
	at com.technolads.ChangeDateFormat.main(ChangeDateFormat.java:22)

Solution:

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

Modify line number 17 to as shown below:

SimpleDateFormat inputDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

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.


Conclusion:

We hope that the above methods helped you resolve the “java.text.parseexception: unparseable date” error message. If you are still facing the issue then please do mention it in the comment section or you can reach out to us using Contact Form.

Leave a Comment