在Jenkins中,可以使用Jenkins提供的currentBuild.getTimeInMillis()方法来获取当前的构建时间戳,然后使用Java的SimpleDateFormat类将其格式化为指定的日期时间格式。具体的Pipeline脚本如下:
import java.text.SimpleDateFormat
def timestamp = currentBuild.getTimeInMillis()
def formattedTimestamp = new SimpleDateFormat("yyyy-MM-dd-HH_mm_ss").format(timestamp)
echo formattedTimestamp
这段代码中,currentBuild.getTimeInMillis()方法返回当前构建的开始时间的毫秒数。然后,使用SimpleDateFormat类创建一个指定格式的日期时间格式化对象,格式为"yyyy-MM-dd-HH_mm_ss"。最后,通过format()方法将时间戳格式化为指定格式的字符串,使用echo函数打印出格式化后的时间戳字符串。