50

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?

Ivar
  • 6,138
  • 12
  • 49
  • 61
junaidp
  • 10,801
  • 29
  • 89
  • 137
  • You will find this useful http://stackoverflow.com/questions/759036/how-to-convert-string-into-time-format-and-add-two-hours – Jayy Jan 26 '12 at 08:38
  • Dont Forget the quotes if you want this to be a string. – Philippe Jan 26 '12 at 08:39
  • possible duplicate of [How do I say 5 seconds from now in Java?](http://stackoverflow.com/questions/1655357/how-do-i-say-5-seconds-from-now-in-java) – Basil Bourque Feb 01 '15 at 09:34

8 Answers8

101

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.

m0s
  • 4,250
  • 9
  • 41
  • 64
24

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Since Java 8 there's `java.time` with a very similar API to Joda Time (heavily inspired by it) and that should be used instead. – Joachim Sauer Jan 04 '21 at 11:40
13

Use Calendar.add(int field,int amount) method.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
7

Java 7 Time API

    DateTimeFormatter df = DateTimeFormatter.ofPattern("HH:mm");

    LocalTime lt = LocalTime.parse("14:10");
    System.out.println(df.format(lt.plusMinutes(10)));
Grigory Kislin
  • 16,647
  • 10
  • 125
  • 197
1

I used the code below to add a certain time interval to the current time.

    int interval = 30;  
    SimpleDateFormat df = new SimpleDateFormat("HH:mm");
    Calendar time = Calendar.getInstance();

    Log.i("Time ", String.valueOf(df.format(time.getTime())));

    time.add(Calendar.MINUTE, interval);

    Log.i("New Time ", String.valueOf(df.format(time.getTime())));
Hamza Polat
  • 355
  • 3
  • 7
1

You need to have it converted to a Date, where you can then add a number of seconds, and convert it back to a string.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
0

I would recommend storing the time as integers and regulate it through the division and modulo operators, once that is done convert the integers into the string format you require.

oers
  • 18,436
  • 13
  • 66
  • 75
rtlayzell
  • 608
  • 4
  • 20
0

You have a plenty of easy approaches within above answers. This is just another idea. You can convert it to millisecond and add the TimeZoneOffset and add / deduct the mins/hours/days etc by milliseconds.

String myTime = "14:10";
int minsToAdd = 10;
Date date = new Date();
date.setTime((((Integer.parseInt(myTime.split(":")[0]))*60 + (Integer.parseInt(myTime.split(":")[1])))+ date1.getTimezoneOffset())*60000);
System.out.println(date.getHours() + ":"+date.getMinutes());
date.setTime(date.getTime()+ minsToAdd *60000);
System.out.println(date.getHours() + ":"+date.getMinutes());

Output :

14:10
14:20
ironwood
  • 8,936
  • 15
  • 65
  • 114
  • One thing I learned about doing time math the hard way is the damn leap seconds... you should never ever do time calculations based on commonly known time (ie 1 minute = 60 seconds) constants when dealing with time in java. – m0s Jan 26 '12 at 20:17
  • 1
    Thanx m0s for your comment. If you can explain the reason for "never ever do time calculations based on commonly known time (ie 1 minute = 60 seconds) constants when dealing with time in java" it would be really helpful for all of us including me. – ironwood Jan 27 '12 at 04:25
  • please refer to http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Date.html and http://lpar.ath0.com/2008/09/16/the-fractious-leap-second-debate/ and http://tripoverit.blogspot.com/2007/07/java-calculate-difference-between-two.html – m0s Jan 27 '12 at 14:29