user2966968 user2966968 - 11 months ago 165
Java Question

compare java fx date picker date to sql date inside a query

I want to know how I can compare a mysql date to a java fx date picker date. So if I have a datepicker date in MM/DD/YYYY format and a mysql date in YYYY/MM/DD format how do I compare those two inside a query?

Answer Source

I prepared for you small appliaction how to convert Date to LocalDate and set in DatePicker, how to take LocalDate from DatePicker and convert to Date. You can also check that date are the same.

public class Main extends Application {

  SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
  private Date dateUtil = sdf.parse("2016/09/25");

  public Main() throws ParseException {

  @Override public void start(Stage primaryStage) throws Exception {
    Button button = new Button("Take date from DatePicker");
    Label labelCompare = new Label();
    Label labelCompare2 = new Label();
    DatePicker datePicker = new DatePicker();

    //convert Date to LocalDate
    LocalDate localDate = dateUtil.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    //set in DatePicker

    VBox hBox = new VBox();
    hBox.getChildren().addAll(datePicker, button, labelCompare, labelCompare2);
    Scene scene = new Scene(hBox, 400, 400);

    button.setOnAction(new EventHandler<ActionEvent>() {
      @Override public void handle(ActionEvent e) {
        //Take LocalDate from DatePicker
        LocalDate localDate = datePicker.getValue();

        //Convert LocalDate to Date
        Date dateFromPicker = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());

        labelCompare.setText("Compare date: " + dateUtil.compareTo(dateFromPicker));

  public static void main(String[] args) {