-1

My API application does not allowed this date time format with +0000 behind it

i have tried to format it by codes

import java.text.SimpleDateFormat

def pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
def input = "2019-02-28T02:54:32.123Z"
def date = new SimpleDateFormat(pattern).parse(input)

created(date)

but still getting result

...
    "created": "2019-02-28T09:54:32+0000",
...

how do i make the output in this format

...
    "created": "2019-02-28T09:54:32.123Z",
...
cfrick
  • 35,203
  • 6
  • 56
  • 68
user2201789
  • 1,083
  • 2
  • 20
  • 45
  • What is `created(date)` doing? – Daniel Dec 09 '19 at 23:25
  • i have jsonbuilder, then created is one of field in json. the output is json – user2201789 Dec 09 '19 at 23:34
  • Why would you want `2019-02-28T02:54:32.123Z` be converted to `2019-02-28T09:54:32.123Z`? Why should the time be changed by 7 hours, when both strings specify that they are in the UTC time zone, i.e. the *same* time zone? – Andreas Dec 10 '19 at 00:20

2 Answers2

0

With your updated comments explaining that you are producing JSON, I suggest this is probably a duplicate of Why does Date serialize for a JSON key differently than it does for a JSON value?

Basically you're trying to customize the output format for your date. If you want to do that, you can:

​import java.util.Date
import java.text.SimpleDateFormat 
import groovy.json.JsonBuilder

def pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" 
def input = "2019-02-28T02:54:32.123Z"   
def date = new SimpleDateFormat(pattern).parse(input)

// everything above here is from your original question

def pattern2 = "yyyy-MM-dd'T'HH:mm:ss'Z'" 
def sdf = new SimpleDateFormat(pattern2) 
sdf.setTimeZone(TimeZone.getTimeZone("GMT"))
def builder = new JsonBuilder()

builder {
  "created"(sdf.format(date)​)
}​
Daniel
  • 3,312
  • 1
  • 14
  • 31
-2

solved it finally

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
String date = sdf.format(new Date())
user2201789
  • 1,083
  • 2
  • 20
  • 45
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/24809803) – Chathuranga Chandrasekara Dec 10 '19 at 01:42
  • @ChathurangaChandrasekara user12158726 is the author of the original question and instead of requesting clarification from himself this is his/her self-answer. – karel Dec 10 '19 at 03:32