Java 如何正确使用DateTimeFormatter将其传递到存储库以执行查询?
我一直在尝试使用DateTimeFormatter API将字符串转换为日期格式,并在存储库界面中查询它。但是,我的SearchController中不断出现错误,无法找出原因 搜索控制器:Java 如何正确使用DateTimeFormatter将其传递到存储库以执行查询?,java,spring,spring-mvc,spring-boot,datetime-format,Java,Spring,Spring Mvc,Spring Boot,Datetime Format,我一直在尝试使用DateTimeFormatter API将字符串转换为日期格式,并在存储库界面中查询它。但是,我的SearchController中不断出现错误,无法找出原因 搜索控制器: @GetMapping("/search") public String searchAccessInfoByDate(@RequestParam("startDate") String startDate, @RequestParam("endDate") String endDate, Mode
@GetMapping("/search")
public String searchAccessInfoByDate(@RequestParam("startDate") String startDate, @RequestParam("endDate") String endDate, Model model) {
DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-mm-dd");
LocalDateTime date1 = LocalDate.parse(startDate, dateTimeFormatter1).atTime(LocalTime.MIN);
DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-mm-dd");
LocalDateTime date2 = LocalDate.parse(endDate, dateTimeFormatter2).atTime(LocalTime.MAX);
List<AccessInfo> foundAccessInfos = accessInfoRepository.searchAccessInfoByDate(date1, date2);
model.addAttribute("foundAccessInfos", foundAccessInfos);
return "search";
}
html(显示搜索结果的位置):
标题
〜
美国石油学会エンドポイント
ステータスコード
アクセス回数[ns]
リクエスト平均時間
アクセスされた日付け
当前错误:
“无法解析文本'2018-09-30':无法从TemporalAccessor获取LocalDate:{Year=2018,MinuteOfHour=9,DayOfMonth=30},ISO类型为java.time.format.parsed”]}问题在于您的
DateTimeFormatter
模式无法解析您提供的日期。您需要使用DateTimeFormatter
模式,如下所示
DateTimeFormatter.ofPattern("yyyy-MM-dd")
有关模式的详细信息,请参阅
DateTimeFormatter
。如果您使用@Madhu Bhat的答案,请更改存储库类?to:1到startDate,2到endDate,我不知道您是否可以在jpa中使用?1和?2,但我有我的存储库,如下面的代码
@Repository
public interface AccessInfoRepository extends JpaRepository<AccessInfo, Long> {
@Query("select i from AccessInfo i where i.accessDate between :startDate and :endDate")
List<AccessInfo> searchAccessInfoByDate(LocalDateTime startDate, LocalDateTime endDate);
}
@存储库
公共接口访问信息存储库扩展了JPA存储库{
@查询(“从AccessInfo i中选择i,其中i.accessDate介于:startDate和:endDate之间”)
列出searchAccessInfoByDate(LocalDateTime开始日期、LocalDateTime结束日期);
}
希望这有帮助。您会遇到什么错误?你能提供它吗?你要传递的字符串日期是什么?@MaruthiAdithya“文本'2018-09-29'无法在索引4处解析“@MadhuBhat@MaruthiAdithya正确的模式是
yyyyy-MM-dd
而不是yyyy-MM-dd
hmmm我消除了错误,但现在查询没有返回任何结果。我在问题中编辑了我的代码,你能看一下吗?@Blaze这样你就不会再出现解析错误了,对吗?您所说的查询不返回任何内容是什么意思?查询的格式是否正确?您需要提供更多详细信息以找出问题所在。要解析yyyy-mm-dd格式的日期,您根本不需要格式化程序,因为此格式是(ISO 8601)标准。我建议LocalDateTime date1=LocalDate.parse(startDate.atStartOfDay()代码>(类似于结束日期)。
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<form method="get" action="/search" style="padding: 10px 0">
<div>
<input type="date" name="startDate" th:value="startDate">
〜
<input type="date" name="endDate" th:value="endDate">
<input type="submit" value="検索">
</div>
</form>
<table border="1" th:if="${foundAccessInfos}">
<tr>
<th>APIエンドポイント</th>
<th>ステータスコード</th>
<th>アクセス回数[ns]</th>
<th>リクエスト平均時間</th>
<th>アクセスされた日付け</th>
</tr>
</table>
DateTimeFormatter.ofPattern("yyyy-MM-dd")
@Repository
public interface AccessInfoRepository extends JpaRepository<AccessInfo, Long> {
@Query("select i from AccessInfo i where i.accessDate between :startDate and :endDate")
List<AccessInfo> searchAccessInfoByDate(LocalDateTime startDate, LocalDateTime endDate);
}