Reputation: 115
How to add HOURS:MINUTES:SECONDS value on DateTime field?
On example I want in my field which contains dimetime value to add (sum) with string value which contains value in HH:MM:SECONDS.
ReportDate is 2013-01-01 10:00:00
Value to add is :120:34:29
So result should be: 2013-01-06 10:34:00
I tried with:
GregorianCalendar c = new GregorianCalendar();
c.setTime(reportDate);
String toAdd='120:34:29';
c.add(Calendar.HOUR, toAdd);
java.util.Date targetFinish = c.getTime();
SystemOutPrintln(targetFinish.toString());
So problematic row is c.add(Calendar.HOUR, toAdd);
How to add these value? I want to do it over Gregorian Calendar. And want to have in SysOut that new value;
Thank you
Upvotes: 1
Views: 297
Reputation: 41220
You can parse text into hours, minutes and seconds and add them like -
String[] time ="120:34:29".split(":");
c.add(Calendar.HOUR, Integer.parseInt(time[0]));
c.add(Calendar.MINUTE, Integer.parseInt(time[1]));
c.add(Calendar.SECOND, Integer.parseInt(time[2]));
Upvotes: 2
Reputation: 3855
GregorianCalendar c = new GregorianCalendar();
c.setTime(reportDate);
String[] toAdd = "120:34:29".split(":");
int hours = Integer.parseInt(toAdd[0]);
int mins = Integer.parseInt(toAdd[1]);
int secs = Integer.parseInt(toAdd[2]);
c.add(Calendar.SECOND, secs);
c.add(Calendar.MINUTE, mins);
c.add(Calendar.HOUR, hours);
java.util.Date targetFinish = c.getTime();
SystemOutPrintln(targetFinish.toString());
Upvotes: 2