16

Possible Duplicate:
How to increment time by 1 hour

I am using this code to get the current date and time

SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd:HH:mm");
String currentDateandTime = sdf.format(new Date());
System.out.println("current time date" + currentDateandTime);

Now I want to add 1 hour to this. I searched but I'm not getting the answer I want. Now I'm getting like this:

current time date2012:12:13:12:05

I want an answer like 2012:12:13:13:05 in 24hr format.

Samuel Philipp
  • 9,983
  • 12
  • 32
  • 52
Brinda K
  • 745
  • 2
  • 15
  • 34
  • 1
    see this [answer](http://stackoverflow.com/a/12366336/1289716) that will help you sure... – MAC Dec 13 '12 at 06:40

3 Answers3

61

try as:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd:HH:mm");
String currentDateandTime = sdf.format(new Date());

Date date = sdf.parse(currentDateandTime);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, 1);

System.out.println("Time here "+calendar.getTime());
ρяσѕρєя K
  • 130,641
  • 51
  • 193
  • 212
20

You can increment date like this before passing it to sdf.format(date)

   Date a=new Date();
   a.setTime(System.currentTimeMillis()+(60*60*1000));
Rahul Verma
  • 2,110
  • 3
  • 20
  • 30
11
Calendar now = Calendar.getInstance();
now.add(Calendar.HOUR,1);
System.out.println(now.getTime());
APriya
  • 1,894
  • 1
  • 16
  • 21