如何使用用户输入日期来筛选数组列表中的对象



我在JavaFX中有一个TableView,显示了许多对象的属性,表示顺序。

示例TableView输出窗口的屏幕截图

为此,我有一个名为items的ArrayList,其中填充了Item类中的对象。我已经声明了很多这样的对象:

ArrayList items = new ArrayList();
items.add(new Item("01/01/2021", 20, 10, 10, 1));
items.add(new Item("01/21/2021", 15, 5, 10, 2));
items.add(new Item("02/11/2021", 12, 6, 6, 1));
items.add(new Item("03/29/2021", 35, 5, 30, 7));
items.add(new Item("04/14/2021", 16, 16, 0, 0));
items.add(new Item("04/21/2021", 20, 10, 10, 1)); // etc

我想知道是否有一种方法可以利用2个用户输入的日期(MM/dd/yyyy(在TableView ONLY对象上显示日期属性在范围之间的对象,以匹配这些对象。我知道我必须删除某些对象才能从TableView中完全省略它们,但不确定如何删除。例如,如果我们有用户输入日期";01/01/2021";以及";2021年1月4日";,tableview将只显示上面示例代码中显示的前4项。

我的用户输入逻辑已经完成,但我希望了解如何在使用两个日期时编辑ArrayList本身,以便TableView输出筛选的项。以下是迄今为止我的代码的简化版本。

主要类别:

import java.util.ArrayList;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import javafx.event.*;
 
public class Main extends Application  {
    
    Label errorMessage;
    Button button;
    
    
    public static void main(String args[]){          
         launch(args);     
    } 
         
    @Override    
    public void start(Stage primaryStage) throws Exception { 
        
        primaryStage.setTitle("Item Information");
        
        VBox layout = new VBox();
        VBox layout2 = new VBox();
        layout.setAlignment(Pos.CENTER);
        layout2.setAlignment(Pos.CENTER);
        
        // 1st scene is for collecting user input, 2nd scene is outputting the TableView
        Scene scene = new Scene(layout, 500, 500);
        Scene scene2 = new Scene(layout2, 500, 500);
        
        // Code for 1st Scene
        Label label1 = new Label("Report Start Date");
        TextField textField1 = new TextField();
        textField1.setPromptText("mm/dd/yyyy");
        // formatting code
        Label label2 = new Label("Report End Date");
        TextField textField2 = new TextField();
        textField2.setPromptText("mm/dd/yyyy");
        // formatting code
        
        errorMessage= new Label(" ");
        button = new Button("Run Report");
        
        button.setOnAction(e -> validityCheck(textField1, textField1.getText(), primaryStage, scene2));
        button.setOnAction(e -> validityCheck(textField2, textField2.getText(), primaryStage, scene2));
        
        
        // Code for 2nd Scene/TableView
        ObservableList data = getData();
        TableView itemTable = new TableView<>();
        itemTable.prefHeightProperty().bind(primaryStage.heightProperty());
        itemTable.prefWidthProperty().bind(primaryStage.widthProperty());
        TableColumn itemSaleDateCol = new TableColumn("Sale Date");
        TableColumn itemSalePriceCol = new TableColumn("Sale Price");
        TableColumn itemSaleCostCol = new TableColumn("Sale Cost"); 
        TableColumn itemProfitOrLossCol = new TableColumn("Profit or Loss"); 
        TableColumn itemProfitMarginCol = new TableColumn("Profit Margin"); 
        
        
        itemSaleDateCol.setCellValueFactory(new PropertyValueFactory("itemSaleDate"));
        itemSalePriceCol.setCellValueFactory(new PropertyValueFactory("itemSalePrice"));
        itemSaleCostCol.setCellValueFactory(new PropertyValueFactory("itemSaleCost"));
        itemProfitOrLossCol.setCellValueFactory(new PropertyValueFactory("itemProfitOrLoss")); 
        itemProfitMarginCol.setCellValueFactory(new PropertyValueFactory("itemProfitMargin"));
                
        itemTable.setItems(data);
        itemTable.getColumns().addAll(itemSaleDateCol, itemSalePriceCol, itemSaleCostCol, itemProfitOrLossCol, itemProfitMarginCol);
        layout.getChildren().addAll(label1, textField1, label2, textField2, button, label3);
        layout2.getChildren().addAll(itemTable);
        primaryStage.setScene(scene);   
        primaryStage.show();
    }
    
    public static ObservableList getData() {
        // create our data, return an observable list
        // this could be any product data, and could come from anywhere
        // we store our data in an array list, then send that array list to FXCollections.observableArrayList to change it to the necessary collection type
        ArrayList items = new ArrayList();
        // sample items from the order
        items.add(new Item("01/21/2021", 20, 10, 10, 1));
        items.add(new Item("03/02/2021", 15, 5, 10, 2));
        items.add(new Item("06/11/2021", 12, 6, 6, 1));
        items.add(new Item("10/30/2021", 35, 5, 30, 7));
        items.add(new Item("12/14/2021", 16, 16, 0, 0));
        
        return FXCollections.observableArrayList(items);
    }
    
    private boolean validityCheck(TextField input, String message, Stage stage, Scene scene) {
    
        DateTimeFormatter df = DateTimeFormatter.ofPattern("MM/dd/yyyy");
        try {
           LocalDate startDate = LocalDate.parse(message, df);
           stage.setScene(scene);
           return true;
         }
         catch(Exception ex) { 
            errorMessage.setText("Invalid Input(s). Ensure you've typed a VALID date formatted as MM/DD/YYYY.");
            return false;
         }
    }
}

物品类别:

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Item {
    // member variables with "property" data types
    private StringProperty itemSaleDate;
    private IntegerProperty itemSalePrice;
    private IntegerProperty itemSaleCost;
    private IntegerProperty itemProfitOrLoss;
    private IntegerProperty itemProfitMargin;
    
    public Item(String iSd, int iSp, int iSc, int iPoL, int iPm) {
        setItemSaleDate(iSd);
        setItemSalePrice(iSp);
        setItemSaleCost(iSc);
        setItemProfitOrLoss(iPoL);
        setItemProfitMargin(iPm);
    }
    
    // Item Sale Date Property - instantiates itemSaleDate if its null first, then returns it
    public StringProperty itemSaleDateProperty() {
        if (itemSaleDate == null) {
            itemSaleDate = new SimpleStringProperty(this, "itemSaleDate");
        }
        return itemSaleDate;
    }
    // getters and setters
    public void setItemSaleDate(String value) {
        itemSaleDateProperty().set(value);
    }
    public String getItemSaleDate() {
        return itemSaleDateProperty().get();
    }
        
    
    public IntegerProperty itemSalePriceProperty() {
        if (itemSalePrice == null) {
            itemSalePrice = new SimpleIntegerProperty(0);
        }
        return itemSalePrice;
    }
    public void setItemSalePrice(int value) {
        itemSalePriceProperty().set(value);
    }
    public int getItemSalePrice() {
        return itemSalePriceProperty().get();
    }
    
    
    public IntegerProperty itemSaleCostProperty() {
        if (itemSaleCost == null) {
            itemSaleCost = new SimpleIntegerProperty(0);
        }
        return itemSalePrice;
    }
    public void setItemSaleCost(int value) {
        itemSaleCostProperty().set(value);
    }
    public int getItemCostPrice() {
        return itemSaleCostProperty().get();
    }
    
    public IntegerProperty itemProfitOrLossProperty() {
        if (itemProfitOrLoss == null) {
            itemProfitOrLoss = new SimpleIntegerProperty(0);
        }
        return itemProfitOrLoss;
    }
    public void setItemProfitOrLoss(int value) {
        itemProfitOrLossProperty().set(value);
    }
    public int getItemProfitOrLoss() {
        return itemProfitOrLossProperty().get();
    }
    
    
    public IntegerProperty itemProfitMarginProperty() {
        if (itemProfitMargin == null) {
            itemProfitMargin = new SimpleIntegerProperty(0);
        }
        return itemProfitMargin;
    }
    public void setItemProfitMargin(int value) {
        itemProfitMarginProperty().set(value);
    }
    public int getItemProfitMargin() {
        return itemProfitMarginProperty().get();
    }
}

首先,将自定义类定义为接受LocalDate对象而不是字符串。

record Item ( LocalDate when , int code ) {}

用法。

List < Item > items = 
    List.of( 
        new Item( LocalDate.of( 2022 , Month.JANUARY ,  23 ) , 10 ) ,
        new Item( LocalDate.of( 2022 , Month.FEBRUARY ,  24 ) , 11 ) ,
        new Item( LocalDate.of( 2022 , 3 ,  25 ) , 12 )
    )
;

定义您的日期范围。

LocalDate start = LocalDate.of( 2022 , 2 , 1 ) ;
LocalDate end = start.plusMonths( 1 ) ;

循环您的列表,检查每个列表是否匹配。

List< Item > hits = new ArrayList <> () ;
for( Item item : items ) {
    if( ( ! item.when.isBefore( start ) ) && item.when.isBefore( end ) ) {
        hits.add( item ) ;
    }
}

或者使用溪流和羔羊肉。

List< Item > hits = items.stream().filter( item -> ( ! item.when.isBefore( start ) ) && item.when.isBefore( end ) ).toList() ;

如果您正在执行大部分代码,我建议将ThreeTen Extra库添加到您的项目中,以使用类LocalDateRange

您可以使用java库中的SimpleDateFormat类,并以特定格式接收用户的日期

试着运行我的这段代码来更好地理解这个类。

它以dd/mm/yyyy格式接收日期,并以yyyy-mm-dd格式显示

public class DateTime {
static void Condate(String inputDate) {
    try {
        
        SimpleDateFormat sfd=new SimpleDateFormat("dd/mm/yyyy");
        //String t=sfd.format();
        Date date=sfd.parse(inputDate);
        SimpleDateFormat outputsfd=new SimpleDateFormat("yyyy mm dd");
        String outputdate=outputsfd.format(date);
        System.out.println("after changing the date to yyyy/mm/dd : "+outputdate);
        
    }catch(java.text.ParseException e){
        System.out.println("error occured");
    }
}   
public static void main(String[] args) {
    Scanner r=new Scanner(System.in);
    System.out.println("enter date is dd/mm/yyyy : ");
    String d=r.nextLine();
    Condate(d); 
    r.close();
}

相关内容

  • 没有找到相关文章

最新更新