Vamsi Emani
Vamsi Emani

Reputation: 10312

Groovy date parse issue

date.parse() method of groovy detects date DD and year yyyy correctly but is unable to detect the month as mmm.. As in

println new Date().parse("DD-MMM-yyyy", '22-MAR-2011')

yields output as

Sat Jan 22 00:00:00 GMT+05:30 2011

Why is the month march as MAR picked up as Jan? What can I do to make it detect the month in mmm format?

Upvotes: 1

Views: 1392

Answers (1)

Liviu T.
Liviu T.

Reputation: 23674

The problem is actualy that you are using DD - that means day in year

Correct way:

println new Date().parse("dd-MMM-yyyy", '22-MAR-2011')

Quick tip when formatting dates try using the reverse and see what comes out:

println new Date().format("dd-MMM-yyyy")

Groovy uses SimpleDateFormat under the hood but that's not that important since most date libraries use the same format conventions.

Upvotes: 4

Related Questions