Skip to content

Commit 67566c6

Browse files
committed
fixed xsd:date was being treated as xsd:datetime
1 parent 1c3dfd7 commit 67566c6

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/main/java/n10s/RDFToLPGStatementProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ protected Object getObjectValue(IRI propertyIRI, Literal object) {
121121
}
122122
} else if (datatype.equals(XMLSchema.DATE)) {
123123
try {
124-
return DateUtils.parseDateTime(object.stringValue());
124+
return DateUtils.parseDate(object.stringValue());
125125
} catch (IllegalArgumentException e) {
126126
//if date cannot be parsed we return string value
127127
return object.stringValue();

src/main/java/n10s/utils/DateUtils.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.time.DateTimeException;
44
import java.time.LocalDateTime;
5+
import java.time.LocalDate;
56
import java.time.format.DateTimeParseException;
67
import java.util.Calendar;
78

@@ -51,4 +52,39 @@ public static LocalDateTime parseDateTime(String stringDateTime) {
5152
return localDateTime;
5253
}
5354
}
55+
56+
public static LocalDate parseDate(String stringDate) {
57+
boolean dateParsed = false;
58+
LocalDate localDate = null;
59+
StringBuilder parserErrors = new StringBuilder("Error parsing ").append(stringDate).append(":\n");
60+
61+
/* Try date parsing with LocalDateTime.parse */
62+
try {
63+
localDate = LocalDate.parse(stringDate);
64+
dateParsed = true;
65+
} catch (DateTimeParseException e) {
66+
dateParsed = false;
67+
parserErrors.append(e.getMessage()).append("\n");
68+
}
69+
70+
/* If date is not parsed */
71+
if (!dateParsed) {
72+
/* Try with DatatypeConverter.parseDateTime */
73+
try {
74+
Calendar calendar = DatatypeConverter.parseDate(stringDate);
75+
localDate = LocalDate.ofInstant(calendar.toInstant(), calendar.getTimeZone().toZoneId());
76+
dateParsed = true;
77+
} catch (IllegalArgumentException | DateTimeException e) {
78+
dateParsed = false;
79+
parserErrors.append(e.getMessage()).append("\n");
80+
}
81+
}
82+
83+
/* If date is not parsed, throw exception */
84+
if (!dateParsed) {
85+
throw new IllegalArgumentException(parserErrors.toString());
86+
} else {
87+
return localDate;
88+
}
89+
}
5490
}

0 commit comments

Comments
 (0)