Home > Net >  How to modify a date to meet correct groovy standard?
How to modify a date to meet correct groovy standard?

Time:10-16

I am trying to parse the following date in Jenkins 2021-10-14T18:12:20.578 00:00 however, I am getting the error Unparseable date: "2020-01-01T10:10:20.578 00:00"

This is my code, not sure what I am doing wrong:

Date myDate= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse("2020-01-01T10:10:20.578 00:00");

EDIT:

Thanks to Kaus, I found that my date is not formatted properly and should be 2020-01-01T10:10:20.578GMT 00:00

I'm getting this date from some other files. I can replace with GMT as follow:

def myDate = "2020-01-01T10:10:20.578 00:00"
myDate = myDate.replaceAll("\\ ", "GMT\\ ")

How can I do the same thing if my date is "2020-01-01T10:10:20.578-06:00" The following is replacing every "-"

def myDate = "2020-01-01T10:10:20.578-06:00"
myDate = myDate.replaceAll("\\ ", "GMT\\ ").replaceAll("\\-", "GMT\\-")

Output: "2020GMT-01GMT-01T10:10:20.578GMT-06:00"

CodePudding user response:

Missing GMT there

Date myDate= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse("2020-01-01T10:10:20.578GMT 00:00");

CodePudding user response:

You can use OffsetDateTime

java.time.OffsetDateTime.parse('2021-10-14T18:12:20.578 00:00')

CodePudding user response:

Use X for ISO8601 time zone, instead of Z for RFC 822 time zone.

(from https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)

import java.text.SimpleDateFormat

Date myDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX")
    .parse("2020-01-01T10:10:20.578 00:00")
  • Related