【发布时间】:2021-02-16 17:30:48
【问题描述】:
My Mongo Input Source data is 2020-04-14 00:00:00.0000000 (GMT-04:00),
Data Type String
Error: Caused by: java.time.format.DateTimeParseException: Text '' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949) ~[na:1.8.0_222]
我做到了:
Command.java:
DateTimeFormatter dateTimeformatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
List<RatingTiming> response = repo.findByserviceRequestedTimestampBetween(LocalDateTime.parse(query.getStartvalue(), dateTimeformatter), LocalDateTime.parse(query.getEndvalue(), dateTimeformatter));
Mogorepo.java:
public interface RatingResponseRepository extends MongoRepository<RatingTiming, String> {
List<RatingTiming> findByserviceRequestedTimestampBetween(LocalDateTime startDate, LocalDateTime endDate);
}
Json.java:
@JsonFormat
(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss'")
@JsonDeserialize(using = DateDeserializer.class)
@JsonProperty("serviceRequestedTimestamp")
public LocalDateTime serviceRequestedTimestamp;
【问题讨论】:
-
这能回答你的问题吗? Java String to DateTime
-
这能回答你的问题吗? java SimpleDateFormat
-
仅供参考:
2020-04-14 00:00:00.0000000 (GMT-04:00)!=2020-04-14 00:00:00.000Z因为它们有不同的时区。00:00 GMT-4与04:00 GMT+0又名04:00Z相同。 -
你可以像这样获得价值
2020-04-14T04:00:00Z:OffsetDateTime.parse("2020-04-14 00:00:00.0000000 (GMT-04:00)", DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSSS (OOOO)")).toInstant() -
您要求的格式
2020-04-14 00:00:00.000Z有点搞笑,因为它类似于 ISO 8601 而不是 ISO 8601。ISO 8601 标准要求在时间部分之前有一个T,所以@987654336 @。考虑是否更喜欢这个。