简体   繁体   English

我如何比较从 csv 文件中读取的日期,以便我只计算该月的那些帖子?

[英]How do i compare the dates read from csv file, such that I will only count those post with that month?

I am currently stumped in filtering out the dates that I have retrieved from 2 CSV file via opencsv.我目前很难过滤掉我通过 opencsv 从 2 个 CSV 文件中检索到的日期。 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.我希望按月过滤日期,以便我可以计算每个月的帖子数量并通过 javaFX 折线图显示。 Eg y-axis would be number of post, x-axis would be the month.例如,y 轴是帖子数,x 轴是月份。 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 文件中的日期格式为:

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.所以我能够修复我的错误,我需要将检索到的日期格式化两次,一次是格式化日期以便我只有月份,第二个格式是将日期转换为字符串,例如“strDate.equals (“二月”)”将起作用。

Can you add a real example of the content of files?你能添加一个文件内容的真实例子吗? You have two csv files, each one has a different date format.您有两个 csv 文件,每个文件都有不同的日期格式。

lets strat with the first one, if you have "Feb 14, 2020, 8:03 pm" then the nextRecord variable will be :让第一个进行策略,如果您有“2020 年 2 月 14 日,晚上 8:03”,那么 nextRecord 变量将是:

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

And you have to test if nextRecord[0].startWith("Feb") and not equal.并且您必须测试 nextRecord[0].startWith("Feb") 是否不相等。

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.您需要从 CSV 文件中读取值并使用类似于给出的逻辑来获取月份计数器的值。 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.如有任何问题,请随时发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM