Forum Discussion

senthkum's avatar
5 years ago

Code to get the current date in yyyy-MM-dd format?

Hi Team,

I need the code to get the current  date in yyyy-MM-dd format and I need to minus with the date with a week / Month and Year to send the valid response. Can anyone give me the code, so I can use it in dispatch style (Script) to send the response. I want the response as "2020-02-24" format.


I used thebelow code but the output is not has excepted.

def today = new Date()
def tenDaysAgo = today - 10 today.format("yyyy-MM-dd") tenDaysAgo.format("yyyy-MM-dd")


Mon Feb 17 16:38:34 AEDT 2020:INFO:2020-02-17

Mon Feb 17 16:38:34 AEDT 2020:INFO:2020-02-07



Senthil. M 

  • nmrao's avatar
    5 years ago
    It is highly appreciated if you can update the thread after the suggestions.

6 Replies

  • nmrao's avatar
    Champion Level 3

    Use below: if you just need local date time.




    • senthkum's avatar

      Hi Mate,


      Thanks for sharing the code, but unfortunately it did not work as excepted. It's still giving the result in the below format. "Tue Feb 18 09:55:01 AEDT 2020:INFO:2020-02-18". But I need only the date with "yyyy-MM-dd" format, bcoz I need to send the response based on condition like

      1. current date - 7,

      2. current date - fortnight,

      3. current date - Month,

      4. current date - Year,



      Senthil. M

      • richie's avatar
        Community Hero
        Hi senthkum,

        nmrao gave you the correct code.

        When you use, that is writing the variable/property to your logging.

        So...when you state the value returned in your logging is "Tue Feb 18 09:55:01 AEDT 2020:INFO:2020-02-18", this is correct.

        The actual value for the logged variable/property is the value returned AFTER 'INFO:' in the response.

        So in this case the value returned when you executed your groovy script is '2020-02-18' which corresponds to 'yyyy-MM-dd' ehich is what you asked for. :)