cancel
Showing results for 
Search instead for 
Did you mean: 

Code for adding an integer to time

prabhu_s2
Active Contributor
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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);

Dennis

prabhu_s2
Active Contributor
0 Kudos

hi dennis

thks...lemme try

prabhu_s2
Active Contributor
0 Kudos

is this part not completed?

<i>Date currentDate = ...</i>

prabhu_s2
Active Contributor
0 Kudos

my current date will be in HH:mm:ss format and integer is 5 or 10. i nned to add this integer to the time which is in string format

Former Member
0 Kudos

Convert the string into a Date instance first.

SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");

Date currentDate = format.parse("You time string");

Former Member
0 Kudos

By the way when you say "I want to add the integer into the time", my understanding is that you want to add the integer to the second portion of a time.

For insance, original time is 12:30:20, if your integer is 5 or 10, then the final result would be 12:30:25 or 12:30:30. Is that correct?

prabhu_s2
Active Contributor
0 Kudos

say if i wanna to add 10 to 12:20:20 then it wud be 12:20:30 but if i add 40 then it wud be 12:21:00...this is the case. can u provide me the code for it

prabhu_s2
Active Contributor
0 Kudos

wud be gr8 if u can post the entire set of code

Former Member
0 Kudos
GregorianCalendar gc = new GregorianCalendar(2007,2,27,12,20,30 );
System.out.println("1." + gc.getTime());
gc.add(GregorianCalendar.SECOND,40);
System.out.println("2." +gc.getTime());

this gives me the following output on my PC...

1.Tue Mar 27 12:20:30 IST 2007
2.Tue Mar 27 12:21:10 IST 2007

prabhu_s2
Active Contributor
0 Kudos

thks ammal

but my input string STRG had an input 12:20:10 and another string inte has 10...i want to add this so that my output is 12:20:30...can u provide me the code for it

Former Member
0 Kudos
		String STRG = "12:20:10";
		String INTE = "10";
		StringTokenizer sTok = new StringTokenizer(STRG, ":");

		int hr = Integer.parseInt(sTok.nextToken());
		int min = Integer.parseInt(sTok.nextToken());
		int sec = Integer.parseInt(sTok.nextToken());

		GregorianCalendar today = new GregorianCalendar();

		GregorianCalendar gc =
			new GregorianCalendar(
				today.get(GregorianCalendar.YEAR),
				today.get(GregorianCalendar.MONTH),
				today.get(GregorianCalendar.DAY_OF_MONTH),
				hr,
				min,
				sec);
		System.out.println(gc.getTime());
		gc.add(GregorianCalendar.SECOND, Integer.parseInt(INTE));
		System.out.println(gc.getTime());

btw....12:20:10 +10 would be 12:20:20 and not 12:20:30

prabhu_s2
Active Contributor
0 Kudos

amol

one quick q...what will be the output for

String STRG = "12:20:59";

String INTE = "10";

can u pls check and post me...sorry i dont have access to nwds and xi now. unable to check it

Former Member
0 Kudos

must give you 12:21:09

Sigiswald
Contributor
0 Kudos

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.setTime(t);
    c.add(Calendar.SECOND, Integer.parseInt(seconds));
    t.setTime(c.getTimeInMillis());

    return t.toString();
  }

Then


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

Kind regards,

Sigiswald

Answers (0)