Skip to content

Commit c909ebc

Browse files
authored
Merge pull request #45 from spdx/isdates
Update Verify to handle ISO 8601 date formats
2 parents 7f1b313 + 44551b5 commit c909ebc

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

src/main/java/org/spdx/library/SpdxConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public class SpdxConstants {
332332
@Deprecated
333333
public static final String PROP_REVIEW_DATE = "reviewDate";
334334

335-
// Date format
335+
// Date format - NOTE: This format does not handle milliseconds. Use Instant.parse for full ISO 8601 parsing
336336
public static final String SPDX_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
337337

338338
// license ID format

src/main/java/org/spdx/library/SpdxVerificationHelper.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
package org.spdx.library;
1818

1919
import java.net.URI;
20-
import java.text.ParseException;
21-
import java.text.SimpleDateFormat;
20+
import java.time.Instant;
21+
import java.time.format.DateTimeParseException;
2222
import java.util.Objects;
2323

2424
import org.spdx.library.model.enumerations.ChecksumAlgorithm;
@@ -118,10 +118,9 @@ private static String verifyOriginatorOrSupplier(String originatorOrSupplier) {
118118
* @return
119119
*/
120120
public static String verifyDate(String creationDate) {
121-
SimpleDateFormat format = new SimpleDateFormat(SpdxConstants.SPDX_DATE_FORMAT);
122121
try {
123-
format.parse(creationDate);
124-
} catch (ParseException e) {
122+
Instant.parse(creationDate);
123+
} catch (DateTimeParseException e) {
125124
return("Invalid date format: "+e.getMessage());
126125
}
127126
return null;

src/test/java/org/spdx/library/SpdxVerificationHelperTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,11 @@ public void testVerifySupplier() {
8989
*/
9090
public void testVerifyDate() {
9191
DateFormat format = new SimpleDateFormat(SpdxConstants.SPDX_DATE_FORMAT);
92-
assertTrue(Objects.isNull(SpdxVerificationHelper.verifyDate(format.format(new Date()))));
92+
String fullDate = format.format(new Date());
93+
assertTrue(Objects.isNull(SpdxVerificationHelper.verifyDate(fullDate)));
9394
assertFalse(Objects.isNull(SpdxVerificationHelper.verifyDate(new Date().toString())));
95+
String withSubSeconds = fullDate.substring(0, fullDate.length()-1) + ".234Z";
96+
assertTrue(Objects.isNull(SpdxVerificationHelper.verifyDate(withSubSeconds)));
9497
}
9598

9699
/**

0 commit comments

Comments
 (0)