Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown pattern character 'x', when using SimpleDateFormat

Im trying to format a Date to String using SimpleDateFormat, and the pattern im using is this one

"yyyy-MM-dd'T'HH:mm:ss.SSSxxx"

but when reach this line

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSxxx");

i get the following exception:

java.lang.IllegalArgumentException: Unknown pattern character 'x' at java.text.SimpleDateFormat.validatePatternCharacter(SimpleDateFormat.java:323) at java.text.SimpleDateFormat.validatePattern(SimpleDateFormat.java:312) at java.text.SimpleDateFormat.(SimpleDateFormat.java:365) at java.text.SimpleDateFormat.(SimpleDateFormat.java:258)

the format im trying to achieve is "2017-06-16T12:19:59.001+02:00"

according to the documentation this pattern should work Whats wrong?

EDIT To clarify, i tried with xxx and XXX

in case of XXX i get java.lang.IllegalArgumentException: Unknown pattern character 'X'

like image 393
Thought Avatar asked Jun 16 '17 11:06

Thought


People also ask

What is the format of SimpleDateFormat?

SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. It allows for formatting (date -> text), parsing (text -> date), and normalization. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.

What can I use instead of SimpleDateFormat?

DateTimeFormatter is a replacement for the old SimpleDateFormat that is thread-safe and provides additional functionality.

How do I declare SimpleDateFormat?

Creating a SimpleDateFormat You create a SimpleDateFormat instance like this: String pattern = "yyyy-MM-dd"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); The pattern parameter passed to the SimpleDateFormat constructor is the pattern to use for parsing and formatting of dates.

Is SimpleDateFormat deprecated?

Class SimpleDateFormat. Deprecated. A class for parsing and formatting dates with a given pattern, compatible with the Java 6 API.


2 Answers

See Update below.

Unfortunately, regarding uppercase X, the documentation [was at the time of the question and this answer] wrong. Since the documentation seems to have changed significantly regarding x and X, I'll go ahead and state here that right now, it says X (uppercase) is supported (since API level 1), but x (lowercase) is not mentioned at all. The docs used to not mention X either.

A check of the Android source code (see validateFormat()) shows that only the letters GyMdkHmsSEDFwWahKzZLc are recognized in that version, despite the docs' claim that X has been supported since API level 1. This explains why you're getting the IllegalArgumentException: Unknown pattern character 'X'.

See this bug report for historical details.

Meanwhile, you'll have to find a workaround, which will vary depending on what kind of input you need to parse. E.g. the OP's answer.

Update: X is available only from Nougat+.

The documentation has now been fixed to add a "Supported (API Levels)" column, which indicates that X is only supported starting from API level 24. Presumably the OP's IllegalArgumentException was due to testing the app on a pre-24 device, since the docs didn't say anything about supported API levels before.

like image 145
LarsH Avatar answered Oct 15 '22 00:10

LarsH


I believe I found the answer in an issue at GitHub:

You are right, Android uses ZZZZZ instead to generate time zone like +01:00 (like XXX in Java). For now, could you try using the ApiClient#setDatetimeFormat method to customize the datetime format to make it work in Android?

Taking the petstore sample as an example:

// import io.swagger.client.Configuration;
// import io.swagger.client.ApiClient;
// import java.text.SimpleDateFormat;

// Customize for the default ApiClient
Configuration.getDefaultApiClient().setDatetimeFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"));

// Customize for a new ApiClient
ApiClient apiClient = new ApiClient();
apiClient.setDatetimeFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"));
// Use the new ApiClient
PetApi api = new PetApi(apiClient);
api.getPetById(new Long(1));

Apparently, I have to use:

yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ  
like image 31
Thought Avatar answered Oct 14 '22 22:10

Thought