Vincent
Vincent

Reputation: 68

Odd behavior of SimpleDateFormat

Code:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYYMMdd", Locale.US);
Object o = simpleDateFormat.parse("20111216");
System.out.println(simpleDateFormat.format((Date)o));

Output

20111226

Why? Giant bug?

Upvotes: 2

Views: 1280

Answers (3)

nilacqua
nilacqua

Reputation: 177

I think the greater question hinted by the op on this has been missed.

Why is it that sometimes 'Y' works despite the docs. Yes it should be 'y', but on some systems 'Y' works.

I developed on a mac where 'Y' worked and it blew up on prod Linux where 'Y' didn't work. Not hard to realize what's going on, but very annoying.

Upvotes: 0

NPE
NPE

Reputation: 500357

Once I fix the pattern (it should be yyyyMMdd with lowercase y), the code works as expected (it outputs 20111216).

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240900

it should be

yyyyMMdd

Note: small letter y

See Also

Upvotes: 4

Related Questions