Skip to content

Commit 7f1fc26

Browse files
authored
Merge pull request #229 from data-integrations/cherry-pick-julian
Use timestamp in gregorian calendar format.
2 parents e13cd6a + fa3cf1c commit 7f1fc26

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

  • database-commons/src/main/java/io/cdap/plugin/util

database-commons/src/main/java/io/cdap/plugin/util/DBUtils.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@
3737
import java.sql.ResultSet;
3838
import java.sql.SQLException;
3939
import java.sql.Types;
40+
import java.util.Calendar;
41+
import java.util.Date;
42+
import java.util.GregorianCalendar;
4043
import java.util.Hashtable;
4144
import java.util.List;
4245
import java.util.Objects;
46+
import java.util.TimeZone;
4347
import javax.annotation.Nullable;
4448
import javax.management.InstanceNotFoundException;
4549
import javax.management.IntrospectionException;
@@ -55,6 +59,18 @@
5559
public final class DBUtils {
5660
private static final Logger LOG = LoggerFactory.getLogger(DBUtils.class);
5761

62+
private static final Calendar PURE_GREGORIAN_CALENDAR = createPureGregorianCalender();
63+
64+
// Java by default uses October 15, 1582 as a Gregorian cut over date.
65+
// Any timestamp created with time less than this cut over date is treated as Julian date.
66+
// This causes old dates from database such as 0001-01-01 01:00:00 mapped to 0000-12-30
67+
// Get the pure gregorian calendar so that all dates are treated as gregorian format.
68+
private static Calendar createPureGregorianCalender() {
69+
GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
70+
gc.setGregorianChange(new Date(Long.MIN_VALUE));
71+
return gc;
72+
}
73+
5874
/**
5975
* Performs any Database related cleanup
6076
*
@@ -116,7 +132,7 @@ public static Object transformValue(int sqlType, int precision, int scale,
116132
case Types.TIME:
117133
return resultSet.getTime(columnIndex);
118134
case Types.TIMESTAMP:
119-
return resultSet.getTimestamp(columnIndex);
135+
return resultSet.getTimestamp(columnIndex, PURE_GREGORIAN_CALENDAR);
120136
case Types.ROWID:
121137
return resultSet.getString(columnIndex);
122138
case Types.BLOB:

0 commit comments

Comments
 (0)