Skip to content

Commit

Permalink
Merge pull request DSpace#9253 from the-library-code/TLC-404_8x_impro…
Browse files Browse the repository at this point in the history
…vements_date

Add new date format pattern to DCDate
  • Loading branch information
tdonohue authored Apr 23, 2024
2 parents 10362d9 + ae3583c commit 29d9172
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions dspace-api/src/main/java/org/dspace/content/DCDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ private enum DateGran { YEAR, MONTH, DAY, TIME }
// just year, "2009"
private final SimpleDateFormat yearIso = new SimpleDateFormat("yyyy");

// Additional iso-like format which contains milliseconds
private final SimpleDateFormat fullIsoWithMs = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.000'");

private static Map<Locale, DateFormatSymbols> dfsLocaleMap = new HashMap<Locale, DateFormatSymbols>();

/**
Expand Down Expand Up @@ -193,6 +196,9 @@ public DCDate(String fromDC) {
if (date == null) {
date = tryParse(fullIso4, fromDC);
}
if (date == null) {
date = tryParse(fullIsoWithMs, fromDC);
}
if (date == null) {
// Seems there is no time component to the date.
date = tryParse(dateIso, fromDC);
Expand Down Expand Up @@ -244,6 +250,7 @@ private void setUTCForFormatting() {
dateIso.setTimeZone(utcZone);
yearMonthIso.setTimeZone(utcZone);
yearIso.setTimeZone(utcZone);
fullIsoWithMs.setTimeZone(utcZone);
}

// Attempt to parse, swallowing errors; return null for failure.
Expand Down
22 changes: 22 additions & 0 deletions dspace-api/src/test/java/org/dspace/content/DCDateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,23 @@ public void testDCDateString() {
assertThat("testDCDateString 10", dc.getHourUTC(), equalTo(0));
assertThat("testDCDateString 11", dc.getMinuteUTC(), equalTo(0));
assertThat("testDCDateString 12", dc.getSecondUTC(), equalTo(1));

// test additional ISO format
dc = new DCDate("2010-04-14T00:00:01.000");
assertThat("testDCDateString 1", dc.getYear(), equalTo(2010));
assertThat("testDCDateString 2", dc.getMonth(), equalTo(04));
assertThat("testDCDateString 3", dc.getDay(), equalTo(13));
assertThat("testDCDateString 4", dc.getHour(), equalTo(16));
assertThat("testDCDateString 5", dc.getMinute(), equalTo(0));
assertThat("testDCDateIntBits 6", dc.getSecond(), equalTo(1));

assertThat("testDCDateString 7", dc.getYearUTC(), equalTo(2010));
assertThat("testDCDateString 8", dc.getMonthUTC(), equalTo(04));
assertThat("testDCDateString 9", dc.getDayUTC(), equalTo(14));
assertThat("testDCDateString 10", dc.getHourUTC(), equalTo(0));
assertThat("testDCDateString 11", dc.getMinuteUTC(), equalTo(0));
assertThat("testDCDateString 12", dc.getSecondUTC(), equalTo(1));

}


Expand Down Expand Up @@ -381,6 +398,11 @@ public void testDisplayDate() {
assertThat("testDisplayDate 7 ", dc.displayDate(false, false,
new Locale("en_GB")),
equalTo("14-Apr-2010"));

dc = new DCDate("2010-04-14T00:00:01.000");
assertThat("testDisplayDate 8 ", dc.displayDate(false, false,
new Locale("en_GB")),
equalTo("14-Apr-2010"));
}

/**
Expand Down

0 comments on commit 29d9172

Please sign in to comment.