我有一個即時欄位并嘗試使用以下方法驗證該欄位中的值:
Instant.parse("2021-09-29 09:35:07.531")
但是,它會引發錯誤:
java.time.format.DateTimeParseException: 無法在索引 10 處決議文本“2021-09-29 09:35:07.531”。
那么,如何測驗Instant字串格式的給定日期是否為有效Instant日期?
我正在實施匯入功能,我需要檢查日期單元格。因此,日期通常以即時格式保存在 db 中,因此我需要檢查單元格日期值是否為有效的即時日期。所以,如果有更合適的方法,我當然可以遵循那個方法。
我不使用該值,我只需要檢查日期是否為有效日期。
uj5u.com熱心網友回復:
如果您嘗試使用Instant該類,則必須參考時區。所以,讓我們試試這個:
LocalDateTime.from(DateTimeFormatter.ofPattern("yyyy-MM-dd H:mm:ss.SSS").parse("2021-09-29 09:35:07.351")).atZone(ZoneId.systemDefault()).toInstant();
uj5u.com熱心網友回復:
您的日期時間字串沒有時區資訊,因此您無法在Instant不引入時區的情況下將其決議為。LocalDateTime如果應該獨立于時區使用它,我建議您將其決議為并將其用于資料庫作業。
演示:
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String args[]) {
DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);
LocalDateTime ldt = LocalDateTime.parse("2021-09-29 09:35:07.531", dtfInput);
System.out.println(ldt);
}
}
輸出:
2021-09-29T09:35:07.531
ONLINE DEMO
如何LocalDateTime在JDBC中使用?
下面給出了將 a 插入LocalDateTime到columnfoo(TIMESTAMP型別)的示例代碼:
PreparedStatement st = conn.prepareStatement("INSERT INTO mytable (columnfoo) VALUES (?)");
st.setObject(1, ldt);
st.executeUpdate();
st.close();
下面給出的是LocalDateTime從 中檢索 a 的示例代碼columnfoo:
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM mytable WHERE <some condition>");
while (rs.next()) {
// Assuming the column index of columnfoo is 1
LocalDateTime ldt = rs.getObject(1, LocalDateTime.class));
System.out.println(ldt);
}
rs.close();
st.close();
如果您想將給定的日期時間字串決議為Instant:
如上所述,您需要引入一個時區才能將其決議為Instant.
演示:
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String args[]) {
// Change the ZoneId to the applicable one e.g. ZoneId.of("Etc/UTC")
DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS", Locale.ENGLISH)
.withZone(ZoneId.systemDefault());
Instant instant = Instant.from(dtfInput.parse("2021-09-29 09:35:07.531"));
System.out.println(instant);
}
}
輸出在我的時區,歐洲/倫敦:
2021-09-29T08:35:07.531Z
ONLINE DEMO
從Trail: Date Time 中了解有關現代日期時間 API * 的更多資訊。
* 如果您正在為 Android 專案作業并且您的 Android API 級別仍然不符合 Java-8,請檢查通過 desugaring 可用的 Java 8 API。請注意,Android 8.0 Oreo 已提供對java.time.
uj5u.com熱心網友回復:
那么,如何測驗
Instant字串格式的給定日期是否為有效Instant日期?
它不是。
瞬間是一個(唯一的)時間點。您的字串包含日期和時間。在不知道時區的情況下,這可能表示 24 或 27 小時范圍內的某個點 - 遠不是一個時間點。
編輯:我知道您是從某處匯入字串,您無法決定字串的格式或內容,您需要對其進行驗證。您可以將其驗證為日期和時間。您基本上無法將其轉換為Instant,或者至少您只能在我不知道其有效性的假設下。為了驗證,我建議使用此格式化程式:
private static final DateTimeFormatter PARSER = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral(' ')
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.toFormatter(Locale.ROOT);
格式化程式重用內置格式化程式并在秒內接受可變數量的小數,我認為這在許多情況下都有意義。你更清楚它是否適用于你的。
像這樣驗證:
String importedDateTimeString = "2021-09-29 09:35:07.531";
try {
LocalDateTime.parse(importedDateTimeString, PARSER);
System.out.format("Valid date and time: %s%n", importedDateTimeString);
} catch (DateTimeParseException dtpe) {
System.out.format("Not a valid date and time: %s. Validation error: %s%n",
importedDateTimeString, dtpe);
}
輸出:
有效日期和時間:2021-09-29 09:35:07.531
原始建議:因此,請改用包含與 UTC 偏移量的格式。尤其是 UTC 中的瞬間 ISO 8601 格式是一個推薦選項,用于多種用途,例如2021-09-29T01:35:07.531Z.
鏈接:維基百科文章:ISO 8601
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/313670.html
