Perform a search from a certain date

0

I'm reading a csv file that generates data when a particular page was accessed. However I am not able to implement a method that allows me to perform the search from a date that I wish. Follow the code

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class httpcompleto {
    public static void main(String[] args) throws IOException {

        BufferedReader httpcompleto = new BufferedReader(new FileReader("http-completo.csv"));
        String line = "";
        String date;
        while((line = httpcompleto.readLine()) != null ){
            String[] row = line.split(",");
            System.out.println(row[0] + " - " + row[1] + " - " + row[2] + " - " + row[3] + " - " + row[4]);
        }
    }
}
    
asked by anonymous 31.05.2017 / 01:49

1 answer

0
  • For ease of processing, create a class Linha to represent the line.
  • For each line read from the file, instantiate a Linha object with the read data.
  • Store the instantiated object in a list.
  • Scroll through the list, looking for items that have the desired date.
  • Notes:

    To make it easier to compare the date, make it a Calendar . You can do this:

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    Calendar cal = Calendar.getInstance();
    cal.setTime(sdf.parse("02/02/2010 09:43:44"));
    

    An example of class Linha :

    class Linha {
    
        public static final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    
        private String campo0;
        private Calendar campo1;
        private String campo2;
        private String campo3;
        private String campo4;
    
        public Linha(String campo0, String campo1, String campo2, String campo3, String campo4) throws Exception {
            this.campo0 = campo0;
    
            Calendar cal = Calendar.getInstance();
            cal.setTime(sdf.parse(campo1));
            this.campo1 = cal;
    
            this.campo2 = campo2;
            this.campo3 = campo3;
            this.campo4 = campo4;
        }
    
        public String getCampo0 () { return campo0; };
        public Calendar getCampo1 () { return campo1; };
        public String getCampo2 () { return campo2; };
        public String getCampo3 () { return campo3; };
        public String getCampo4 () { return campo4; };
    
    }
    
        
    02.06.2017 / 17:50