on 02-26-2007 5:58 PM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
User | Count |
---|---|
93 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.