A DESCRIPTION OF THE PROBLEM :
java.sql.Date.valueOf(java.time.LocalDate) and java.sql.Timestamp.valueOf(java.time.LocalDateTime) return incorrect values when the year is BCE.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Save any negative date to an SQL database that supports it.
Extract the date, then convert it to LocalDate. See test case
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Date sent to database: -0200-04-04
Date returned from database: -0200-04-04
Timestamp sent to database: -0200-04-04T10:30
Timestamp returned from database: -0200-04-04T10:30
ACTUAL -
Date sent to database: -0200-04-04
Date returned from database: 0201-04-04
Timestamp sent to database: -0200-04-04T10:30
Timestamp returned from database: 0201-04-04T10:30
---------- BEGIN SOURCE ----------
import java.sql.Date;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
public class SqlDateTest {
public static void main(String[] args) {
LocalDate startDate = LocalDate.of(-200,4,4);
Date sqlDate = Date.valueOf(startDate);
LocalDate dateFromDatabase = sqlDate.toLocalDate();
LocalDateTime startStamp = LocalDateTime.of(-200,4,4,10,30,0);
Timestamp sqlStamp = Timestamp.valueOf(startStamp);
LocalDateTime stampFromDatabase = sqlStamp.toLocalDateTime();
System.out.println("Date sent to database: " + startDate);
System.out.println("Date returned from database: " + dateFromDatabase);
System.out.println("Timestamp sent to database: " + startStamp);
System.out.println("Timestamp returned from database: " + stampFromDatabase);
}
}
---------- END SOURCE ----------
FREQUENCY : always
java.sql.Date.valueOf(java.time.LocalDate) and java.sql.Timestamp.valueOf(java.time.LocalDateTime) return incorrect values when the year is BCE.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Save any negative date to an SQL database that supports it.
Extract the date, then convert it to LocalDate. See test case
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Date sent to database: -0200-04-04
Date returned from database: -0200-04-04
Timestamp sent to database: -0200-04-04T10:30
Timestamp returned from database: -0200-04-04T10:30
ACTUAL -
Date sent to database: -0200-04-04
Date returned from database: 0201-04-04
Timestamp sent to database: -0200-04-04T10:30
Timestamp returned from database: 0201-04-04T10:30
---------- BEGIN SOURCE ----------
import java.sql.Date;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
public class SqlDateTest {
public static void main(String[] args) {
LocalDate startDate = LocalDate.of(-200,4,4);
Date sqlDate = Date.valueOf(startDate);
LocalDate dateFromDatabase = sqlDate.toLocalDate();
LocalDateTime startStamp = LocalDateTime.of(-200,4,4,10,30,0);
Timestamp sqlStamp = Timestamp.valueOf(startStamp);
LocalDateTime stampFromDatabase = sqlStamp.toLocalDateTime();
System.out.println("Date sent to database: " + startDate);
System.out.println("Date returned from database: " + dateFromDatabase);
System.out.println("Timestamp sent to database: " + startStamp);
System.out.println("Timestamp returned from database: " + stampFromDatabase);
}
}
---------- END SOURCE ----------
FREQUENCY : always
- duplicates
-
JDK-8272194 java.sql.Date::toLocalDate() broken for dates before 1 A.D.
- Open