如何在日期选择器的日期和存储数据作为长格式日期的情况下逐行获取数据

时间:2018-04-28 12:35:27

标签: sqlite

当我通过从日期选择器传递日期来调用getAllInvoice(日期日期)来获取可观察的发票清单但是它返回空列表时,请帮助如何获取发票可观察列表通过日期选择器的日期我存储date作为我在下面提供的示例中的长格式。

public VBox getBody() {
    DatePicker datePicker = new DatePicker();
    datePicker.setPrefWidth(width);
    datePicker.setValue(LocalDate.now());

    Calendar calendar = Calendar.getInstance();
    calendar.set(datePicker.getValue().getYear(),datePicker.getValue().getMonthValue(),datePicker.getValue().getDayOfMonth());


    ListView<String> invoiceListView = new ListView<>();
    invoiceListView.setPrefWidth(width);
    invoiceListView.setPrefHeight(550);

    ObservableList<Invoice> invoiceObservableList = connection.getAllInvoice(Date.valueOf(datePicker.getValue()));

    datePicker.valueProperty().addListener((observable, oldValue, newValue) -> {
        invoiceObservableList.clear();
        invoiceObservableList.addAll(connection.getAllInvoice(Date.valueOf(datePicker.getValue())));
    });

    ListChangeListener<Invoice> listener = new ListChangeListener<Invoice>() {
        @Override
        public void onChanged(Change<? extends Invoice> c) {
            ObservableList<String> invoiceList = FXCollections.observableArrayList();
            for (Invoice invoice : invoiceObservableList) {
                Customer customer = connection.getCustomerById(invoice.getCustomerId());
                invoiceList.add("#" + invoice.getId() + " - " + customer.getName() + " (" + customer.getNumber() + ")");
            }
            invoiceListView.setItems(invoiceList);
        }
    };

    invoiceObservableList.addListener(listener);

    ObservableList<String> invoiceList = FXCollections.observableArrayList();
    for (Invoice invoice : invoiceObservableList) {
        Customer customer = connection.getCustomerById(invoice.getCustomerId());
        invoiceList.add("#" + invoice.getId() + " - " + customer.getName() + " (" + customer.getNumber() + ")");
    }
    invoiceListView.setItems(invoiceList);

    VBox invoicePreview = new VBox();//addMe
    invoicePreview.setPrefWidth(360);
    invoicePreview.setPrefHeight(560);
    invoicePreview.setAlignment(Pos.CENTER);
    //invoicePreview.setStyle("-fx-background-color: green");
    //invoicePreview.setPadding(new Insets(15));

    invoiceListView.setOnMouseClicked(event -> {
        if (event.getClickCount() == 2) {
            invoicePreview.getChildren().clear();
            invoicePreview.setAlignment(Pos.TOP_CENTER);
            Invoice invoice = invoiceObservableList.get(invoiceListView.getSelectionModel().getSelectedIndex());
            InvoicePreview preview = new InvoicePreview(invoice,sizer.getValue(100));
            invoicePreview.getChildren().add(preview.printPreview());
        }
    });

    Label massage = new Label("Please select invoice first.");
    massage.setFont(new Font(invoiceWidth*5/100));
    invoicePreview.getChildren().add(massage);

    VBox leftVBox = new VBox(datePicker, invoiceListView);
    leftVBox.setSpacing(5);

    HBox mainBody = new HBox(leftVBox,invoicePreview);
    mainBody.setSpacing(5);


    VBox root = new VBox(mainBody);
    root.setId("body");
    return root;
}

这是Connection类的getAllInvoice(Date date)方法,它返回可观察的发票清单,

public ObservableList<Invoice> getAllInvoice(Date date) {
    long longDate = date.getTime();
    ObservableList<Invoice> list = FXCollections.observableArrayList();

    String query = "SELECT * FROM " + TABLE_INVOICE + " WHERE " + INVOICE_DATE + " LIKE \'"+longDate+"\'";
    System.out.println(query);
    try {
        connection = DriverManager.getConnection("jdbc:sqlite:" + DATA_BASE);

        Statement state = connection.createStatement();
        ResultSet resultSet = state.executeQuery(query);

        while (resultSet.next()) {
            list.add(new Invoice(resultSet.getInt(INVOICE_ID), resultSet.getInt(INVOICE_USER_ID), resultSet.getInt(INVOICE_CUSTOMER_ID), resultSet.getLong(INVOICE_DATE)));
        }

    } catch (SQLException e) {
        e.printStackTrace();
    } finally {

    }
    return list;
}

datadase文件如:

"id" "uId" "cusId" "date"
-------------------------------
"3" "1" "4" "1524636334412"
"4" "1" "4" "1524636355419"
"5" "1" "3" "1524636411858"
"6" "1" "3" "1524637462701"
"7" "1" "4" "1524638110920"

如何将数据作为参数传递给Date?

1 个答案:

答案 0 :(得分:0)

假设您希望同一日期(一年中的某一天)的记录与日期过去(长),那么您可以使用: -

Mono
  • SELECT * FROM invoice WHERE date / 86400000 = <your_date> / 86400000 是传递的日期

这是剥离毫秒(即除以1000),然后剥离秒(即除以另外60),然后剥离分钟(即除以另外60)然后剥离小时(除以24)

即。 1000 * 60 * 60 * 24 = 86400000

根据您的示例数据(其中1524636334412是第一行的日期时间): -

<your_date>

导致: -

enter image description here

因此你可以改变: -

SELECT *
FROM invoice 
WHERE date / 86400000  = 1524636334412 / 86400000

    String query = "SELECT * FROM " + TABLE_INVOICE + " WHERE " + INVOICE_DATE + " LIKE \'"+longDate+"\'";