Home > Software engineering >  How can I use user input dates to filter out objects in an array list?
How can I use user input dates to filter out objects in an array list?

Time:01-01

I have a TableView in JavaFX showing many objects' attributes, representing orders.

Screenshot of sample TableView Output Window

To do so, I have an ArrayList called items, which is filled with objects from an Item class. I've declared many objects like this:

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

I wanted to know if there was a way to utilize 2 user-inputted dates (MM/dd/yyyy) to display on the TableView ONLY objects with date attributes in between the range to match the objects. I know I must remove certain objects to omit them from the TableView entirely but am unsure how.

For example, if we have the user input dates "01/01/2021" and "04/01/2021", the tableview will showcase only the first 4 items shown in the sample code.

My user input logic is already completed, but I would appreciate insight on how to edit the ArrayList itself when utilizing two dates so the TableView outputs only the correct orders.

CodePudding user response:

Firstly, define your custom class to take a LocalDate object rather than a string.

record Item ( LocalDate when , int code ) {}

Usage.

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 )
    )
;

Define your date range.

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

Loop your list, examining each for a match.

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

Or use streams and lambdas.

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

If you are doing much of this code, I suggest adding the ThreeTen-Extra library to your project to make use of the class LocalDateRange.

CodePudding user response:

you can use the SimpleDateFormat class in the java library and take in date from the user in a particular format

try running this code of mine to have a better understanding of the class.

it takes in date in dd/mm/yyyy format and displays it in yyyy mm dd format

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();
}
  • Related