Localdate.format, format is not applied

后端 未结 2 1558
北海茫月
北海茫月 2020-11-30 15:46

I have a DatePicker in my FXML and I need the Date to insert it into my SQL-Database. I want to format my Date but it doesn\'t work.

    LocalDate localDate          


        
2条回答
  •  伪装坚强ぢ
    2020-11-30 16:24

    I had to use a String converter for my Datepicker.

        public String changeformat(DatePicker date) {
    
        date.setConverter(new StringConverter() {
            String pattern = "MM.yyyy";
            DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(pattern);
    
            {
                date.setPromptText(pattern.toLowerCase());
            }
    
            @Override
            public String toString(LocalDate date) {
                if (date != null) {
                    return dateFormatter.format(date);
                } else {
                    return "";
                }
            }
    
            @Override
            public LocalDate fromString(String string) {
                if (string != null && !string.isEmpty()) {
                    return LocalDate.parse(string, dateFormatter);
                } else {
                    return null;
                }
            }
        });
        return null;
    }
    

    It worked perfectly fine. I had to use a parameter since I'm currently using 5 Datepickers.

提交回复
热议问题