-
Notifications
You must be signed in to change notification settings - Fork 54.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BAEL-7614 sql.Timestamp to java.util.Calendar conversion #16607
BAEL-7614 sql.Timestamp to java.util.Calendar conversion #16607
Conversation
bcd4556
to
fcbd0fb
Compare
...ations-4/src/main/java/com/baeldung/timestamptocalendar/SqlTimestampToCalendarConverter.java
Outdated
Show resolved
Hide resolved
.../src/test/java/com/baeldung/timestamptocalendar/SqlTimestampToCalendarConverterUnitTest.java
Outdated
Show resolved
Hide resolved
.../src/test/java/com/baeldung/timestamptocalendar/SqlTimestampToCalendarConverterUnitTest.java
Outdated
Show resolved
Hide resolved
.../src/test/java/com/baeldung/timestamptocalendar/SqlTimestampToCalendarConverterUnitTest.java
Outdated
Show resolved
Hide resolved
...ations-4/src/main/java/com/baeldung/timestamptocalendar/SqlTimestampToCalendarConverter.java
Outdated
Show resolved
Hide resolved
I apologize for being all over the board! I pushed the changes and also edited the article to reflect the removal of java.time.Instant. |
|
||
@Test | ||
public void givenTimestamp_whenConvert_thenEqualMillis() { | ||
Timestamp timestamp = new Timestamp(124, 3, 19, 9, 30, 0, 801789562); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This Timestamp constructor has been deprecated for a very long time (since Java 1.2 AFAIK) - we should really only be using the new Timestamp(long time) constructor + Timestamp.setNanos if you want to control the nanos field
} | ||
|
||
@Test | ||
public void givenCalendar_whenConvert_thenLoseNanos() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public void givenCalendar_whenConvert_thenLoseNanos() { | |
public void givenTimestamp_whenConvertToCalendarAndBack_thenLoseNanos() { |
public class SqlTimestampToCalendarConverterUnitTest { | ||
|
||
@Test | ||
public void givenTimestamp_whenConvert_thenEqualMillis() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public void givenTimestamp_whenConvert_thenEqualMillis() { | |
public void givenTimestamp_whenConvertToCalendar_thenEqualMillis() { |
} | ||
|
||
@Test | ||
public void givenCalendarFromTimestamp_whenConvertBack_thenEqualMillis() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public void givenCalendarFromTimestamp_whenConvertBack_thenEqualMillis() { | |
public void givenCalendarFromTimestamp_whenConvertBackToTimestamp_thenEqualMillis() { |
assertEquals(nanos, timestamp.getNanos()); | ||
Calendar calendar = SqlTimestampToCalendarConverter.timestampToCalendar(timestamp); | ||
timestamp = SqlTimestampToCalendarConverter.calendarToTimestamp(calendar); | ||
assertNotEquals(nanos, timestamp.getNanos()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do an assertEquals here instead to make it clear what exactly happens to the nanos field vs the original value
dc87763
to
b370f51
Compare
b370f51
to
2afae60
Compare
971dd17
to
b351836
Compare
b351836
to
ea455f3
Compare
Demonstrating the conversion from java.sql.Timestamp to java.util.Calendar, as well as suggesting the use of Instant.