[英]JPA Entity not having the column from the table but query based on the non existing column in the entity
[英]Query a Table based on the column as datetime2(7) from JPA
我需要根據 MS SQL 服務器中定義為 datetime2(7) 的列查詢給定日期范圍之間的記錄列表。
從 HTTP 請求中,我將收到 startDate 和 endDate 如下。
http://{host:port}/api/records/date?startDate=**2021-05-31T14:12:44.8020000**&endDate=**2021-05-31T14:12:44.8020000**
在數據庫中,lastUpdated 列中的值存儲為2021-05-31 14:12:44.8020000
我正在嘗試將傳入的查詢參數(字符串)轉換為 java.sql.Date 在代碼中,如下所示
@Override
public Page<Entity> getAllRecordsWithDateRange(String startDate, String endDate) {
Page<Entity> recordsWithinDateRange = null;
String time = startDate;
String formatIn = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS";
String formatOut = "yyyy-MM-dd HH:mm:ss.SSSSSS";
SimpleDateFormat in = new SimpleDateFormat(formatIn);
SimpleDateFormat out = new SimpleDateFormat(formatOut);
Date dateIn = null;
try {
dateIn = in.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
String valueOut = out.format(dateIn);
System.out.println(">>> " + valueOut);
Pageable page = PageRequest.of(0,5000);
Date date1= null;
try {
date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").parse(valueOut);
java.util.Date utilDate = date1;
recordsWithinDateRange = repo.getAllRecordsBetweenDates(utilDate,utilDate,page);
} catch (ParseException e) {
e.printStackTrace();
}
return recordsWithinDateRange;
}
我在這里看到的問題是我的實際輸入日期是2021-05-31T14:12:44.8020000但是,在轉換之后,它會增加到另一個時間2021-05-31 16:26:24.000000 。 因此,查詢沒有從數據庫返回任何記錄。
有人可以幫我解決這個問題嗎? 蒂亞!
java.util
日期時間 API 及其格式 API、 SimpleDateFormat
已過時且容易出錯。 建議完全停止使用它們並切換到現代日期時間 API * 。
使用java.time
的解決方案, 現代日期時間 API * :
datetime2
映射到TIMESTAMP
ANSI SQL 類型或 JDBC 中的LocalDateTime
。
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("u-M-d'T'H:m:s.SSSSSSS", Locale.ENGLISH);
DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSSS", Locale.ENGLISH);
String strDateTime = "2021-05-31T14:12:44.8020000";
LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtfInput);
System.out.println(ldt.format(dtfOutput));
}
}
Output:
2021-05-31 14:12:44.8020000
檢查此答案以了解如何使用LocalDateTime
執行 JDBC 操作。
使用舊版 API 的解決方案:
SimpleDateFormat
無法正確處理超過毫秒精度的小數秒。 檢查此答案以了解更多信息。
您需要將日期時間字符串截斷為毫秒精度。
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Main {
public static void main(String[] args) throws ParseException {
String strDateTime = "2021-05-31T14:12:44.8020000";
strDateTime = strDateTime.substring(0, strDateTime.indexOf('.') + 4);
String formatIn = "yyyy-MM-dd'T'HH:mm:ss.SSS";
String formatOut = "yyyy-MM-dd HH:mm:ss.SSS";
SimpleDateFormat in = new SimpleDateFormat(formatIn);
SimpleDateFormat out = new SimpleDateFormat(formatOut);
System.out.println(out.format(in.parse(strDateTime)));
}
}
Output:
2021-05-31 14:12:44.802
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API級別仍然不符合 Java-8,請檢查Java 8+ API 可通過脫糖和如何在 Android 項目中使用 ThreeTenABP 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.