Perform a search from a certain date

Asked

Viewed 63 times

0

I’m reading a csv file that generates data from when a particular page was accessed. However I am not able to implement a method that allows me to perform the research from a date 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]);
        }
    }
}
  • What is the shape of the line? Comment one of them here for me to see.

  • {M7H1-V0BD21MJ-5619JXLH} - 02/02/2010 09:43:44 - DTAA/BJM0311 - PC-2790 - "http://yahoo.com"

1 answer

0

  1. To facilitate processing, create a class Linha to represent the line.
  2. On each line read from the file, instate an object Linha with the data read.
  3. Store the instantiated object in a list.
  4. Browse the list, looking for items that have the desired date.

Remarks:

To facilitate the comparison of the date, turn it into Calendar. You can do it like 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; };

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.