0

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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Andrea
  • 190
  • 1
  • 20

3 Answers3

3

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.

Jens
  • 67,715
  • 15
  • 98
  • 113
  • Hi jens, i don't know why the minus was removed copying the code... but i have them... i edited and corrected it – Andrea Feb 01 '16 at 14:05
  • Hi Jens... that was... maybe copy and past from source to my code uses a character that was like the minus but not it. Thanks a lot – Andrea Feb 01 '16 at 14:21
  • 1
    Your [HYPHEN-MINUS](http://www.fileformat.info/info/unicode/char/002d/index.htm) characters may have been replaced with [HYPHEN](http://www.fileformat.info/info/unicode/char/2010/index.htm) or [en dash or em dash characters](https://en.m.wikipedia.org/wiki/Dash). Be sure to use only text editors with your programming, never word-processors or formatted email/messaging. – Basil Bourque Feb 01 '16 at 17:56
0

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();
user2004685
  • 9,548
  • 5
  • 37
  • 54
0

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'.

jhamon
  • 3,603
  • 4
  • 26
  • 37