-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
31250ff
commit 9fc90b8
Showing
7 changed files
with
122 additions
and
21 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
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
9 changes: 9 additions & 0 deletions
9
...java/de/deadlocker8/budgetmaster/transactions/csvimport/CsvTransactionParseException.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,9 @@ | ||
package de.deadlocker8.budgetmaster.transactions.csvimport; | ||
|
||
public class CsvTransactionParseException extends Exception | ||
{ | ||
public CsvTransactionParseException(String message) | ||
{ | ||
super(message); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...erServer/src/main/java/de/deadlocker8/budgetmaster/transactions/csvimport/DateParser.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,32 @@ | ||
package de.deadlocker8.budgetmaster.transactions.csvimport; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeParseException; | ||
import java.util.Locale; | ||
import java.util.Optional; | ||
|
||
public class DateParser | ||
{ | ||
|
||
private DateParser() | ||
{ | ||
} | ||
|
||
public static Optional<LocalDate> parse(String dateString, String pattern, Locale locale) | ||
{ | ||
if(dateString == null || pattern == null || locale == null) | ||
{ | ||
return Optional.empty(); | ||
} | ||
|
||
try | ||
{ | ||
return Optional.of(LocalDate.parse(dateString, DateTimeFormatter.ofPattern(pattern).withLocale(locale))); | ||
} | ||
catch(DateTimeParseException e) | ||
{ | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
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
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
41 changes: 41 additions & 0 deletions
41
.../src/test/java/de/deadlocker8/budgetmaster/unit/transaction/csvimport/DateParserTest.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,41 @@ | ||
package de.deadlocker8.budgetmaster.unit.transaction.csvimport; | ||
|
||
import de.deadlocker8.budgetmaster.transactions.csvimport.DateParser; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.LocalDate; | ||
import java.util.Locale; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class DateParserTest | ||
{ | ||
@Test | ||
void test_nullText() | ||
{ | ||
assertThat(DateParser.parse(null, "dd.MM", Locale.ENGLISH)) | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
void test_nullPattern() | ||
{ | ||
assertThat(DateParser.parse("14.01.23", null, Locale.ENGLISH)) | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
void test_textNotMatchingPattern() | ||
{ | ||
assertThat(DateParser.parse("14.01.23", "dd.MM", Locale.ENGLISH)) | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
void test_matchingPattern() | ||
{ | ||
assertThat(DateParser.parse("14.01.23", "dd.MM.yy", Locale.ENGLISH)) | ||
.isPresent() | ||
.get().isEqualTo(LocalDate.of(2023, 1, 14)); | ||
} | ||
} |