How to get today's date in Groovy

时间:2015-07-28 16:19:03

标签: groovy

I am trying to get today's date in format of yyyyMMdd in groovy language.

I have tried to do this:

String oldDate = '20150702'
Date date = Date.parse( 'yyyyMMdd', oldDate )
String currentDate = date.format( 'yyyyMMdd' )

Output: 20150702

I am trying to get today's date however.

2 个答案:

答案 0 :(得分:51)

Then use:

new Date().format( 'yyyyMMdd' )

答案 1 :(得分:0)

@Opal的答案对我不起作用: groovy.lang.MissingMethodException: No signature of method: java.util.Date.format() is applicable for argument types: (String) values: [yyyy-MM-dd]

以下是对我有用的解决方案:

new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date()))