-2

I have an array list with string and I would like to convert it in Date. Seems 'parse' cannot be used to do this, within the following piece of code:

List<String> eventsList = Arrays.asList("2015-06-02 00:15", "2015-06-02 22:15", "2015-06-03 00:18");
List<Date> eventsDate = new SimpleDateFormat("y-M-d H:m").parse(eventsList);

But that doesn't work.

Could you help me please?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Mic
  • 1
  • Do you know what a loop is? – Sotirios Delimanolis Sep 30 '15 at 15:45
  • 1
    You can only parse individual Strings - you will need to do that in a loop. – Cinnam Sep 30 '15 at 15:45
  • yes I know what a loop is, but I thought loop can be avoided here - sorry, I didn't precise this – Mic Sep 30 '15 at 15:51
  • 1
    The [documentation](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html#parse-java.lang.String-java.text.ParsePosition-) for `parse` clearly indicates the type of parameters it accepts. – GriffeyDog Sep 30 '15 at 15:52
  • yes, that's why I thought it exists an alternative to `parse`, which can be used without a loop. Sorry if this question looks silly, I am a newbie with Java... :/ – Mic Sep 30 '15 at 15:55

1 Answers1

0

There's no parse method with List<String> as a parameter which is why your code won't compile.

There's a parse that accepts a String. You can use that with a loop or a stream to get the result you need.

Further reading on the available methods of SimpleDateFormat can be done here.

João Neves
  • 944
  • 1
  • 13
  • 18
  • ok guys, thank you very much, I understand that loop is the only solution! It's working like that! Thanks a lot for you help. Now I need to work a bit more to have a better understanding of how Java is working. – Mic Oct 01 '15 at 05:36