Skip to content

Commit

Permalink
Exception shouldn't happen when pasting an entry with a publication d…
Browse files Browse the repository at this point in the history
…ate-range of form yyyy-yyyy (#8247)

* Fixed the DOI connection Error issue: Issue 8127

* Update CHANGELOG.md

* cleaning up code for fixing issue 8127

* adding endDate for processing date range

* modified Changelog

* add test case for the modified Date.java

* fixing the checkstyle error

* add one line comment to indicate the issue

* removed the comment CS427 Issue ...

* Update JabRef_Main.xml

* Update .gitignore

* Update JabRef_Main.xml

* Update Date.java

Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com>
  • Loading branch information
jiezheng5 and calixtus authored Dec 14, 2021
1 parent 851fa4b commit 1eed985
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .idea/runConfigurations/JabRef_Main.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve

### Fixed

- We fixed an issue where an exception occurred when pasting an entry with a publication date-range of the form 1910/1917 [#7864](https://github.com/JabRef/jabref/issues/7864)
- We fixed an issue where an exception occured when a preview style was edited and afterwards another preview style selected. [#8280](https://github.com/JabRef/jabref/issues/8280)
- We fixed an issue where the actions to move a file to a directory were incorrectly disabled. [#7908](https://github.com/JabRef/jabref/issues/7908)
- We fixed an issue where an exception occurred when a linked online file was edited in the entry editor [#8008](https://github.com/JabRef/jabref/issues/8008)
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/org/jabref/model/entry/Date.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class Date {
}

private final TemporalAccessor date;
private final TemporalAccessor endDate;

public Date(int year, int month, int dayOfMonth) {
this(LocalDate.of(year, month, dayOfMonth));
Expand All @@ -57,15 +58,46 @@ public Date(int year) {

public Date(TemporalAccessor date) {
this.date = date;
endDate = null;
}

/**
* Creates a Date from date and endDate.
*
* @param date the start date
* @param endDate the start date
*/
public Date(TemporalAccessor date, TemporalAccessor endDate) {
this.date = date;
this.endDate = endDate;
}

/**
* Creates a Date from date and endDate.
*
* @param dateString the string to extract the date information
* @throws DateTimeParseException if dataString is mal-formatted
*/
public static Optional<Date> parse(String dateString) {
Objects.requireNonNull(dateString);

if (dateString.isEmpty()) {
return Optional.empty();
}

// if dateString has format of uuuu/uuuu, treat as date range
if (dateString.matches("[0-9]{4}/[0-9]{4}")) {
try {
String[] strDates = dateString.split("/");
TemporalAccessor parsedDate = SIMPLE_DATE_FORMATS.parse(strDates[0]);
TemporalAccessor parsedEndDate = SIMPLE_DATE_FORMATS.parse(strDates[1]);
return Optional.of(new Date(parsedDate, parsedEndDate));
} catch (DateTimeParseException ignored) {
return Optional.empty();
}

}

try {
TemporalAccessor parsedDate = SIMPLE_DATE_FORMATS.parse(dateString);
return Optional.of(new Date(parsedDate));
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/org/jabref/model/entry/DateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

class DateTest {

@Test
void parseCorrectlyYearRangeDate() throws Exception {
Date expectedDataRange = new Date(Year.of(2014), Year.of(2017));
assertEquals(Optional.of(expectedDataRange), Date.parse("2014/2017"));
}

@Test
void parseCorrectlyDayMonthYearDate() throws Exception {
Date expected = new Date(LocalDate.of(2014, 6, 19));
Expand Down

0 comments on commit 1eed985

Please sign in to comment.