Reputation: 37
I have a CSV file which contains the following data:
red, 03/11/2014, 11:00, 10
blue, 04/11/2014, 12:00, 15
pink, 03/11/2014, 15:00, 50
blue, 05/11/2014, 14:00, 15
pink, 02/11/2013, 12:00, 10
green, 03/12/2014, 23:00, 1
red, 03/11/2013, 23:11, 11
I am trying to print the date and the time but keeps failing. The code works fine when
public static final String DATE_TIME_FORMAT_DATA = "dd/MM/yyyy HH:mm";
is changed to
public static final String DATE_FORMAT = "dd/MM/yyyy";
but that will only print the date.
The code:
import java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class RangeCalculator {
private File file;
public static final String DATE_TIME_FORMAT_DATA = "dd/MM/yyyy HH:mm";
public class Entry {
String colour;
String date;
String time;
String noise;
public Entry(String colour, String date, String time, String noise) {
super();
this.colour = colour;
this.date = date;
this.time = time;
this.noise = noise;
}
// implement getters and setters here if necessary
@Override
public String toString() {
return "Entry [colour=" + colour + ", date=" + date + ", time="
+ time + ", noise=" + noise + "]";
}
}
public RangeCalculator(String fileName) {
file = new File(fileName);
}
private List<Entry> computeRange(String from, String to)
throws FileNotFoundException, ParseException {
List<Entry> result = new LinkedList<>();
SimpleDateFormat formatter = new SimpleDateFormat(DATE_TIME_FORMAT_DATA);
Date fromDate = formatter.parse(from);
Date toDate = formatter.parse(to);
Scanner scanner = new Scanner(file);
scanner.useDelimiter("[,\n]");
// maybe you want/need to skip the first line of the file
// if (scanner.hasNextLine()) {
// scanner.nextLine();
// }
while (scanner.hasNextLine()) {
String color = scanner.next();
String date = scanner.next().substring(1);
String time = scanner.next().substring(1);
String noise = scanner.next().substring(1);
Date currDate = formatter.parse(date);
if (!currDate.before(fromDate) && !currDate.after(toDate)) {
result.add(new Entry(color, date, time, noise));
}
}
scanner.close();
return result;
}
private void printEntries(List<Entry> entries) {
for (Entry entry : entries) {
System.out.println(entry.toString());
}
}
public static void main(String[] args) {
RangeCalculator app = new RangeCalculator("Data.csv");
List<Entry> calculatedEntries = null;
try {
calculatedEntries = app.computeRange("03/11/2014 11:00", "04/11/2014 12:00");
} catch (FileNotFoundException e) {
System.err.println("ERROR: File not found!");
System.exit(1);
} catch (ParseException e) {
System.err.println("ERROR: Wrong date format!");
System.exit(1);
}
app.printEntries(calculatedEntries);
System.exit(0);
}
}
Appreciate any help.
Upvotes: 0
Views: 138
Reputation: 319
As you are getting Date and time component as two separate field you need to append them both before using. Hence below shall be
Date currDate = formatter.parse(date+" "+time);
Upvotes: 1
Reputation: 31
You're trying to parse date only using formatter that includes date and time. All you need to do is replace this line:
Date currDate = formatter.parse(date);
with this:
Date currDate = formatter.parse(date + " " + time);
Upvotes: 3