Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix date parser for year/month #9507

Merged
merged 2 commits into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- The tab "deprecated fields" is shown in biblatex-mode only. [#7757](https://github.com/JabRef/jabref/issues/7757)
- We fixed an issue where the last opened libraries were not remembered when a new unsaved libray was open as well [#9190](https://github.com/JabRef/jabref/issues/9190)
- We fixed an issue where no context menu for the group "All entries" was present [forum#3682](https://discourse.jabref.org/t/how-sort-groups-a-z-not-subgroups/3682)
- We fixed an issue where entering a date in the format "YYYY/MM" in the entry editor date field caused an exception [#9492](https://github.com/JabRef/jabref/issues/9492)

### Removed

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jabref.gui.fieldeditors;

import java.time.DateTimeException;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
Expand All @@ -12,7 +13,12 @@
import org.jabref.model.entry.field.Field;
import org.jabref.model.strings.StringUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateEditorViewModel extends AbstractEditorViewModel {

private static final Logger LOGGER = LoggerFactory.getLogger(DateEditorViewModel.class);
private final DateTimeFormatter dateFormatter;

public DateEditorViewModel(Field field, SuggestionProvider<?> suggestionProvider, DateTimeFormatter dateFormatter, FieldCheckers fieldCheckers) {
Expand All @@ -25,7 +31,12 @@ public StringConverter<TemporalAccessor> getDateToStringConverter() {
@Override
public String toString(TemporalAccessor date) {
if (date != null) {
return dateFormatter.format(date);
try {
return dateFormatter.format(date);
} catch (DateTimeException ex) {
LOGGER.error("Could not format date", ex);
return "";
}
} else {
return "";
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/model/entry/Date.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class Date {
"uuuu-MM-dd'T'H[:ss][xxx][xx][X]", // covers 2018-10-03T7
"uuuu-M-d", // covers 2009-1-15
"uuuu-M", // covers 2009-11
"uuuu/M", // covers 2020/10
"d-M-uuuu", // covers 15-1-2012
"M-uuuu", // covers 1-2012
"M/uuuu", // covers 9/2015 and 09/2015
Expand All @@ -43,7 +44,6 @@ public class Date {
"uuuu.M.d", // covers 2015.1.15
"uuuu", // covers 2015
"MMM, uuuu", // covers Jan, 2020
"uuuu/M", // covers 2020/10
"uuuu.MM.d" // covers 2015.10.15
);

Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/jabref/model/entry/DateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ private static Stream<Arguments> validDates() {
Arguments.of(Year.of(2015), "2015"),
Arguments.of(YearMonth.of(2020, Month.JANUARY), "Jan, 2020"),
Arguments.of(LocalDate.of(2015, Month.OCTOBER, 15), "2015.10.15"),
Arguments.of(LocalDate.of(-10000, Month.OCTOBER, 15), "-10000-10-15")
Arguments.of(LocalDate.of(-10000, Month.OCTOBER, 15), "-10000-10-15"),
Arguments.of(YearMonth.of(2015, Month.NOVEMBER), "2015/11")
);
}

Expand Down