2

I have a CSV file with lots of data in it. One of the columns contains the date and time in the format, Day of week Month Day Time Timezone year or "Mon Feb 23 05:30:55 +0000 2015" as an example.

Mon Feb 23 05:30:56 +0000 2015

Mon Feb 23 05:30:56 +0000 2015

Mon Feb 23 05:30:56 +0000 2015

Mon Feb 23 05:30:56 +0000 2015

Mon Feb 23 05:30:56 +0000 2015

Mon Feb 23 05:30:55 +0000 2015

Mon Feb 23 05:30:55 +0000 2015

Those are some examples of the date and time data I have. But, the time runs from 01:00:00 to what is listed above.

I'm trying to read that column of times, and find the count of another data in another column in regards to the time.

So, for example, another column contains data such as a number.

I want to find the hour and minute when a certain number is displayed the most.

I honestly just don't know where to start in regards to the timestamp. How do I increment the timestamp if I have it in string format right now like it is? I need just the hour and minute though. How do I get that part out of the string and increment it?

user2925924
  • 45
  • 2
  • 12
  • possible duplicate of [Java string to date conversion](http://stackoverflow.com/questions/4216745/java-string-to-date-conversion) – lw29 Mar 09 '15 at 02:22

2 Answers2

2

A starting point:

String string = "Mon Feb 23 05:30:55 +0000 2015";

DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date); // Mon Feb 23 05:30:55 GMT 2015

int hours = date.getHours();
date.setHours(hours + 1);
System.out.println(date); // Mon Feb 23 06:30:55 GMT 2015

UPD: A better solution using Calendar class:

String string = "Mon Feb 23 05:30:55 +0000 2015";

DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
Calendar date = Calendar.getInstance();
date.setTime(format.parse(string));
System.out.println(format.format(date.getTime())); // Mon Feb 23 05:30:55 GMT 2015          

int hours = date.get(Calendar.HOUR_OF_DAY); // 5
int minutes = date.get(Calendar.MINUTE); // 30

date.add(Calendar.HOUR_OF_DAY, 1); // add a hour
date.add(Calendar.MINUTE , 1); // add a minute
System.out.println(format.format(date.getTime())); // Mon Feb 23 06:31:55 GMT 2015
Max
  • 1,534
  • 1
  • 18
  • 24
1

You can do this easily with uniVocity-parsers:

  CsvParserSettings settings = new CsvParserSettings();
  //processes only these two fields, all others are ignored
  parserSettings.selectFields("your_calendar_field", "your_other_field");

  ObjectRowProcessor rowProcessor = new ObjectListRowProcessor();
  //converts the content of "your_calendar_field" to a java.util.Calendar.
  rowProcessor.convertFields(Conversions.toCalendar("EEE MMM dd HH:mm:ss Z yyyy")).set("your_calendar_field");

  //Creates a parser with the given settings
  CsvParser parser = new CsvParser(settings);
  parser.parse(new FileReader("your_input_file"));

  //returns all rows, ordered as defined in the field selection above,
  //with date Strings properly converted to Calendar.
  List<Object[]> parsedRows = rowProcessor.getRows();

Disclosure: I am the author of this library. It's open-source and free (Apache V2.0 license).

josliber
  • 43,891
  • 12
  • 98
  • 133
Jeronimo Backes
  • 6,141
  • 2
  • 25
  • 29