我的API应用程序不允许使用此日期时间格式,后跟+0000
我试图通过代码对其进行格式化
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)
但仍能得到结果
...
"created": "2019-02-28T09:54:32+0000",
...
我如何以这种格式输出
...
"created": "2019-02-28T09:54:32.123Z",
...
答案 0 :(得分:0)
更新后的注释说明您正在生成JSON,我建议这可能是Why does Date serialize for a JSON key differently than it does for a JSON value?的副本
基本上,您正在尝试自定义日期的输出格式。如果要这样做,可以:
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))
}
答案 1 :(得分:-2)
终于解决了
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
String date = sdf.format(new Date())