Java : how to add 10 mins in my Time

前端 未结 8 1900
闹比i
闹比i 2020-11-27 21:00

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

8条回答
  •  醉话见心
    2020-11-27 21:23

    I would use Joda Time, parse the time as a LocalTime, and then use

    time = time.plusMinutes(10);
    

    Short but complete program to demonstrate this:

    import org.joda.time.*;
    import org.joda.time.format.*;
    
    public class Test {
        public static void main(String[] args) {
            DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm");
            LocalTime time = formatter.parseLocalTime("14:10");
            time = time.plusMinutes(10);
            System.out.println(formatter.print(time));
        }       
    }
    

    Note that I would definitely use Joda Time instead of java.util.Date/Calendar if you possibly can - it's a much nicer API.

提交回复
热议问题