使用 Groovy 将日期解析为“yyyy-MM-dd'T'00:00:00”

rAJ*_*rAJ 2 groovy datetime soapui datetime-format

我正在尝试将日期格式 '2017-12-18T20:41:06.136Z' 解析为“2017-12-18'T'00:00:00”

Date date = new Date();
def dateformat =  new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
dateformat.setTimeZone(TimeZone.getTimeZone(TimeZoneCode));
def currentDate = dateformat.format(date)
log.info "Current Date : " + currentDate
date1 =  new SimpleDateFormat("yyyy-MM-dd'T'00:00:00").parse(currentDate)
log.info "Current Date : " + date1
Run Code Online (Sandbox Code Playgroud)

显示错误:

java.text.ParseException:无法解析的日期:“2017-12-18T20:46:06:234Z”错误位于第:16行

这一行给出了错误:

date1 =  new SimpleDateFormat("yyyy-MM-dd'T'00:00:00").parse(currentDate)
Run Code Online (Sandbox Code Playgroud)

tim*_*tes 5

在 Java 8 上运行 Groovy 可以让您访问更好的日期/时间类...您可以这样做:

import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit

String result = ZonedDateTime.parse("2017-12-18T20:41:06.136Z")
    .truncatedTo(ChronoUnit.DAYS)
    .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
Run Code Online (Sandbox Code Playgroud)