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?
setTime(d); cal. add(Calendar. MINUTE, 10); String newTime = df.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With