-1

I got stuck while converting Array of String to Array of Date

{
  String[] departureDate=request.getParameterValues("departureDate"); 
}

How do I convert the above Strings to Date[] ?

user4261201
  • 2,324
  • 19
  • 26
Munal Dhakal
  • 37
  • 1
  • 10

2 Answers2

0

First of all the sentence:

request.getParameterValues("departureDate"); 

belongs to ServletRequest.

when you use that code you will get an array of String from your input with name departureDate so if you had something like this for example:

    <input type="checkbox" name="departureDate" value="26">day
    <input type="checkbox" name="departureDate" value="04">month
    <input type="checkbox" name="departureDate" value="1988">year

You will get the array with the values 26,04,1988.

as you can see that is a date, right?

Now everyone want to know what values do you have? so we can help you because you cannot transform a String[] to a Date[].

Yussef
  • 610
  • 6
  • 11
  • Yes that is a Servlet Request but in my JSP form I am taking multiple dates "2017/01/22","2015/11/23","1996/12/02" using the same name departureDate. Hence while passing from Jsp to Servlet it goes as a array of Strings so I need to convert that array to Array of Dates – Munal Dhakal Nov 30 '17 at 03:27
0

Solved the problem.

 String[] departureDate=request.getParameterValues("departureDate");

    List<Date> date=(List<Date>) new Date();
    for(String dateToConvert : departureDate) {
        try {
            Date convertedDate=  formatter.parse(dateToConvert);
            date.add(convertedDate);
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
    }
Munal Dhakal
  • 37
  • 1
  • 10