Localdate.format,格式未应用 [英] Localdate.format, format is not applied

查看:723
本文介绍了Localdate.format,格式未应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的FXML中有一个DatePicker,我需要将Date插入到我的SQL数据库中。我想格式化我的日期,但它不起作用。

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 = purchased_at.getValue();
    localDate.format(DateTimeFormatter.ofPattern("dd.mm.yyyy"));

这是我得到的错误。

Caused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: MinuteOfHour

我还是个初学者。我过去3或4个月就有Java了。我正在努力改进。

I'm still kind of a beginner. I had Java for the past 3 or 4 months now. I'm trying my best to improve.

推荐答案

我必须为我的Datepicker使用String转换器。

I had to use a String converter for my Datepicker.

    public String changeformat(DatePicker date) {

    date.setConverter(new StringConverter<LocalDate>() {
        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;
}

它工作得很好。因为我目前正在使用5个Datepickers,所以我不得不使用参数。

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

这篇关于Localdate.format,格式未应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆