How to use Date with Pipeline?

Issue

How do I use Java Date in Pipeline jobs?

Environment

  • CloudBees Jenkins Enterprise
  • Pipeline
  • java.util.Date

Resolution

Simple example:

node {
    def date = new Date()

    println date
}

Formatted date example:

import java.text.SimpleDateFormat

node {
    def dateFormat = new SimpleDateFormat("yyyyMMddHHmm")
    def date = new Date()

    println(dateFormat.format(date))
}

NOTE: You might need to approve this with Script Approval ( JENKINS_URL/scriptApproval) to allow usage of Date (if you get RejectedAccessException)

Reference:

Have more questions? Submit a request

2 Comments

  • 0
    Avatar
    Zillayt .

    v2.126

     

    I get the following exception when using this. I've upgraded Groovy plugin to v2.0. I also see Script Security Plugin v1.44 installed.

    I don't see any scripts needing approval shown in Manage Jenkins->In-process Script Approval section.

    ERROR: org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new java.util.Date java.lang.String
    Edited by Zillayt .
  • 0
    Avatar
    Denys Digtiar

    It might depend on the groovy feature that you use to get the Date. Please review the discussion on JENKINS-27725 and let us know if it helps you resolve the issue.

Please sign in to leave a comment.