Java SimpleDataFormat not displaying correctly

63 views Asked by At

I am trying to convert an array of strings to a Date ArrayList with the following format - "dd/MM/yyyy" I have included my method below. When I print the ArrayList the dates are not formatted correctly and are displayed as: "Thu Mar 05 00:00:00 GMT 2020" Can anyone think why this is happening?

private void convertDates()
        {
            SimpleDateFormat formatter1=new SimpleDateFormat("dd/MM/yyyy");

            for (String dateString : date) {
                try {
                    dateList.add(formatter1.parse(dateString));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            displayDates.setText(Arrays.toString(dateList.toArray()));
        }
1

There are 1 answers

0
Mureinik On

You have a list of Date objects, and when you format them to a string, they'll use their default formatting, regardless of the format you used to parse them. If you want to display them in that format, you'll have to do so explicitly. E.g.:

displayDates.setText(
    dateList.stream().map(formatter1::format).collect(Collectors.joining(","));