Java 中 org.joda.time.DateTime 與 java.sql.Timestamp 之間的轉換
1. 概述
在 Java 中處理時間戳記是一項常見任務,它使我們能夠更有效地操作和顯示日期和時間信息,特別是在處理資料庫或全域應用程式時。用於處理時間戳記和時區的兩個基本類別是java.sql.Timestamp和org.joda.time.DateTime 。
在本教程中,我們將了解在org.joda.time.DateTime和java.sql.Timestamp之間進行轉換的各種方法.
2. 將java.sql.Timestamp轉換為org.joda.time.DateTime
首先,我們將研究將java.sql.Timestamp轉換為org.joda.time.DateTime的多種方法。
2.1.使用建構函數
將java.sql.Timestamp轉換為org.joda.time.DateTime的最簡單方法之一涉及使用建構子。在這個方法中,我們將使用getTime()方法來取得自 Unix 紀元以來的毫秒數,並將該值作為DateTime建構子的輸入提供。
讓我們看看如何使用getTime()方法:
DateTime convertToDateTimeUsingConstructor(Timestamp timestamp) {
return new DateTime(timestamp.getTime());
}
我們來看下面的測試程式碼:
public void givenTimestamp_whenUsingConstructor_thenConvertToDateTime() {
long currentTimeMillis = System.currentTimeMillis();
Timestamp timestamp = new Timestamp(currentTimeMillis);
DateTime expectedDateTime = new DateTime(currentTimeMillis);
DateTime convertedDateTime = DateTimeAndTimestampConverter.convertToDateTimeUsingConstructor(timestamp);
assertEquals(expectedDateTime, convertedDateTime);
}
2.2.使用Instant類別
將Instant類別視為 UTC 區域中的單一時刻最簡單的方法。如果我們將時間視為一條線, Instant代表線上的一點。
在底層, Instant類別只是計算相對於 1970 年 1 月 1 日 00:00:00 的標準 Unix 紀元時間的秒數和奈秒數。該時間點以 0 秒和 0 奈秒錶示,其他一切都只是相對於此的偏移量。
儲存相對於該特定時間點的秒數和奈秒數允許該類別儲存負偏移量和正偏移量。換句話說, Instant類別可以表示紀元時間之前和之後的時間。
讓我們看看如何使用Instant類別將Timestamp轉換為DateTime :
DateTime convertToDateTimeUsingInstant(Timestamp timestamp) {
Instant instant = timestamp.toInstant();
return new DateTime(instant.toEpochMilli());
}
在上面的方法中,我們使用Timestamp類別的toInstant()方法將提供的timestamp轉換為Instant ,它代表 UTC 時間軸上的時刻。然後,我們對Instant物件使用toEpcohMilli()方法來取得自 Unix 紀元以來的毫秒數。
讓我們使用系統目前的毫秒數來測試這個方法:
@Test
public void givenTimestamp_whenUsingInstant_thenConvertToDateTime() {
long currentTimeMillis = System.currentTimeMillis();
Timestamp timestamp = new Timestamp(currentTimeMillis);
DateTime expectedDateTime = new DateTime(currentTimeMillis);
DateTime convertedDateTime = DateTimeAndTimestampConverter.convertToDateTimeUsingInstant(timestamp);
assertEquals(expectedDateTime, convertedDateTime);
}
2.3.使用LocalDateTime類
java.timе套件是在 Java 8 中引入的,它提供了現代的日期和時間 API。 LоcalDatеTimе是該套件中的類別之一,它可以儲存和操作不同時區的資料和時間。我們來看看這個方法:
DateTime convertToDateTimeUsingLocalDateTime(Timestamp timestamp) {
LocalDateTime localDateTime = timestamp.toLocalDateTime();
return new DateTime(localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
}
Timestamp類別的toLocalDateTime()方法將Timestamp轉換為LocalDateTime,它表示不含時區資訊的日期和時間。我們來看看這個方法:
@Test
public void givenTimestamp_whenUsingLocalDateTime_thenConvertToDateTime() {
long currentTimeMillis = System.currentTimeMillis();
Timestamp timestamp = new Timestamp(currentTimeMillis);
DateTime expectedDateTime = new DateTime(currentTimeMillis);
DateTime convertedDateTime = DateTimeAndTimestampConverter.convertToDateTimeUsingLocalDateTime(timestamp);
assertEquals(expectedDateTime, convertedDateTime);
}
3.將org.joda.time.DateTime to java.sql.Timestamp
現在,我們將研究將org.joda.time.DateTime轉換為java.sql.Timestamp的多種方法。
3.1.使用建構函數
我們也可以使用Timestamp建構子將org.joda.time.DateTime轉換為java.sql.Timestamp.在這裡,我們將使用DateTime類別的getMillis()方法,該方法將傳回自 Unix 紀元以來的毫秒數,然後將該值提供給Timestamp建構子。
讓我們看看如何使用getMillis()方法:
Timestamp convertToTimestampUsingConstructor(DateTime dateTime) {
return new Timestamp(dateTime.getMillis());
}
現在,讓我們測試一下這種方法:
@Test
public void givenDateTime_whenUsingConstructor_thenConvertToTimestamp() {
long currentTimeMillis = System.currentTimeMillis();
DateTime dateTime = new DateTime(currentTimeMillis);
Timestamp expectedTimestamp = new Timestamp(currentTimeMillis);
Timestamp convertedTimestamp = DateTimeAndTimestampConverter.convertToTimestampUsingConstructor(dateTime);
assertEquals(expectedTimestamp, convertedTimestamp);
}
3.2.使用Instant類別
讓我們看看如何使用Instant類別將DateTime轉換為java.sql.Timestamp :
Timestamp convertToTimestampUsingInstant(DateTime dateTime) {
Instant instant = Instant.ofEpochMilli(dateTime.getMillis());
return Timestamp.from(instant);
}
在上面的方法中,我們透過提供自 Unix 紀元以來的毫秒數將提供的dateTime轉換為Instant 。之後,我們使用Timestamp'建構子從Instant物件取得Timestamp 。
我們來看下面的測試程式碼:
@Test
public void givenDateTime_whenUsingInstant_thenConvertToTimestamp() {
long currentTimeMillis = System.currentTimeMillis();
DateTime dateTime = new DateTime(currentTimeMillis);
Timestamp expectedTimestamp = new Timestamp(currentTimeMillis);
Timestamp convertedTimestamp = DateTimeAndTimestampConverter.convertToTimestampUsingInstant(dateTime);
assertEquals(expectedTimestamp, convertedTimestamp);
}
3.3.使用LocalDateTime類
讓我們使用LocalDateTime類別將DateTime轉換為java.sql.Timestamp:
Timestamp convertToTimestampUsingLocalDateTime(DateTime dateTime) {
Instant instant = Instant.ofEpochMilli(dateTime.getMillis());
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
return Timestamp.valueOf(localDateTime);
}
在上面的方法中,我們透過提供自 Unix 紀元以來的毫秒數將提供的dateTime轉換為Instant 。 **LocalDateTime**表示沒有時區的日期和時間。之後,我們使用ZoneId.systemDefault()檢索系統預設時區,以確保使用本地時區設定建立LocalDateTime 。
現在,讓我們執行測試:
@Test
public void givenDateTime_whenUsingLocalDateTime_thenConvertToTimestamp() {
long currentTimeMillis = System.currentTimeMillis();
DateTime dateTime = new DateTime(currentTimeMillis);
Timestamp expectedTimestamp = new Timestamp(currentTimeMillis);
Timestamp convertedTimestamp = DateTimeAndTimestampConverter.convertToTimestampUsingLocalDateTime(dateTime);
assertEquals(expectedTimestamp, convertedTimestamp);
}
4。
在這個快速教學中,我們學習如何在 Java 中將org.joda.time.DateTime轉換為java.sql.Timestamp類別。
與往常一樣,本文中使用的程式碼可以在 GitHub 上找到。