无法将日期显示为 yyyy-MM-dd’T’HH:mm:ss:SSS Java

huangapple go评论85阅读模式
英文:

Cant display date as yyyy-MM-dd'T'HH:mm:ss:SSS Java

问题

以下是翻译好的部分:

我在尝试转换以下字符串时遇到了以下错误。我希望日期的格式为 yyyy-MM-dd'T'HH:mm:ss:SSS,但实际上日期似乎显示为 Sun Mar 01 23:00:01 GMT 2020

String FULL_ISO_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
SimpleDateFormat formatter = new SimpleDateFormat(FULL_ISO_DATE_FORMAT);
Date from = formatter.parse("2020-03-01T23:00:01.000");

错误信息

feign.FeignException: 状态码 400,在 Controller#searchController(Date,Date,Integer,String) 读取内容时出错:
{"status":"fail","data":{"errors":[{"type":"IllegalArgumentException","description":"无效值 Sun Mar 01 23:00:01 GMT 2020 用于过滤器 from。字段必须符合格式:yyyy-MM-dd'T'HH:mm:ss.SSS"}]}}

任何帮助将不胜感激。我需要使用 Date 对象,因为我正在查询的构造函数使用 Date 对象。理想情况下,我想使用 LocalDateTime,但我不能。

英文:

Im getting the following error when I try to convert the following string. Id like the Date to be in the format yyyy-MM-dd'T'HH:mm:ss:SSS but instead the Date seems to be coming out as Sun Mar 01 23:00:01 GMT 2020

    String FULL_ISO_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
    SimpleDateFormat formatter = new SimpleDateFormat(FULL_ISO_DATE_FORMAT);
    Date from = formatter.parse("2020-03-01T23:00:01.000");

Error

feign.FeignException: status 400 reading Controller#searchController(Date,Date,Integer,String); content:
{"status":"fail","data":{"errors":[{"type":"IllegalArgumentException","description":"Invalid value Sun Mar 01 23:00:01 GMT 2020 for filter from. Field needs to be in format: yyyy-MM-dd'T'HH:mm:ss.SSS"}]}]}

Any help would be appreciated. I need to use the Date object as the constructor Im querying is using the Date object. Ideally I'd like to use LocalDateTime but I cant.

答案1

得分: 1

使用来自Java 8 日期时间APILocalDateTime,停止使用旧的Date

String FULL_ISO_DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime dateTime = LocalDateTime.now();
dateTime.format(FULL_ISO_DATE_FORMAT);
英文:

Use the LocalDateTime from java-8 date-time API and stop using legacy Date classes

String FULL_ISO_DATE_FORMAT = DateTimeFormatter. ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime dateTime = LocalDateTime.now();
dateTime.format(FULL_ISO_DATE_FORMAT);

答案2

得分: 1

请不要使用旧的类DateSimpleDateFormat。使用更为强大和设计更合理的新的java.time API。


您可以按以下方式执行相同的操作:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime date = LocalDateTime.parse("2020-03-01T23:00:01.000", formatter);

请注意,您可以按以下方式将其转换为Date以实现兼容性:

Date legacyDate = Date.from(date.atZone(ZoneId.systemDefault()).toInstant());
英文:

Please don't use the old classes Date and SimpleDateFormat. Use the new java.time api that is much more robust and better designed.


You can do the same thing as follows:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime date = LocalDateTime.parse("2020-03-01T23:00:01.000", formatter);

Keep in mind that you can convert it to Date for compatibility like so:

Date legacyDate = Date.from(date.atZone(ZoneId.systemDefault()).toInstant());

huangapple
  • 本文由 发表于 2020年3月17日 02:41:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/60711554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定