如何更改ResponseEntity的时间戳字段格式?

问题描述 投票:0回答:1

我的API使用org.springframework.http.ResponseEntity返回响应正文

我需要将时间戳字段格式更改为特定格式。

在我的application.properties文件中使用什么属性?

我试图通过网络找到它,但发现了其他第三方库的引用,而不是春天的引用。

而且如果我还可以用其他格式定义message字段,那可能很棒。

当前响应正文:

{
  "timestamp": "Oct 2, 2019 3:24:32 PM",
  "status": 200,
  "error": "OK",
  "message": "Initialization failed. cfgId doesn't exist",
  "path": "/a/b/c/d/init"
}

我不使用任何第三方库来返回json,它只是org.springframework.http.ResponseEntity

我正在寻找一个通用的解决方案,而不是每个领域。我想要一个application.properties值来修复它。

java spring format httpresponse
1个回答
-1
投票

也许您可以使用类似:

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
cal.setTime(sdf.parse("Mon Mar 14 16:02:37 GMT 2011"));// all done

注意:根据您的环境/要求设置Locale


另请参见>>

  • Javadoc
  • 我们的朋友Jigar Joshi回答了类似的问题-here
© www.soinside.com 2019 - 2024. All rights reserved.