[Solved] Date Format : java.text.ParseException: Unparseable date: “yyyy-MM-ddThh:mm:ss.SSSZ”


SimpleDateFormat throw java.text.ParseException exception when passing date for parsing not match with formatting pattern.

java.text.ParseException is runtime unchecked exception which signals that an error has been reached unexpectedly while parsing.

Constructors

  • ParseException (String message, int errorOffset) : Constrcut a ParseException with the specified detail message and offset. A detail message is a String that describes this particular exception. where errorOffset : is the position where error occurred.

Methods

  • public int getErrorOffset() :Returns the position where the error was found.

Example SimpleDateFormat throw ParseException

In this example currentFormat is matching with date passed in sourceDate that’s what failing with parse exception.

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class ChangeDateFormat {

	public static void main(String[] args) {
		String targetFormat = "MM/DD/YYYY hh:mm:ss a";
		String currentFormat = "yyyy-MM-dd'T'hh:mm:ss'Z'";
		String sourceDate = "2019-02-12T11:29:10.761Z";
		System.out.println("Source Date :"+sourceDate);
		String timezone = "CDT";
		DateFormat srcDf = new SimpleDateFormat(currentFormat);
		srcDf.setTimeZone(TimeZone.getTimeZone(timezone));
		DateFormat destDf = new SimpleDateFormat(targetFormat);
		try {
			Date date = srcDf.parse(sourceDate);
			String targetDate = destDf.format(date);
			System.out.println("Target Date :"+targetDate);

		} catch (ParseException ex) {
			ex.printStackTrace();
		}

	}

}

Output:


Source Date :2019-02-12T11:29:10.761Z
java.text.ParseException: Unparseable date: "2019-02-12T11:29:10.761Z"
    at java.text.DateFormat.parse(Unknown Source)
    at com.fiot.test.ChangeDateFormat.main(ChangeDateFormat.java:21)

Solutions

To resolve this issue date format should be same as passing source date.

Correct Format for Date is yyyy-MM-dd’T’hh:mm.ss.SS’Z’

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class ChangeDateFormat {

	public static void main(String[] args) {
		String targetFormat = "MM/DD/YYYY hh:mm:ss a";
		String currentFormat = "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'";
		String sourceDate = "2019-02-12T11:29:10.761Z";
		System.out.println("Source Date :"+sourceDate);
		String timezone = "CDT";
		DateFormat srcDf = new SimpleDateFormat(currentFormat);
		srcDf.setTimeZone(TimeZone.getTimeZone(timezone));
		DateFormat destDf = new SimpleDateFormat(targetFormat);
		try {
			Date date = srcDf.parse(sourceDate);
			String targetDate = destDf.format(date);
			System.out.println("Target Date :"+targetDate);

		} catch (ParseException ex) {
			ex.printStackTrace();
		}

	}

}

Output:


Source Date :2019-02-12T11:29:10.761Z
java.text.ParseException: Unparseable date: "2019-02-12T11:29:10.761Z"
    at java.text.DateFormat.parse(Unknown Source)
    at com.fiot.test.ChangeDateFormat.main(ChangeDateFormat.java:21)

More Issues Solution

To solved Date Time same code , formatting  and issues solutions follow the link given below:

Java Date and Time Handling Tutorial

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s