将 DatePicker 转换为 java.util.Date

Converting DatePicker to java.util.Date

我在 Vaadin 11 中有一个 DatePicker,我正在尝试绑定到日期字段。看起来像这样的东西会起作用:

getBinder().forField(datePicker)
            .withConverter(new LocalDateTimeToDateConverter(ZoneId.systemDefault()))
            .bind(MatchEntry::getMatchDate, MatchEntry::setMatchDate);

但我收到以下错误:

no suitable method found for withConverter(com.vaadin.flow.data.converter.LocalDateTimeToDateConverter)
    method com.vaadin.flow.data.binder.Binder.BindingBuilder.<NEWTARGET>withConverter(com.vaadin.flow.data.converter.Converter<java.time.LocalDate,NEWTARGET>) is not applicable
      (cannot infer type-variable(s) NEWTARGET
        (argument mismatch; com.vaadin.flow.data.converter.LocalDateTimeToDateConverter cannot be converted to com.vaadin.flow.data.converter.Converter<java.time.LocalDate,NEWTARGET>))
    method com.vaadin.flow.data.binder.Binder.BindingBuilder.<NEWTARGET>withConverter(com.vaadin.flow.function.SerializableFunction<java.time.LocalDate,NEWTARGET>,com.vaadin.flow.function.SerializableFunction<NEWTARGET,java.time.LocalDate>) is not applicable
      (cannot infer type-variable(s) NEWTARGET
        (actual and formal argument lists differ in length))
    method com.vaadin.flow.data.binder.Binder.BindingBuilder.<NEWTARGET>withConverter(com.vaadin.flow.function.SerializableFunction<java.time.LocalDate,NEWTARGET>,com.vaadin.flow.function.SerializableFunction<NEWTARGET,java.time.LocalDate>,java.lang.String) is not applicable
      (cannot infer type-variable(s) NEWTARGET
        (actual and formal argument lists differ in length))
com/github/javydreamercsw/tournament/manager/ui/views/matchlist/MatchEditorDialog.java:[115,19] invalid method reference
  non-static method getMatchDate() cannot be referenced from a static context

如果需要,这里是 MatchEntry class:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;

@Entity
@Table(name = "match_entry")
@XmlRootElement
@NamedQueries(
        {
          @NamedQuery(name = "MatchEntry.findAll", query = "SELECT m FROM MatchEntry m"),
          @NamedQuery(name = "MatchEntry.findById",
                  query = "SELECT m FROM MatchEntry m WHERE m.matchEntryPK.id = :id"),
          @NamedQuery(name = "MatchEntry.findByRoundId",
                  query = "SELECT m FROM MatchEntry m WHERE m.matchEntryPK.roundId = :roundId"),
          @NamedQuery(name = "MatchEntry.findByFormatId",
                  query = "SELECT m FROM MatchEntry m WHERE m.matchEntryPK.formatId = :formatId")
        })
public class MatchEntry implements Serializable
{
  private static final long serialVersionUID = 1L;

  @EmbeddedId
  protected MatchEntryPK matchEntryPK;

  @ManyToOne(fetch = FetchType.LAZY, optional = false)
  @JoinColumns(
          {
            @JoinColumn(name = "FORMAT_ID", referencedColumnName = "ID",
                    insertable = false, updatable = false),
            @JoinColumn(name = "GAME_ID", referencedColumnName = "ID",
                    insertable = false, updatable = false)
          })
  private Format format;

  @ManyToOne(optional = true, fetch = FetchType.LAZY)
  @JoinColumns(
          {
            @JoinColumn(name = "ROUND_ID", referencedColumnName = "ID",
                    insertable = false, updatable = false),
            @JoinColumn(name = "TOURNAMENT_ID", referencedColumnName = "ID",
                    insertable = false, updatable = false)
          })
  private Round round;

  @OneToMany(mappedBy = "matchEntry", fetch = FetchType.LAZY,
          cascade = CascadeType.ALL)
  private List<MatchHasTeam> matchHasTeamList;

  @Basic(optional = false)
  @Column(name = "match_date")
  @Temporal(TemporalType.TIMESTAMP)
  private Date matchDate;

  public MatchEntry()
  {
    setMatchHasTeamList(new ArrayList<>());
  }

  public MatchEntry(MatchEntryPK matchEntryPK)
  {
    this.matchEntryPK = matchEntryPK;
  }

  public MatchEntry(int roundId, int formatId)
  {
    this.matchEntryPK = new MatchEntryPK(roundId, formatId);
  }

  public MatchEntryPK getMatchEntryPK()
  {
    return matchEntryPK;
  }

  public void setMatchEntryPK(MatchEntryPK matchEntryPK)
  {
    this.matchEntryPK = matchEntryPK;
  }

  public Format getFormat()
  {
    return format;
  }

  public void setFormat(Format format)
  {
    this.format = format;
  }

  public Round getRound()
  {
    return round;
  }

  public void setRound(Round round)
  {
    this.round = round;
  }

  public List<MatchHasTeam> getMatchHasTeamList()
  {
    return matchHasTeamList;
  }

  public final void setMatchHasTeamList(List<MatchHasTeam> matchHasTeamList)
  {
    this.matchHasTeamList = matchHasTeamList;
  }

  @Override
  public int hashCode()
  {
    int hash = 0;
    hash += (matchEntryPK != null ? matchEntryPK.hashCode() : 0);
    return hash;
  }

  @Override
  public boolean equals(Object object)
  {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof MatchEntry))
    {
      return false;
    }
    MatchEntry other = (MatchEntry) object;
    return !((this.matchEntryPK == null && other.matchEntryPK != null) 
            || (this.matchEntryPK != null 
            && !this.matchEntryPK.equals(other.matchEntryPK)));
  }

  @Override
  public String toString()
  {
    return "MatchEntry[ matchEntryPK=" 
            + matchEntryPK + " ]";
  }

  public Date getMatchDate()
  {
    return matchDate;
  }

  public void setMatchDate(Date matchDate)
  {
    this.matchDate = matchDate;
  }
}

java.time

您可以使用 java.time.LocalDate (or java.time.LocalDateTime) 而不是旧的 Date

它更易于管理,并且可以自动格式化日期。