Skip to Content

Code for adding an integer to time

hi all

i have req in xi where i need to add '1' to current time. say if the vairable tme holds the current time and interger 1 is in constant integ what will be the piece of code to acieve this...i will be using it in a user def f/n.

ur expertise is requested

Add a comment
10|10000 characters needed characters exceeded

Related questions

1 Answer

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Feb 26, 2007 at 07:08 PM

    Wish the following piece code would help you.

    for instance your constant is ONE_SECOND = 1 and the currentDate is the time variable.

    Date currentDate = ...

    long currentTime = currentDate.getTime();

    long newTime = currentTime + ONE_SECOND*1000;

    Date newDate = new Date( newTime);


    Add a comment
    10|10000 characters needed characters exceeded

    • What about not using custom string parsing

      import java.sql.Time;
      import java.util.Calendar;
      import java.util.Date;
        // time in format "hh:mm:ss"
        private static String addSeconds(String time, String seconds) {
          Time t = Time.valueOf(time);
          Calendar c = Calendar.getInstance();
          c.add(Calendar.SECOND, Integer.parseInt(seconds));
          return t.toString();


          System.out.println(addSeconds("12:20:10", "10")); // 12:20:20
          System.out.println(addSeconds("23:59:55", "10")); // 00:00:05

      Kind regards,


Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.