Skip to content

Commit 5b0b903

Browse files
committed
PTBAS-737: Fix inconsistent locale usage in some DatePickers
1 parent a0ef07b commit 5b0b903

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/main/java/de/doubleslash/keeptime/common/DateFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class DateFormatter {
3131
private static DateTimeFormatter dayDateFormatter = DateTimeFormatter.ofPattern("eeee dd.MM.yyyy");
3232
private static DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
3333

34-
private static Locale systemLocale = Locale.GERMAN;
34+
private static Locale systemLocale = Locale.getDefault();
3535

3636
private DateFormatter() {
3737
throw new IllegalStateException("Utility class: DateFormatter");

src/main/java/de/doubleslash/keeptime/view/ExternalProjectsSyncController.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ public class ExternalProjectsSyncController {
124124
private final Color colorLoadingSuccess = Color.valueOf("#74a317");
125125
private final Color colorLoadingFailure = Color.valueOf("#c63329");
126126

127-
private final LocalTimeStringConverter localTimeStringConverter = new LocalTimeStringConverter(FormatStyle.MEDIUM);
127+
private final LocalTimeStringConverter localTimeStringConverter = new LocalTimeStringConverter(
128+
FormatStyle.MEDIUM,
129+
DateFormatter.getSystemLocale());
128130
private ObservableList<TableRow> items;
129131

130132
private LocalDate currentReportDate;
@@ -579,8 +581,10 @@ private void showErrorDialog(List<String> errorMessages) {
579581
}
580582

581583
private void setUpTimeSpinner(final Spinner<LocalTime> spinner) {
584+
final LocalTimeStringConverter stringConverter = new LocalTimeStringConverter(
585+
FormatStyle.MEDIUM,
586+
DateFormatter.getSystemLocale());
582587
spinner.focusedProperty().addListener(e -> {
583-
final LocalTimeStringConverter stringConverter = new LocalTimeStringConverter(FormatStyle.MEDIUM);
584588
final StringProperty text = spinner.getEditor().textProperty();
585589
try {
586590
stringConverter.fromString(text.get());
@@ -621,7 +625,7 @@ public void increment(final int steps) {
621625

622626
});
623627

624-
spinner.getValueFactory().setConverter(new LocalTimeStringConverter(FormatStyle.MEDIUM));
628+
spinner.getValueFactory().setConverter(stringConverter);
625629
}
626630

627631
public static LocalTime decrementToLastFullQuarter(LocalTime time) {

src/test/java/de/doubleslash/keeptime/common/DateFormatterTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@
1818

1919
import static org.hamcrest.MatcherAssert.assertThat;
2020

21+
import java.time.LocalDate;
2122
import java.time.LocalDateTime;
23+
import java.util.Locale;
2224

25+
import javafx.scene.control.DatePicker;
2326
import org.hamcrest.Matchers;
2427
import org.junit.jupiter.api.Disabled;
2528
import org.junit.jupiter.api.Test;
2629

30+
import static org.junit.jupiter.api.Assertions.assertTrue;
31+
2732
@Disabled
2833
class DateFormatterTest {
2934

@@ -38,4 +43,21 @@ void zeroSecondsBetweenTest() {
3843
final long secondsBewtweenSwitched = DateFormatter.getSecondsBewtween(endDate, startDate);
3944
assertThat(secondsBewtweenSwitched, Matchers.is(0l)); // why??
4045
}
46+
47+
@Test
48+
void applySystemLocaleShouldSetDatePickerLocale() {
49+
// GIVEN
50+
Locale.setDefault(Locale.ENGLISH);
51+
DateFormatter.setSystemLocale(Locale.GERMAN);
52+
DatePicker datePicker = new DatePicker();
53+
LocalDate date = LocalDate.of(2024, 6, 3);
54+
55+
// WHEN
56+
DateFormatter.applySystemLocaleOnDate(datePicker);
57+
datePicker.setValue(date);
58+
59+
// THEN
60+
String shown = datePicker.getEditor().getText();
61+
assertTrue(shown.contains("Montag") || shown.contains("06.2024") || shown.contains("06"));
62+
}
4163
}

0 commit comments

Comments
 (0)