I have a textbox which accepts Minutes.
This value will be stored in a variable – textBoxVal
I also have dates saved in a String Arraylist- workDate where each element is in format yyyy-mm-dd HH:mm:ss
The requirement is to add the textBoxVal value to each of the elements in the string arraylist and save the new value in a date array – newWorkDate
For eg:
textBoxVal = 60
workDate[0] = "2012-02-12 09:00:00.0"
So, newWorkDate[0] should be 2012-02-12 10:00:00.0
This is what I have so far-
int textBoxVal = 0;
ArrayList<String> workDate = new ArrayList<String>();
Date[] newParsedWorkDate = new Date[10];
Date[] newWorkDate = new Date[10];
textBoxVal = getTextBoxValue();
for (int i=0; i< cntr; i++){
workDate[i] = getWorkDate(i);
newParsedWorkDate[i] = DateHelper.parseDate (workDate[i], "yyyy-MM-DD HH:mm:ss"); //Converting string to datetime
newWorkDate[i] = DateHelper.addMinutes(newParsedWorkDate[i], textBoxVal);
}
The problem here is although workDate is “2012-02-12 09:00:00.0”, newWorkDate is “2012-01-12 10:00:00.0” i.e. it adds the textBoxVal but sets the date to the previous day.
Please help me understand where my code is wrong.Thx!
SOLUTION:
Used "yyyy-MM-dd HH:mm:ss" instead of
"yyyy-MM-DD HH:mm:ss"
Use
MMto represent months, andddto represent the day in month.So, you need to change your format to
yyyy-MM-dd HH:mm:ss.See the javadocs to understand what the letters in the pattern mean.