Tag Archives: Date Format

[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

[Solved] Date Format : java.lang.IllegalArgumentException: Illegal pattern character ‘Z’ or ‘T’


SimpleDateFormat throw java.lang.IllegalArgumentException at runtime while parsing date format.

Follow link below to get in depth knowledge of IllegalArgumentException .

[Solved] java.lang.IllegalArgumentException: “ABC”

Example DateTime throwing IllegalArgumentexception

In this example target format is throwing IllegalArgumentException because of using illegal pattern character as Z and T.

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-ddThh:mm:ss.SSSZ";
		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
Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 'T'
    at java.text.SimpleDateFormat.compile(Unknown Source)
    at java.text.SimpleDateFormat.initialize(Unknown Source)
    at java.text.SimpleDateFormat.(Unknown Source)
    at java.text.SimpleDateFormat.(Unknown Source)
    at com.fiot.test.ChangeDateFormat.main(ChangeDateFormat.java:17)

Solutions

SimpleDateFormat allow only some key characters only while using date time formatting. Here using characters Z and T for matching date time pattern in sourceDate that what throwing IllegalArgumentException . To resolve such issues for using  characters apart from formatting char in date format always use with ‘Z’ or ‘T’ (with single quote) as given below. Follow link to see complete list for formatting characters and date format.

Date Time Formatting Characters and Patterns

Correct Date Time Format
"yyyy-MM-dd'T'hh:mm:ss.SSS'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
Target Date :02/43/2019 04:59:10 PM

More Issues Solution

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

Java Date and Time Handling Tutorial

 

[Solved] Date Format AM/PM : java.lang.IllegalArgumentException: Illegal pattern character ‘A’


SimpleDateFormat throw java.lang.IllegalArgumentException at runtime while parsing date format.

Follow link below to get in depth knowledge of IllegalArgumentException .

[Solved] java.lang.IllegalArgumentException: “ABC”

Example DateTime throwing IllegalArgumentexception

In this example target format is throwing IllegalArgumentException because of using invalid formatting char for display AM/PM.

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 AM/PM";
		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
Exception in thread "main" java.lang.IllegalArgumentException: Illegal pattern character 'A'
    at java.text.SimpleDateFormat.compile(Unknown Source)
    at java.text.SimpleDateFormat.initialize(Unknown Source)
    at java.text.SimpleDateFormat.(Unknown Source)
    at java.text.SimpleDateFormat.(Unknown Source)
    at com.fiot.test.ChangeDateFormat.main(ChangeDateFormat.java:19)

Solutions :

SimpleDateFormat allow only some key characters only while using date time formatting. Here for showing AM/PM should use character as as mentioned in below code in targetFormat. Follow link to see complete list for formatting characters and date format.

Date Time Formatting Characters and Patterns

Correct format to AM/PM
"MM/DD/YYYY hh:mm:ss a"

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
Target Date :02/43/2019 04:59:10 PM

More Issues Solution

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

Java Date and Time Handling Tutorial