Search code examples
javacsvarraylistbufferedreader

How to I split date and time that are on the same column and store them in an ArrayList?


I'm creating a data visualisation program using 'JFreeChart' and i'm having trouble reading data from a CSV file using BufferedReader. In the CSV file I have the date and time stored in the same column. I know that I have to use the " " function to separate them but I can't figure out how to go around doing so.

I have tried looking everywhere but I can't put my finger on it. I need to be pushed onto the right track.

//This is part of my Data Class

private int millis;
    private int stamp;
    private int light; 
    private double temp;
    private double vcc;
    private Time theTime;
    private Date theDate;


//This is part of another class

public class CSVreader {

    private List<Data> dataList = new ArrayList<Data>();
    private String path;

    public List<Data> getDataList() {
        return dataList;
    }
    public String getPath() {
        return path;
    }
    public void setPath(String path) {
        this.path = path;
    }

    public void readCSV() throws IOException{ 

        BufferedReader in = new BufferedReader (new FileReader(path));

        String line = in.readLine();

        while(line != null) {

            Data d = new Data();

            String[] splits = line.split(",");

            int millis = Integer.parseInt(splits[0]);
            int stamp = Integer.parseInt(splits[1]);
            int light = Integer.parseInt(splits[2]);
            double temp = Double.parseDouble(splits[3]);
            double vcc = Double.parseDouble(splits[4]);

            d.setMillis(millis);
            d.setStamp(stamp);
            d.setLight(light);
            d.setTemp(temp);
            d.setVcc(vcc);

            dataList.add(d);

        }

        }

}

The end result should be that all the data will be on an arraylist and I can bring up those results and from the List I can create a graph using JFreeChart.


Solution

  • Based on your comment split the 3 element in splits array by space " " delimiter

    String dateTime[] = splits[2].split(" ");
    String date = dateTime[0];
    String time = dateTime[1];
    

    But i can see datatype of date and Time are different

    private Time theTime;
    private Date theDate;
    

    If Date is java.util.Date you can use SimpleDateFormat to convert string to Date

    DateFormat formatter = new SimpleDateFormat("yyyy/dd/mm");
    Date date = formatter.parse(testDate);