2

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.

Denise Tay
  • 380
  • 3
  • 14
  • not related to fx .. – kleopatra Feb 16 '20 at 13:28
  • To start with, read [How do I compare strings in Java?](https://stackoverflow.com/q/513832/5221149) – Andreas Feb 16 '20 at 13:41
  • Then parse the date string and extract the month, e.g. for value `2020-01-20T16:34:57+0800` use `OffsetDateTime.parse(nextRecord[2], DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssXX")).getMonth().getDisplayName(TextStyle.SHORT, Locale.US)` – Andreas Feb 16 '20 at 13:48
  • Hi Andreas, I would receive this error "The method parse(String, ParsePosition) in the type SimpleDateFormat is not applicable for the arguments (String, DateTimeFormatter)" – Denise Tay Feb 16 '20 at 13:57

2 Answers2

0

Can you add a real example of the content of files? You have two csv files, each one has a different date format.

lets strat with the first one, if you have "Feb 14, 2020, 8:03 pm" then the nextRecord variable will be :

[
    0 => "Feb 14",
    1 => "2020",
    2 => "8:03 pm",
]

And you have to test if nextRecord[0].startWith("Feb") and not equal.

0

You need to read the value from the CSV file and use logic similar to the one given to get the value of month counters. For the purpose of the demo, I have put some dates in an array.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.List;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        List<String> monthList = List.of("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov",
                "Dec");
        int[] months = new int[12];
        String[] dateStrs = { "Feb 14, 2020, 8:03 pm", "Jan 20, 2020, 9:03 am", "Mar 6, 2020, 2:03 pm",
                "Feb 15, 2020, 11:03 am", "Jul 18, 2020, 4:03 am", "Jan 21, 2020, 5:03 pm", "Mar 11, 2020, 6:03 pm" };
        for (String dateStr : dateStrs) {
            months[monthList.indexOf(LocalDate.parse(dateStr, DateTimeFormatter.ofPattern("MMM d, yyyy, h:mm a"))
                    .getMonth().getDisplayName(TextStyle.SHORT, Locale.US))]++;
        }
        for (int i = 0; i < months.length; i++) {
            System.out.println(monthList.get(i) + ":" + months[i]);
        }

        //Similarly apply the following format for the second CSV file
        // DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssXX")
        // String[] dateStr2 = {"2020-01-20T16:34:57+0800",...};            
    }
}

Output:

Jan:2
Feb:2
Mar:2
Apr:0
May:0
Jun:0
Jul:1
Aug:0
Sep:0
Oct:0
Nov:0
Dec:0

I hope, this is enough hint for you to complete your task. Feel free to comment in case of any issue.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110