Skip to content

Commit

Permalink
Some enhancements and cleanups related to dates
Browse files Browse the repository at this point in the history
  • Loading branch information
oscargus committed Jul 12, 2016
1 parent aa42800 commit ac72228
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 44 deletions.
6 changes: 4 additions & 2 deletions src/main/java/net/sf/jabref/gui/date/DatePickerButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ public class DatePickerButton implements ActionListener {
private final DatePicker datePicker = new DatePicker();
private final JPanel panel = new JPanel();
private final FieldEditor editor;
private final boolean isoFormat;


public DatePickerButton(FieldEditor pEditor) {
public DatePickerButton(FieldEditor pEditor, Boolean isoFormat) {
this.isoFormat = isoFormat;
datePicker.showButtonOnly(true);
datePicker.addActionListener(this);
datePicker.setShowTodayButton(true);
Expand All @@ -52,7 +54,7 @@ public DatePickerButton(FieldEditor pEditor) {
public void actionPerformed(ActionEvent e) {
Date date = datePicker.getDate();
if (date != null) {
editor.setText(new EasyDateFormat().getDateAt(date));
editor.setText(new EasyDateFormat().getDateAt(date, isoFormat));
// Set focus to editor component after changing its text:
new FocusRequester(editor.getTextComponent());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ public Optional<JComponent> getExtra(final FieldEditor editor) {
|| fieldExtras.contains(FieldProperties.DATE)) {
// double click AND datefield => insert the current date (today)
return FieldExtraComponents.getDateTimeExtraComponent(editor,
fieldExtras.contains(FieldProperties.DATE));
fieldExtras.contains(FieldProperties.DATE), fieldExtras.contains(FieldProperties.ISO_DATE));
} else if (fieldExtras.contains(FieldProperties.EXTERNAL)) {
return FieldExtraComponents.getExternalExtraComponent(panel, editor);
} else if (fieldExtras.contains(FieldProperties.JOURNAL_NAME)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,21 +346,22 @@ public static Optional<JComponent> getSelectorExtraComponent(JabRefFrame frame,
* @param isDatePicker
* @return
*/
public static Optional<JComponent> getDateTimeExtraComponent(FieldEditor editor, Boolean isDatePicker) {
public static Optional<JComponent> getDateTimeExtraComponent(FieldEditor editor, Boolean isDatePicker,
Boolean isoFormat) {
((JTextArea) editor).addMouseListener(new MouseAdapter() {

@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {// double click
String date = new EasyDateFormat().getCurrentDate();
String date = new EasyDateFormat().getCurrentDate(isoFormat);
editor.setText(date);
}
}
});

// insert a datepicker, if the extras field contains this command
if (isDatePicker) {
DatePickerButton datePicker = new DatePickerButton(editor);
DatePickerButton datePicker = new DatePickerButton(editor, isoFormat);
return Optional.of(datePicker.getDatePicker());
} else {
return Optional.empty();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/sf/jabref/gui/preftabs/GeneralTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.awt.BorderLayout;
import java.awt.Component;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
Expand Down Expand Up @@ -278,7 +278,7 @@ public void storeSettings() {
public boolean validateSettings() {
try {
// Test if date format is legal:
new SimpleDateFormat(timeStampFormat.getText());
DateTimeFormatter.ofPattern(timeStampFormat.getText());

} catch (IllegalArgumentException ex2) {
JOptionPane.showMessageDialog
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/net/sf/jabref/importer/EntryFromPDFCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -93,8 +94,8 @@ private void addEntryDataFromPDDocumentInformation(File pdfFile, BibEntry entry)
Calendar creationDate = pdfDocInfo.getCreationDate();
if (creationDate != null) {
// default time stamp follows ISO-8601. Reason: https://xkcd.com/1179/
String date = new SimpleDateFormat("yyyy-MM-dd")
.format(creationDate.getTime());
String date = LocalDate.of(creationDate.YEAR, creationDate.MONTH + 1, creationDate.DAY_OF_MONTH)
.format(DateTimeFormatter.ISO_LOCAL_DATE);
appendToField(entry, "timestamp", date);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@
*/
package net.sf.jabref.logic.layout.format;

import java.text.SimpleDateFormat;
import java.util.Date;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import net.sf.jabref.logic.layout.LayoutFormatter;

/**
* Inserts the current date (the time a database is being exported).
*
* <p>If a fieldText is given, it must be a valid {@link SimpleDateFormat} pattern.
* <p>If a fieldText is given, it must be a valid {@link DateTimeFormatter} pattern.
* If none is given, the format pattern will be <code>yyyy-MM-dd hh:mm:ss z</code>.
* This follows ISO-8601. Reason: <a href="https://xkcd.com/1179/">https://xkcd.com/1179/</a>.</p>
*
Expand All @@ -55,6 +54,6 @@ public String format(String fieldText) {
if ((fieldText != null) && (fieldText.trim() != null) && !fieldText.trim().isEmpty()) {
format = fieldText;
}
return new SimpleDateFormat(format).format(new Date());
return LocalDate.now().format(DateTimeFormatter.ofPattern(format));
}
}
29 changes: 12 additions & 17 deletions src/main/java/net/sf/jabref/logic/net/Cookie.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
package net.sf.jabref.logic.net;

import java.net.URI;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Locale;
import java.util.Optional;

Expand All @@ -28,15 +27,12 @@ class Cookie {
private final String name;
private final String value;
private String domain;
private Date expires;
private LocalDate expires;
private String path;

/**
* DateFormats should not be reused among instances (or rather among threads), because they are not thread-safe.
* If they are shared, their usage should be synchronized.
*/
private final DateFormat whiteSpaceFormat = new SimpleDateFormat("E, dd MMM yyyy k:m:s 'GMT'", Locale.US);
private final DateFormat hyphenFormat = new SimpleDateFormat("E, dd-MMM-yyyy k:m:s 'GMT'", Locale.US);
private final DateTimeFormatter whiteSpaceFormat = DateTimeFormatter.ofPattern("E, dd MMM yyyy k:m:s 'GMT'",
Locale.US);
private final DateTimeFormatter hyphenFormat = DateTimeFormatter.ofPattern("E, dd-MMM-yyyy k:m:s 'GMT'", Locale.US);


/**
Expand Down Expand Up @@ -82,11 +78,11 @@ public Cookie(URI uri, String header) {
this.path = value;
} else if ("expires".equalsIgnoreCase(name)) {
try {
this.expires = whiteSpaceFormat.parse(value);
} catch (ParseException e) {
this.expires = LocalDate.parse(value, whiteSpaceFormat);
} catch (DateTimeParseException e) {
try {
this.expires = hyphenFormat.parse(value);
} catch (ParseException e2) {
this.expires = LocalDate.parse(value, hyphenFormat);
} catch (DateTimeParseException e2) {
throw new IllegalArgumentException(
"Bad date format in header: " + value);
}
Expand All @@ -99,8 +95,7 @@ public boolean hasExpired() {
if (expires == null) {
return false;
}
Date now = new Date();
return now.after(expires);
return LocalDate.now().isAfter(expires);
}

/**
Expand Down
46 changes: 37 additions & 9 deletions src/main/java/net/sf/jabref/logic/util/date/EasyDateFormat.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.sf.jabref.logic.util.date;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import net.sf.jabref.Globals;
Expand All @@ -11,8 +13,8 @@ public class EasyDateFormat {
/**
* The formatter objects
*/
private SimpleDateFormat dateFormatter;

private DateTimeFormatter dateFormatter;
private boolean isISOFormat;

/**
* Creates a String containing the current date (and possibly time),
Expand All @@ -22,7 +24,28 @@ public class EasyDateFormat {
* @return The date string.
*/
public String getCurrentDate() {
return getDateAt(new Date());
return getCurrentDate(false);
}

/**
* Creates a String containing the current date (and possibly time),
* formatted according to the format set in preferences under the key
* "timeStampFormat".
*
* @return The date string.
*/
public String getCurrentDate(boolean isoFormat) {
return getDateAt(LocalDate.now(), isoFormat);
}

/**
* Creates a readable Date string from the parameter date. The format is set
* in preferences under the key "timeStampFormat".
*
* @return The formatted date string.
*/
public String getDateAt(Date date, boolean isoFormat) {
return getDateAt(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(), isoFormat);
}

/**
Expand All @@ -31,12 +54,17 @@ public String getCurrentDate() {
*
* @return The formatted date string.
*/
public String getDateAt(Date date) {
public String getDateAt(LocalDate localDate, boolean isoFormat) {
// first use, create an instance
if (dateFormatter == null) {
String format = Globals.prefs.get(JabRefPreferences.TIME_STAMP_FORMAT);
dateFormatter = new SimpleDateFormat(format);
if ((dateFormatter == null) || (isoFormat != isISOFormat)) {
if (isoFormat) {
dateFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
} else {
String format = Globals.prefs.get(JabRefPreferences.TIME_STAMP_FORMAT);
dateFormatter = DateTimeFormatter.ofPattern(format);
}
isISOFormat = isoFormat;
}
return dateFormatter.format(date);
return localDate.format(dateFormatter);
}
}
3 changes: 2 additions & 1 deletion src/main/java/net/sf/jabref/model/entry/FieldProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public enum FieldProperties {
EDITOR_TYPE,
PAGINATION,
TYPE,
CROSSREF;
CROSSREF,
ISO_DATE;

public static final Set<FieldProperties> ALL_OPTS = EnumSet.allOf(FieldProperties.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ private InternalBibtexFields() {
add(new BibtexSingleField("eid", true, BibtexSingleField.SMALL_W));

dummy = new BibtexSingleField("date", true);
dummy.setPrivate();
dummy.setExtras(EnumSet.of(FieldProperties.DATE));
dummy.setPrivate(); // TODO: Why private?
add(dummy);

add(new BibtexSingleField("pmid", false, BibtexSingleField.SMALL_W, 60).setNumeric(true));
Expand Down Expand Up @@ -300,6 +301,7 @@ private InternalBibtexFields() {
field = new BibtexSingleField(fieldText, true, BibtexSingleField.SMALL_W);
}
field.getExtras().add(FieldProperties.DATE);
field.getExtras().add(FieldProperties.ISO_DATE);
add(field);
}

Expand Down

0 comments on commit ac72228

Please sign in to comment.