Localdate.format, format is not applied

后端 未结 2 1543
北海茫月
北海茫月 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:23

    Don’t format your date for insertion into your SQL database. Assuming that your database column has datatype date and you are using at least Java 8 and at least JDBC 4.2, just pass the LocalDate to your PreparedStatement as it is:

        PreparedStatement insertStmt = myConnection.prepareStatement(
                "insert into my_table(purchase_date) values (?)");
        insertStmt.setObject(1, purchaseDate);
    

    Your JDBC driver will take care of the rest. If using JPA, your JPA implementation will take care of it too.

    If your column has char type (for example varchar(10)) and you cannot change it, don’t invent your own format for it. Store the date in ISO 8601 format. LocalDate.toString() produces this format.

        String formattedDate = purchaseDate.toString();
        System.out.println(formattedDate);
    

    In my case output was:

    2017-11-29

    As an aside, for presentation to your user you shouldn’t invent your own format either. Rather rely on the built-in formats in Java. For example:

        Locale turkish = Locale.forLanguageTag("tr");
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
                .withLocale(turkish);
        String formattedDate = purchaseDate.format(dateFormatter);
        System.out.println(formattedDate);
    

    Output:

    29.11.2017

    What went wrong in your code?

    There are two things wrong:

    1. You used lowercase mm. This means minute of hour, and since a LocalDate doesn’t have a time of day in it, it threw the exception you saw. The message you got is pretty precise:

      Unsupported field: MinuteOfHour

      Instead you may use uppercase MM for two-digit month.

    2. You need to pick up the format in the String returned from the format method. The LocalDate is immutable and therefore not affected by the method call. Also it cannot have a format in it. It’s just a date in the calendar.

    Link: Wikipedia article: ISO 8601

    0 讨论(0)
  • 2020-11-30 16:24

    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;
    }
    

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

    0 讨论(0)
提交回复
热议问题