2

I have an arraylist which is providing us to string type date. I want to convert that string array into a date array; how can I do the conversion?

  ArrayList<String> dates = new ArrayList<String>();

where I am getting value some thing like:

2015-11-09
2015-11-08
2015-11-07
2015-11-06
2015-11-10

I want to convert this into an ArrayList of Dates and return same value in same format.

4 Answers 4

6

Under Java 8+, you can do this easily :

ArrayList<String> dateStrings = new ArrayList<>(  );
[...]

List<LocalDate> dates = dateStrings.stream()
        .map( LocalDate::parse )
        .collect( Collectors.toList() );
Sign up to request clarification or add additional context in comments.

Comments

3

Have you read up on SimpleDateFormat?

    ArrayList<String> dateStringList = new ArrayList<String>();
    ArrayList<Date> dateList = new ArrayList<Date>();

    dateStringList.add("2015-11-09");
    dateStringList.add("2015-11-08");

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy-MM-dd");

    for (String dateString : dateStringList) {
        try {
            dateList.add(simpleDateFormat.parse(dateString));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    for (Date date : dateList) {
        System.out.println("Date " + simpleDateFormat.format(date));
    }

3 Comments

i have already tried this but it is feturening in this format Mon Nov 16 00:00:00 IST 2015
i need in same foramt as it was
You need to read SimpleDateFormat documentation and examples. Have you even tried? Look at the format() method.
1

If you have a list of Strings representing a date you can convert them into a Date using the SimpleDateFormat. Find below a small snippet.

// a list of String representing a Date
List<String> dateStrings = new ArrayList<>();
dateStrings.add("2015-11-09");
dateStrings.add("2015-11-08");
dateStrings.add("2015-11-07");
dateStrings.add("2015-11-06");
dateStrings.add("2015-11-10");

// create a list of Date with the size of the String list
List<Date> dates = new ArrayList<>(dateStrings.size());

// define the date format used in the String
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

// loop over all String values
for (String dateString : dateStrings) {
    // sdf.parse(dateString) - convert the String into a Date accoring the pattern
    // dates.add(...) - add the Date to the list
    dates.add(sdf.parse(dateString));
}

2 Comments

i already dine thih but reutern in this fromat Mon Nov 16 00:00:00 IST 2015
@ShaktiSharma Could you please add what you have done and what is not working as expected into your question.
1
    ArrayList<String> dateStrings = new ArrayList<String>();  // ArrayList of strings

             dateStrings.add("2015-11-09");

             dateStrings.add("2015-11-08");

             dateStrings.add("2015-11-07");

             dateStrings.add("2015-11-06");

             dateStrings.add("2015-11-10");

   List<Date> dates = new ArrayList<>(dateStrings.size()); // ArrayList of dates

             for(String s : dateStrings)
             {

                 try 
                 {
                        Date dateObj = new SimpleDateFormat("yyyy-MM-dd").parse(s);

                        dates.add(dateObj);
                  } 
                  catch (ParseException e) 
                  {
                        e.printStackTrace();
                  }
             }

             for(Date d : dates)
             {
                 String str = new SimpleDateFormat("yyyy-MM-dd").format(d);

                 System.out.println(str);
             }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.