Tran Tam
Tran Tam

Reputation: 699

parse string to date with given information

I knew that there some issues like this in this page but I didn't find my answer, please help me solve this. I got:

params.month = JANUARY
params.year = 2014
params.date = 10

I tried

def parse = new Date(2014, JANUARY, 10)
print parse(format(yyyy/MM/dd))

result:

2014/01/10

but When i tried

def parse = new Date(params.year.toInteger(), params.month.toInteger(), params.date.toInteger())

it can't parse params.month.toInteger()

Upvotes: 0

Views: 59

Answers (2)

saw303
saw303

Reputation: 9082

Don't use deprecated constructors such as Date(year, month, day)

I recommend to use the Groovy JDK method parse in order to create a Date instance.

def yourDateInstance = Date.parse('yyyy/MM/dd, "${params.year}/${params.month}/${params.date}")

Upvotes: 2

Ashok_Pradhan
Ashok_Pradhan

Reputation: 1169

try this

def parse = new Date(2014, 0 , 10)

NOTE: In java Months start with 0 ,0=JANUARY

And Date constructor has signature Date(int year,int month,int date) ,this is the cause of the exception as JANUARY is a string so it can't be parsed to int

Upvotes: 0

Related Questions