Reputation: 180
I have to parse this two date
private static final String d1 = "2013-04-02T08:37:56Z";
private static final String d2 = "2013-04-02T10:37:56+02:00";
for the first one I'm using the pattern
"yyyy-MM-dd'T'HH:mm:ss'Z'"
and for the second one I'm using
"yyyy-MM-dd'T'HH:mm:ssXXX"
but I'm always getting
java.text.ParseException: Unparseable date: ....
here my test code:
private static final String d1 = "2013-04-02T08:37:56Z";
private static final String d2 = "2013-04-02T10:37:56+02:00";
private static final String DATE_FORMAT_WITH_TIMEZONE = "yyyy-MM-dd'T'HH:mm:ss'Z'";
private static final String DATE_FORMAT_WITH_TIMEZONE2 = "yyyy-MM-dd'T'HH:mm:ssXXX";
public static void main(String[] args) {
try {
SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT_WITH_TIMEZONE);
Date d = df.parse(d1);
System.out.println("d1 = " + d);
df = new SimpleDateFormat(DATE_FORMAT_WITH_TIMEZONE2);
df.setTimeZone(TimeZone.getTimeZone("Europe/Rome"));
Date dt = df.parse(d2);
System.out.println("d2 = " + dt);
} catch (ParseException ex) {
ex.printStackTrace();
}
}
What's wrong in my code?
Upvotes: 0
Views: 2351
Reputation: 3691
After converting your examples to Hex, it seems you are not using normal hyphen (0x2D
) but a similar character (0xC2AD
according to my notepad++ converter) called 'soft hyphen'.
Upvotes: 0
Reputation: 69460
Your DateFormat Definition is wrong:
private static final String DATE_FORMAT_WITH_TIMEZONE = "yyyyMMdd'T'HH:mm:ss'Z'";
private static final String DATE_FORMAT_WITH_TIMEZONE2 = "yyyyMMdd'T'HH:mm:ssXXX";
There are no minus sign in the string
UPDATE:
After you have updated your question, I think the -
sign is not the minus sign. It only look so. Replace this signs. and try again.
Upvotes: 4
Reputation: 9648
Change your Formatter
as follows.
private static final String DATE_FORMAT_WITH_TIMEZONE = "yyyyMMdd'T'HH:mm:ss'Z'";
private static final String DATE_FORMAT_WITH_TIMEZONE2 = "yyyyMMdd'T'HH:mm:ssXXX";
However, I would recommend using JodaTime
for this purpose. JodaTime has better functions. Using JodaTime you can do something like this:
DateTimeFormatter df = DateTimeFormat.forPattern("yyyyMMdd'T'HH:mm:ss'Z'");
DateTime dateTime = df.parseDateTime("2013-04-02T08:37:56Z");
Date date = dateTime.toDate();
Upvotes: 0