I am just starting with Java (Android) and got stuck on a Date formatting issue.
I have a small Form where you can enter a project name and choose a Start date on a Calendar. The Startdate and Projectname gets than entered into the database, after that the pre-defined Tasks get than entered automatically into the database.
- Task 1 Due Date is Startdate,
- Task 2 is the Startdate plus x days = DueDate2,
- Task 3 is The DueDate2 plus x days = DueDate3
I have now come up with the below Sourcecode and everything works besides that I get the wrong Format of my Date. For some reason, my Format is correct in newDateStr, but when I parse it again to be a Date Object, the format changes and is than incorrect. I can’t see my mistake, anyone can help?
My understanding is:
- Set the Date format of the date entered (curFormat)
- Set the target Date format (postFormater)
- Parse your Date which is a String at this time, to turn it into a date Object (use curFormat)
- Format this date to get target date format (use postFormater), now its a String again
- Parse this again to get it back to be a date which is needed for the calendar
- Use calendar instance, setTime(here the formated date) and add the x days
- Format the Date to get target date format (use postFormater), now its a String again
-
Because I need a Date Object again, I have to parse it again.
// The format of your input date string SimpleDateFormat curFormater = new SimpleDateFormat("MM/dd/yyyy"); // The format of your target date string SimpleDateFormat postFormater = new SimpleDateFormat("dd-MM-yyyy"); // The calendar instance which adds a locale to the date Calendar cal = Calendar.getInstance(); // Parse the string (pro.getStart()) to return a Date object Date dateObj = curFormater.parse(pro.getStart()); // Format the Date dd-MM-yyyy String newDateStr = postFormater.format(dateObj); // Parse the string to return a Date object Date Startdate = postFormater.parse(newDateStr); while (cur.isAfterLast() == false) { Integer delayTime = cur.getInt(cur.getColumnIndex("DelayTime")); if (flag == false) { dateInString = Startdate; flag = true; }else{ cal.setTime(dateInString); // add the extra days cal.add(Calendar.DAY_OF_MONTH, delayTime); // Format the Date dd-MM-yyyy newDateStr = postFormater.format(cal.getTime()); // Parse the string to return a Date object dateInString = postFormater.parse(newDateStr); Log.i("newDateStr Format",newDateStr.toString()); // 29-11-2012 Log.i("dateInString parse",dateInString.toString()); // Thu Nov 29 00:00:00 GMT 2012
I hope someone sees my mistake. Thank you very much in advance !
A
java.util.Dateobject does not have any format in it or memory of the format you used to parse it. The output of itstoStringmethod, and hence what you get as output fromdateInString.toString()will always be in the default JDK format that you are seeing:Thu Nov 29 00:00:00 GMT 2012You have to use a formatter to convert it into a formatted string whenever you want to display it. You cannot “Format the Date object” so to say. (UI frameworks tend to have built in facilities for doing this automatically.)