10

I want to get current time in my application.

Then i want to add 30 minutes to the current time.

What is the best practice to achieve this?

I am able to get start and stop time from my web service. eg: ((start time)11:00 am to (stop time) 11:00 pm) now, i would like to add 30 minutes to the current time till the stop time is reached.

4
  • Use Calender for refrences docs.oracle.com/javase/1.4.2/docs/api/java/util/Calendar.html Mar 20, 2012 at 10:04
  • thanks samir. i am getting start time and stop time from web service. EX:((start time)11:00 am to 11:00 pm(stop time)) after getting this time i want to get current time in my application and divide the time in to 30 minutes time slot till the stop time?? Mar 20, 2012 at 10:05
  • 1
    Or System.currentTimeMillis() + 1000*60*30; if time in milliseconds is required.
    – Jave
    Mar 20, 2012 at 10:06
  • ok samir let me try this Mar 20, 2012 at 10:11

3 Answers 3

30
Calendar now = Calendar.getInstance();
now.add(Calendar.MINUTE, 30);

And to output the time you could use

// 24 hours format
SimpleDateFormat df = new SimpleDateFormat("HH:mm");
// AM/PM format
SimpleDateFormat df = new SimpleDateFormat("hh:mm aa");

System.out.println(df.format(now.getTime()));
1
  • Just use SimpleDateFormat df = new SimpleDateFormat("HH:mm a"); Mar 20, 2012 at 10:46
14

Use the following:

//get current Time
long currentTime = System.currentTimeMillis();
//now add half an hour, 1 800 000 miliseconds = 30 minutes
long halfAnHourLater = currentTime + 1800000;
0
11
System.currentTimeMillis()+(30*60*1000);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.