/** * Formatter for dates and times. * * @param locale The locale, must not be null. */ public DateTimeFormatter(Locale locale) { requireNonNull(locale); this.prettyTime = new PrettyTime(locale); this.shortTimeFormat = new LocalTimeStringConverter(SHORT, locale); // getTimeInstance(SHORT, locale); this.mediumDateFormat = new LocalDateStringConverter(MEDIUM, locale, null); // getDateInstance(MEDIUM, locale); }
void updateValue() { if (is24HourView) { LocalTimeStringConverter localTimeStringConverter = new LocalTimeStringConverter(FormatStyle.SHORT, Locale.GERMAN); timePicker.setValue(localTimeStringConverter.fromString(selectedHourLabel.getText() + ":" + selectedMinLabel.getText())); } else { timePicker.setValue(LocalTime.parse(selectedHourLabel.getText() + ":" + selectedMinLabel.getText() + " " + period.get(), DateTimeFormatter.ofPattern("h:mm a").withLocale(Locale.ENGLISH))); } }
@Override public Node createVisualisation(SimpleObjectProperty<LocalTime> boundTo, boolean readonly) { TextField textField = new TextField(); textField.setPromptText("e.g.: 12:13"); textField.textProperty().bindBidirectional(boundTo,new LocalTimeStringConverter()); HBox hBox = new HBox(); hBox.setSpacing(3); HBox.setHgrow(textField, Priority.ALWAYS); hBox.getChildren().add(textField); hBox.getChildren().add(new Label("h")); ChoiceBox<Integer> hour = new ChoiceBox<>(); for (int i=0;i<24;i++){ hour.getItems().add(i); } hour.setMinWidth(47); hour.setMaxWidth(47); hour.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { int newMinute=0; if (boundTo.getValue()!=null){ newMinute=(boundTo.getValue().getMinute()); } boundTo.setValue(LocalTime.of(newValue, newMinute)); }); hBox.getChildren().add(hour); hBox.getChildren().add(new Label("m")); ChoiceBox<Integer> minute = new ChoiceBox<>(); for (int i=0;i<60;i++){ minute.getItems().add(i); } minute.setMinWidth(47); minute.setMaxWidth(47); minute.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> { int newHour=0; if (boundTo.getValue()!=null){ newHour=(boundTo.getValue().getHour()); } boundTo.setValue(LocalTime.of(newHour, newValue)); }); LocalTimeStringConverter localTimeStringConverter = new LocalTimeStringConverter(); textField.textProperty().addListener((observable, oldValue, newValue) -> { textField.getStyleClass().remove("error"); LocalTime localTime=null; try { localTime= localTimeStringConverter.fromString(newValue); } catch (DateTimeParseException e){ if (!Strings.isNullOrEmpty(newValue)){ textField.getStyleClass().add("error"); } } if (localTime!=null){ hour.getSelectionModel().select(localTime.getHour()); minute.getSelectionModel().select(localTime.getMinute()); } }); hBox.getChildren().add(minute); hBox.setDisable(readonly); return hBox; }