简体   繁体   中英

Converting DatePicker to java.util.Date

I have a DatePicker in Vaadin 11 i'm trying to bind to Date field. Seemed like something like this would work:

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

But I'm getting the following error:

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

In case it's needed, here's the 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

You could use java.time.LocalDate (or java.time.LocalDateTime ) instead of the old Date .

It is much easier to manage and has way to automatically format the Date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM