-
Notifications
You must be signed in to change notification settings - Fork 54.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-7614 sql.Timestamp to java.util.Calendar conversion
- Loading branch information
1 parent
05fc667
commit bcd4556
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
...ons-4/src/main/java/com/baeldung/timestamptocalendar/SqlTimestampToCalendarConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.baeldung.timestamptocalendar; | ||
|
||
import java.sql.Timestamp; | ||
import java.time.Instant; | ||
import java.util.Calendar; | ||
|
||
public class SqlTimestampToCalendarConverter { | ||
|
||
public static void main(String... args) { | ||
Timestamp timestamp = new Timestamp(124, 3, 19, 9, 30, 0, 801789562); | ||
Calendar calendar = timestampToCalendar(timestamp); | ||
System.out.println(calendar.getTime()); | ||
Instant instant = timestampToInstant(timestamp); | ||
System.out.println(instant); | ||
} | ||
|
||
public static Calendar timestampToCalendar(Timestamp timestamp) { | ||
Calendar calendar = Calendar.getInstance(); | ||
calendar.setTimeInMillis(timestamp.getTime()); | ||
return calendar; | ||
} | ||
|
||
public static Instant timestampToInstant(Timestamp timestamp) { | ||
return timestamp.toInstant(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...4/src/test/java/com/baeldung/timestamptocalendar/SqlTimestampToCalendarConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.baeldung.timestamptocalendar; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.sql.Timestamp; | ||
import java.time.Instant; | ||
import java.time.ZoneId; | ||
import java.util.Calendar; | ||
|
||
import org.junit.Test; | ||
|
||
public class SqlTimestampToCalendarConverterTest { | ||
|
||
@Test | ||
public void whenConvert_thenEqualMillis() { | ||
Timestamp timestamp = new Timestamp(124, 3, 19, 9, 30, 0, 801789562); | ||
Calendar calendar = Calendar.getInstance(); | ||
calendar.setTimeInMillis(timestamp.getTime()); | ||
assertEquals(calendar.getTimeInMillis(), timestamp.getTime()); | ||
System.out.println(timestamp);// 2024-04-19 09:30:00.801789562 | ||
timestamp.setTime(calendar.getTimeInMillis()); | ||
System.out.println(timestamp);// 2024-04-19 09:30:00.801 | ||
} | ||
|
||
@Test | ||
public void whenConvert_thenEqualNanos() { | ||
Timestamp timestamp = new Timestamp(124, 3, 19, 9, 30, 0, 801789562); | ||
Instant instantNoTZ = timestamp.toInstant(); | ||
Instant instantTZ = timestamp.toInstant().atZone(ZoneId.systemDefault()).toInstant(); | ||
assertEquals(instantNoTZ.getEpochSecond(), instantTZ.getEpochSecond()); | ||
assertEquals(instantNoTZ.getNano(), instantTZ.getNano()); | ||
} | ||
} |