java - How can I iterate through a Date/Time column in a CSV file when the dates are different for each file -
so format of date/time column in csv file looks this:
csv[0] csv[1] 2014-06-02t00:00:00 somenumericaldata 2014-06-03t00:00:00 etc...
depending on file i'm working with, 2013, 2012 etc. want able numerical data in other column each day of data. numerical data 2014-06-02 in 1 array/list, when hit next date, store data in new array/list.
i can iterate through csv , data fine, it's checking dates that's bugging me. if can or point me in right direction how tackle great. (note: data relevant, time doesn't matter)
one way use map
grouping values same date. can use hashmap
, or better use treemap
entries sorted date.
i don't want bother reading csv input, i'll put example data map (those don't have ordered). adapting csv approach should trivial.
map<string, double> ungroupedcsv = new treemap<>(); ungroupedcsv.put("2014-06-02t00:01:00", 1.1); ungroupedcsv.put("2014-06-02t00:02:00", 1.2); ungroupedcsv.put("2014-06-02t03:00:00", 1.3); ungroupedcsv.put("2014-06-02t04:20:00", 1.4); ungroupedcsv.put("2014-06-03t00:00:01", 2.1); ungroupedcsv.put("2014-06-03t00:01:00", 2.2); ungroupedcsv.put("2014-06-03t00:11:00", 2.3); ungroupedcsv.put("2014-06-03t01:10:00", 2.4);
next need 2 date formats: 1 original format, date , time, , 1 date only:
simpledateformat datetime = new simpledateformat("yyyy-mm-dd't'hh:mm:ss"); simpledateformat dateonly = new simpledateformat("yyyy-mm-dd");
the grouped dates stored in map
, mapping date-only string in above format lists of numerical values original list.
map<string, list<double>> grouped = new treemap<>();
now can iterate entries in original map (or csv file), re-code date format, , add value respective date-list (or create list if not yet exist).
for (string key : ungroupedcsv.keyset()) { string date = dateonly.format(datetime.parse(key)); if (! grouped.containskey(date)) { grouped.put(date, new arraylist<double>()); } grouped.get(date).add(ungroupedcsv.get(key)); } system.out.println(grouped);
output:
{2014-06-02=[1.1, 1.2, 1.3, 1.4], 2014-06-03=[2.1, 2.2, 2.3, 2.4]}
Comments
Post a Comment