Skip to content
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

Merged

Conversation

sandysimonton1337
Copy link
Contributor

Demonstrating the conversion from java.sql.Timestamp to java.util.Calendar, as well as suggesting the use of Instant.

@sandysimonton1337
Copy link
Contributor Author

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);
Copy link
Collaborator

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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public void givenCalendar_whenConvert_thenLoseNanos() {
public void givenTimestamp_whenConvertToCalendarAndBack_thenLoseNanos() {

public class SqlTimestampToCalendarConverterUnitTest {

@Test
public void givenTimestamp_whenConvert_thenEqualMillis() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public void givenTimestamp_whenConvert_thenEqualMillis() {
public void givenTimestamp_whenConvertToCalendar_thenEqualMillis() {

}

@Test
public void givenCalendarFromTimestamp_whenConvertBack_thenEqualMillis() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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());
Copy link
Collaborator

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

@theangrydev theangrydev merged commit a952848 into eugenp:master Jun 1, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants