Tag Archives: AM/PM

[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

JAVA : Generate 15 Minute Time Interval AM/PM


In this program you can get list of in time interval with fixed difference Ex: 15 minute in AM and PM format.

Example

import java.util.ArrayList;
import java.util.List;

public class GenerateTimeInterval {

public static void main(String[] args) {
	int interval = 15; //minutes interval
	List timesList=new ArrayList() ; // time array
	String []ap = {"AM", "PM"}; // AM-PM
	String timeFormat;
	for(int h=0;h<24;h++)
	{
	for(int m=0;m<60;)
	{
		if(h<12)
		{
		timeFormat=String.format("%02d:%02d %s", h,m,"AM");
		}
		else
		{
		timeFormat=String.format("%02d:%02d %s", h,m,"PM");
		}
		timesList.add(timeFormat);
		m=m+interval;
		}

	}
	//To match the time
	timeFormat=String.format("%02d:%02d %s", 0,0,"AM");
	timesList.add(timeFormat);
	//print timings
	for(String time : timesList)
	{
		System.out.println(time);
	}

	}
}

Output


00:00 AM
00:15 AM
00:30 AM
00:45 AM
01:00 AM
01:15 AM
01:30 AM
01:45 AM
02:00 AM
02:15 AM
02:30 AM
02:45 AM
03:00 AM
03:15 AM
03:30 AM
03:45 AM
04:00 AM
04:15 AM
04:30 AM
04:45 AM
05:00 AM
05:15 AM
05:30 AM
05:45 AM
06:00 AM
06:15 AM
06:30 AM
06:45 AM
07:00 AM
07:15 AM
07:30 AM
07:45 AM
08:00 AM
08:15 AM
08:30 AM
08:45 AM
09:00 AM
09:15 AM
09:30 AM
09:45 AM
10:00 AM
10:15 AM
10:30 AM
10:45 AM
11:00 AM
11:15 AM
11:30 AM
11:45 AM
12:00 PM
12:15 PM
12:30 PM
12:45 PM
13:00 PM
13:15 PM
13:30 PM
13:45 PM
14:00 PM
14:15 PM
14:30 PM
14:45 PM
15:00 PM
15:15 PM
15:30 PM
15:45 PM
16:00 PM
16:15 PM
16:30 PM
16:45 PM
17:00 PM
17:15 PM
17:30 PM
17:45 PM
18:00 PM
18:15 PM
18:30 PM
18:45 PM
19:00 PM
19:15 PM
19:30 PM
19:45 PM
20:00 PM
20:15 PM
20:30 PM
20:45 PM
21:00 PM
21:15 PM
21:30 PM
21:45 PM
22:00 PM
22:15 PM
22:30 PM
22:45 PM
23:00 PM
23:15 PM
23:30 PM
23:45 PM
00:00 AM