-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Added layout DateFormatter, see #1249 * Removed comment (sorry about that)
- Loading branch information
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/net/sf/jabref/logic/layout/format/DateFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package net.sf.jabref.logic.layout.format; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
import net.sf.jabref.logic.layout.ParamLayoutFormatter; | ||
|
||
|
||
public class DateFormatter implements ParamLayoutFormatter { | ||
|
||
private String formatString = "yyyy-MM-dd"; // Use ISO-format as default | ||
|
||
|
||
@Override | ||
public String format(String fieldText) { | ||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatString); | ||
LocalDate date = LocalDate.parse(fieldText, DateTimeFormatter.ISO_LOCAL_DATE); | ||
return date.format(formatter); | ||
} | ||
|
||
@Override | ||
public void setArgument(String arg) { | ||
formatString = arg; | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/test/java/net/sf/jabref/logic/layout/format/DateFormatterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package net.sf.jabref.logic.layout.format; | ||
|
||
import net.sf.jabref.logic.layout.ParamLayoutFormatter; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class DateFormatterTest { | ||
|
||
private ParamLayoutFormatter formatter; | ||
|
||
@Before | ||
public void setUp() { | ||
formatter = new DateFormatter(); | ||
} | ||
|
||
@Test | ||
public void testDefaultFormat() { | ||
Assert.assertEquals("2016-07-15", formatter.format("2016-07-15")); | ||
} | ||
|
||
@Test | ||
public void testRequestedFormat() { | ||
formatter.setArgument("MM/yyyy"); | ||
Assert.assertEquals("07/2016", formatter.format("2016-07-15")); | ||
} | ||
|
||
} |