Add 1 hour to current time in android

42,671

Solution 1

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());

Solution 2

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

   Date a=new Date();
   a.setTime(System.currentTimeMillis()+(60*60*1000));

Solution 3

Calendar now = Calendar.getInstance();
now.add(Calendar.HOUR,1);
System.out.println(now.getTime());
Share:
42,671
Brinda K
Author by

Brinda K

SOreadytohelp

Updated on April 15, 2020

Comments

  • Brinda K
    Brinda K about 4 years

    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.