Skip to content

Commit 0141114

Browse files
authored
Merge pull request #87 from forcedotcom/develop
Fix date format: support multiple formats
2 parents 247dc0b + 34a16d4 commit 0141114

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/main/java/com/salesforce/cdp/queryservice/core/QueryServiceResultSet.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,13 @@ public class QueryServiceResultSet implements ResultSet {
4747
private final AtomicBoolean closed = new AtomicBoolean();
4848
private final AtomicBoolean wasNull = new AtomicBoolean();
4949
protected ResultSetMetaData resultSetMetaData;
50-
//private SimpleDateFormat dateFormatterWithTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
50+
5151
//To fix the Invalid Date format issue
52-
private SimpleDateFormat dateFormatterWithTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS Z");
53-
private SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
52+
private final String dateSimple = "yyyy-MM-dd";
53+
private final String dateISOStandard = "yyyy-MM-dd'T'HH:mm:ss";
54+
private final String dateWithSeconds = "yyyy-MM-dd HH:mm:ss";
55+
private final String dateWithMsTz = "yyyy-MM-dd HH:mm:ss.SSS Z";
56+
5457
protected QueryServiceAbstractStatement statement;
5558
private int currentPageNum = 1;
5659

@@ -811,6 +814,7 @@ public Date getDate(int columnIndex, Calendar cal) throws SQLException {
811814
return getDate(columnNameByIndex, cal);
812815
}
813816

817+
//Handle multiple date formats one by one
814818
@Override
815819
public Date getDate(String columnLabel, Calendar cal) throws SQLException {
816820
errorOutIfClosed();
@@ -819,20 +823,25 @@ public Date getDate(String columnLabel, Calendar cal) throws SQLException {
819823
wasNull.set(true);
820824
return null;
821825
}
822-
dateFormatter.setTimeZone(cal.getTimeZone());
823-
dateFormatterWithTime.setTimeZone(cal.getTimeZone());
826+
String[] formats = new String[] {dateWithMsTz, dateISOStandard, dateWithSeconds, dateSimple};
824827
try {
825828
String valueString = value.toString();
826-
if (valueString.length() == 10) {
827-
cal.setTime(dateFormatter.parse(value.toString()));
828-
} else {
829-
cal.setTime(dateFormatterWithTime.parse(value.toString()));
829+
for (String format: formats) {
830+
SimpleDateFormat sdFormat = new SimpleDateFormat(format);
831+
sdFormat.setTimeZone(cal.getTimeZone());
832+
try {
833+
cal.setTime(sdFormat.parse(valueString));
834+
return new Date(cal.getTimeInMillis());
835+
} catch (ParseException e) {
836+
log.info("QSRS: caught exp {}", e.getMessage());
837+
log.warn("QSRS: Date format does not match the formatter, trying another format", e);
838+
}
830839
}
831-
return new Date(cal.getTimeInMillis());
832840
}
833-
catch (IllegalArgumentException | ParseException e) {
841+
catch (IllegalArgumentException e) {
834842
throw new SQLException("Invalid date from server: " + value, e);
835843
}
844+
return null;
836845
}
837846

838847
@Override

0 commit comments

Comments
 (0)