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

0 Comments

Please sign in to leave a comment.