junaidp junaidp - 12 days ago 5
Java Question

Java : how to add 10 mins in my Time

I am getting this time

String myTime = "14:10";


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


Is this possible, and if so, how?

Thanks

m0s m0s
Answer

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.