3
votes

Sorry for bringing this up, but I don't see it. I have date+time in such format:

dd-MM-yy HH:mm

For example

15-11-11 09:36

(at the time of writing, it indicates date 5 days ago). When I try to run:

new java.text.SimpleDateFormat("dd-MM-yy HH:mm").parse("15-11-11 09:36")

I get exception:

java.text.ParseException: Unparseable date: "15-11-11 09:36"

At first I thought it is something with year (despite (-80,+20) boundary -- according to documentation). Well, maybe, but when I remove time (from string and pattern), the date is recognized correctly.

Does anyone see what I don't see?

Update

My system (Linux) short date format:

YYYY-MM-DD

long:

WEEKDAY DD MONTH YYYY

and time:

HH:MM:SS

However I thought the purpose of giving the pattern is to be locale independent.

Out of curiosity I added ":ss" to pattern and ":01" to string, still exception.

2
Works fine for me. What's your locale, although I'm not sure if that would matter. - Dave Newton
Same here, no exception. Could it possibly have anything to do with locales? - Vlad
Of course, you don't have to use SimpleDateFormat. You could split the string on non-digits. eg mystring.split("\\D"); - mike jones
@mike jones, sure but this would mean inventing a wheel each time I spot a problem with a library. - greenoldman

2 Answers

3
votes

I can just estimate but I think that you copy/pasted either format fragment or the date. And by mistake inserted different semicolon character in both cases. The characters look the same but are different.

So, I suggest you to take your working example without time and type manually HH:mm and 10:36. If it does not work, I am really sorry. :(. If it does try first to change 10 to 09. This is to be sure that it takes numbers with leading zeros.

2
votes

Try this

DateFormat df = new SimpleDateFormat("dd-MM-yy HH:mm");  

System.out.println("Formatted date: " + df.format(date));