I am currently stumped in filtering out the dates that I have retrieved from 2 CSV file via opencsv. I wish to filter out the dates by month such that I could count the number of posts of each month and display it via javaFX line chart. E.g. y-axis would be number of post, x-axis would be the month. So far, as per my codes below, it just prints out a list of "is it not comparing". So far I have manage to convert the date to string format such that it would print out just the month, Feb, Jan, Dec, etc...
Format of the dates in csv files are:
csv file 1: Feb 14, 2020, 8:03 pm
csv file 2: 2020-01-20T16:34:57+0800
CODE
int febCounter = 0;
int janCounter = 0;
int decCounter = 0;
String pattern = "MMM";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try (Reader reader = Files.newBufferedReader(Paths.get("file_path"));
CSVReader csvReader = new CSVReader(reader);){
String [] nextRecord;
while((nextRecord = csvReader.readNext()) != null) {
String retrievedate = nextRecord[2];
Date date = sdf.parse(retrievedate);
String strDate = sdf.format(date);
if(strDate.equals("Feb")) {
febCounter++;
}
else {
System.out.println("it is not comparing");
}
System.out.println(febCounter);
}
} catch(IOException e) {
System.out.print("File can't be found");
}
EXPECTED OUTPUT
Feburary: 50
January: 20
December: 10
Current OUTPUT
it is not comparing
it is not comparing
it is not comparing
it is not comparing
so on....
UPDATE
So I am able to fix my error, I needed to format the retrieved date twice, once is to format the date such that I would only have the month, second format is to convert the date into a string, such that "strDate.equals("Feb")" will work.