Q:

How to add seconds to date elements

I have a date array. I want to add 20 seconds to each of the elements in the array.
I tried
     for(int i=0i<20;i++)
     {
      date1[i]=date1[i].gettime()+20;
     }

This gives a long int value. But what i need is time format result.My question is it possible to add seconds using built in functions or manual function should be written for the same.

A:

That is because Date.getTime() returns number of milliseconds since January 1, 1970, 00:00:00 GMT. So you are resetting the value in your array with Long. To convert it back to Date you need to construct new Date object like this.
 for(int i = 0; i < 20; i++) {
  date1[i] = new Date(date1[i].gettime() + TimeUnit.SECONDS.toMillis(20));
 }

or set the time back like this:
 for(int i = 0; i < 20; i++) {
  date1[i].setTime(date1[i].gettime() + TimeUnit.SECONDS.toMillis(20));
 }

But I would strongly advice you to use Joda Time instead of Java Date API

