Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add 10 minutes to my (String) time?

Tags:

java

I have this time:

String myTime = "14:10"; 

Now I want to add 10 minutes to this time, so that it would be 14:20

How can I achieve this?

like image 916
junaidp Avatar asked Jan 26 '12 08:01

junaidp


People also ask

How to increase time by minutes in java?

setTime(d); cal. add(Calendar. MINUTE, 10); String newTime = df.

How to add time to time java?

We can use the cal. add(unit, amount) method for adding and subtracting time. If the amount was positive number then specified amount of specified unit of time is added to the calendar.

How do you add minutes to a date?

To add minutes to a date:Use the getMinutes() method to get the minutes of the specific date. Use the setMinutes() method to set the minutes for the date. The setMinutes method takes the minutes as a parameter and sets the value for the date.


1 Answers

Something like this

 String myTime = "14:10";  SimpleDateFormat df = new SimpleDateFormat("HH:mm");  Date d = df.parse(myTime);   Calendar cal = Calendar.getInstance();  cal.setTime(d);  cal.add(Calendar.MINUTE, 10);  String newTime = df.format(cal.getTime()); 

As a fair warning there might be some problems if daylight savings time is involved in this 10 minute period.

like image 101
m0s Avatar answered Oct 28 '22 08:10

m0s