Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add 30 minutes to the current time in java

Tags:

java

android

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.

like image 478
Pinakin Kansara Avatar asked Mar 20 '12 10:03

Pinakin Kansara


2 Answers

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()));
like image 178
Leo Avatar answered Oct 20 '22 10:10

Leo


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;
like image 42
Matthew Avatar answered Oct 20 '22 10:10

Matthew