I have a String Array which is contain dates that read from a CSV file.
Now I want to count how many weekend days are in that array. But in my array there are some dates are duplicated. Here shows part of the data that is containing in my Array.
12/2/2010
12/3/2010
12/5/2010
12/10/2010
12/5/2010
12/13/2010
12/14/2010
12/12/2010
In this data set 12/5/2010 is Sunday (but there are two records) & 12/12/2010 is Saturday (has One record). In output, I want to print number of weekend days in this array using java. Compared to this example the Answer should be 2.
FileInputStream fis=new FileInputStream("c:/cdr2.csv");
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader bf = new BufferedReader(isr);
while (bf.ready()) {
String line = bf.readLine();
String[] values=line.split(",");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/d/yyyy");
Date date = simpleDateFormat.parse(values[2]);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
}
First, you need to write a method that determines if a given
Dateis a weekend or not:Then, you need to define a
DateFormat, so that you can parse a dateStringinto ajava.util.Date. Finally, you can use aSetto assure every weekend you find is not going to be duplicated: