I have to write a function to accept a list of date which are in different formated strings and return a list of Stringdates same format. I have tried the following code but some dates cannot parse. please help me to get the correct output.
public static void main(String[] args) {
List<String> dateList = new ArrayList<>();
dateList.add("2010/03/30");
dateList.add("15/12/2016");
dateList.add("11-15-2012");
dateList.add("20130720");
changeDates(dateList);
}
public static List<String> changeDates(List<String> dates){
List<String> returnedData = new ArrayList<>();
dates.forEach(parsedDate ->{
try {
Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(parsedDate);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String yyyyMMdd = sdf.format(date1);
System.out.println(yyyyMMdd);
} catch (ParseException e) {
e.printStackTrace();
}
});
return returnedData;
}
Output should be like this ["20100330","15122016","11152012","20130720"]
Thanks,