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
  • 10,631
  • 12
  • 36
  • 56
Brinda K
  • 745
  • 3
  • 16
  • 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

62

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
  • 132,198
  • 53
  • 198
  • 213
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,140
  • 3
  • 21
  • 31
11
Calendar now = Calendar.getInstance();
now.add(Calendar.HOUR,1);
System.out.println(now.getTime());
APriya
  • 1,994
  • 1
  • 16
  • 21