How to convert String format dates to Date format dates?
- by Jani Bela
I have a string with dates it looks like: "20120316 20120317 20120318" ... I store this dates in this format, but I would like to make a Date array from these numbers with the format 03/16 03/17 03/18 ...
So far:
String[] DailyDatasOnce2 = DatesOnce.split(" ");
DailyDatasOnce = new String[DailyDatasOnce2.length];
for (int i=0;i< (DailyDatasOnce2.length) ;i++){
DailyDatasOnce[i]=DailyDatasOnce2[i];
}
datumok = new Date[DailyDatasOnce.length];
for (int i=0;i< (DailyDatasOnce.length) ;i++){
SimpleDateFormat curFormater = new SimpleDateFormat("yyyyMMdd");
java.util.Date dateObj = null;
java.util.Date dateObj2 = null;
try {
dateObj = curFormater.parse(DailyDatasOnce[i]);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SimpleDateFormat postFormater = new SimpleDateFormat("MM/dd");
String newDateStr = postFormater.format(dateObj);
try {
dateObj2 = curFormater.parse(newDateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
datumok[i] = dateObj2;
}
So first I make a string array with the string dates (DailyDatasOnce), maybe that first for loop is useless but i can skip it. Now I make a Date array and I want to put the dates into it. I format the dates to format I want, then I try to convert them to Date format. Until the String newDateStr it is working, I manage to change the type of the date.
But I get syntax error: Type mismatch: Cannot convert from java.util.date to java.sql.data.
I suspect the problem but if it is not possible, how can i do this?