Skip to content

Commit a0ef07b

Browse files
author
mal-saiaf
committed
PTBAS-737: using consistent english language now
* using system locale for date/times
1 parent 8d0cd21 commit a0ef07b

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

src/main/java/de/doubleslash/keeptime/App.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@
2121
import java.io.StringWriter;
2222
import java.time.LocalDate;
2323
import java.util.List;
24+
import java.util.Locale;
2425
import java.util.Optional;
2526
import java.util.stream.Collectors;
2627

28+
import de.doubleslash.keeptime.common.DateFormatter;
2729
import javafx.stage.Window;
2830
import org.slf4j.Logger;
2931
import org.slf4j.LoggerFactory;
@@ -84,6 +86,8 @@ public class App extends Application {
8486
@Override
8587
public void init() throws Exception {
8688
LOG.info("Starting KeepTime.");
89+
DateFormatter.setSystemLocale(Locale.getDefault());
90+
Locale.setDefault(Locale.ENGLISH);
8791
final DefaultExceptionHandler defaultExceptionHandler = new DefaultExceptionHandler();
8892
defaultExceptionHandler.register();
8993

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,23 @@
1616

1717
package de.doubleslash.keeptime.common;
1818

19+
import javafx.scene.control.DatePicker;
20+
import javafx.util.StringConverter;
21+
1922
import java.time.Duration;
2023
import java.time.LocalDate;
2124
import java.time.LocalDateTime;
2225
import java.time.format.DateTimeFormatter;
26+
import java.time.format.DateTimeParseException;
27+
import java.time.format.FormatStyle;
28+
import java.util.Locale;
2329

2430
public class DateFormatter {
2531
private static DateTimeFormatter dayDateFormatter = DateTimeFormatter.ofPattern("eeee dd.MM.yyyy");
2632
private static DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
2733

34+
private static Locale systemLocale = Locale.GERMAN;
35+
2836
private DateFormatter() {
2937
throw new IllegalStateException("Utility class: DateFormatter");
3038
}
@@ -55,4 +63,33 @@ public static String toTimeString(final LocalDateTime localDateTime) {
5563
return localDateTime.format(timeFormatter);
5664
}
5765

66+
public static void setSystemLocale(Locale locale) {
67+
systemLocale = locale;
68+
}
69+
70+
public static Locale getSystemLocale() {
71+
return systemLocale;
72+
}
73+
74+
public static void applySystemLocaleOnDate(DatePicker datePicker) {
75+
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(systemLocale);
76+
77+
datePicker.setConverter(new StringConverter<>() {
78+
@Override
79+
public String toString(LocalDate date) {
80+
return (date != null) ? formatter.format(date) : "";
81+
}
82+
83+
@Override
84+
public LocalDate fromString(String s) {
85+
try {
86+
return (s != null && !s.isEmpty()) ? LocalDate.parse(s, formatter) : null;
87+
} catch (DateTimeParseException e) {
88+
return null;
89+
}
90+
}
91+
});
92+
93+
datePicker.setPromptText(formatter.format(LocalDate.now()));
94+
}
5895
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.time.format.DateTimeParseException;
2222
import java.time.format.FormatStyle;
2323

24+
import de.doubleslash.keeptime.common.DateFormatter;
2425
import javafx.scene.control.skin.ComboBoxListViewSkin;
2526
import org.slf4j.Logger;
2627
import org.slf4j.LoggerFactory;
@@ -120,6 +121,9 @@ private void initialize() {
120121

121122
private void setUpTimeRestriction() {
122123

124+
DateFormatter.applySystemLocaleOnDate(startDatePicker);
125+
DateFormatter.applySystemLocaleOnDate(endDatePicker);
126+
123127
BooleanBinding isValidBinding = Bindings.createBooleanBinding(() -> {
124128
if (startTimeSpinner.getValue() == null || endTimeSpinner.getValue() == null
125129
|| startDatePicker.getValue() == null || endDatePicker.getValue() == null) {
@@ -150,8 +154,11 @@ private void setUpTimeRestriction() {
150154
}
151155

152156
private void setUpTimeSpinner(final Spinner<LocalTime> spinner) {
157+
final LocalTimeStringConverter stringConverter = new LocalTimeStringConverter(
158+
FormatStyle.MEDIUM,
159+
DateFormatter.getSystemLocale()
160+
);
153161
spinner.focusedProperty().addListener((e) -> {
154-
final LocalTimeStringConverter stringConverter = new LocalTimeStringConverter(FormatStyle.MEDIUM);
155162
final StringProperty text = spinner.getEditor().textProperty();
156163
try {
157164
stringConverter.fromString(text.get());
@@ -188,7 +195,7 @@ public void increment(final int steps) {
188195

189196
});
190197

191-
spinner.getValueFactory().setConverter(new LocalTimeStringConverter(FormatStyle.MEDIUM));
198+
spinner.getValueFactory().setConverter(stringConverter);
192199

193200
}
194201

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ private void updateReport(final LocalDate dateToShow) {
355355

356356
private void loadCalenderWidget() {
357357
final DatePicker myDatePicker = new DatePicker(this.currentReportDate);
358+
DateFormatter.applySystemLocaleOnDate(myDatePicker);
358359
myDatePicker.valueProperty().addListener((observable, oldvalue, newvalue) -> {
359360
LOG.info("Datepicker selected value changed to {}", newvalue);
360361
updateReport(newvalue);

0 commit comments

Comments
 (0)