In java im trying to take one time scale that has a kind of custom format and then im trying to add it to another time scale and output it in the same format. My date is formatted like 11d 23h 13m 12s. I then want to take this string, add it to something like 1d 0h 0m 3s. This would then output 12d 23h 13m 15s. Thanks for any help.
-
possible duplicate of [Sum two dates in Java](http://stackoverflow.com/questions/2067657/sum-two-dates-in-java) – NESPowerGlove Feb 17 '15 at 20:47
-
4@user3208218 I'm curious why `11d 24h 13m 12s` + `12d 24h 13m 12s` = `13d 24h 13m 12s` ? – iRuth Feb 17 '15 at 20:50
-
Yea sorry iv confused my self while writing it. ill change it now – user3208218 Feb 17 '15 at 21:00
-
Another problem is that you seem to expect the `24h` not to change to an additional day in the output. Are you sure of that? – RealSkeptic Feb 17 '15 at 21:03
-
Just to make the wording clear, you do not add two strings, you parse two strings into time periods, add those periods and print them out again as strings. – eckes Feb 18 '15 at 01:18
3 Answers
In general, if you're trying to manipulate time, you're much better off using dedicated classes for that purpose - in Java 8, you'd want to manually parse your two strings into java.time.Duration
s, use Duration.plus(Duration)
to add them, and then use Duration's various to*
methods to build your result string.
If you've got some control over your input and output formats, you could change them to match Duration's formats and save some effort there.

- 11,279
- 6
- 41
- 42
-
Which is why I said to build the result string using e.g. `Duration.toHours`, `Duration.toMinutes`, etc. – Sbodd Feb 17 '15 at 20:58
-
But adding one `Duration` whose day part is 11 to a `Duration` whose day part is `12` is never going to give you `13` in the resulting `Duration`'s day part, which is what the OP said he wants. Hence the comment that asks the OP for clarification of his request. – RealSkeptic Feb 17 '15 at 21:01
-
Ok so I'm pretty new to Java so this doesn't make a lot of sense to me. You think you could try dumb it down a little for me? Thanks – user3208218 Feb 17 '15 at 21:09
-
Based on the comments, it looks like OP made some trivial errors in the example given. I wouldn't get too hung up on those details. – David Conrad Feb 17 '15 at 21:20
I think you need a StringBuilder here:
Correct way to use StringBuilder
An example:
StringBuilder and SystemFormat(You can make new Strings).
int KeepingTheSeconds = 5;
StringBuilder timerBuilder = new StringBuilder(String.format("00:%02d",KeepingTheSeconds));

- 1
- 1

- 376
- 1
- 2
- 12
If you want to implement it from scratch you can use regular expressions and perform some calculations:
package stackoverflow;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
static Pattern p = Pattern.compile("(\\d+)d (\\d+)h (\\d+)m (\\d+)s");
private static void add(String s, int[] a) {
Matcher m = p.matcher(s);
m.find();
for (int i = 0; i < a.length; i++)
a[i] += Integer.parseInt(m.group(i + 1));
}
private static String calculate(String s1, String s2) {
int a[] = new int[4];
add(s1, a);
add(s2, a);
if (a[3] >= 60) {
a[3] -= 60;
a[2]++;
}
if (a[2] >= 60) {
a[2] -= 60;
a[1]++;
}
if (a[1] >= 24) {
a[1] -= 24;
a[0]++;
}
return String.format("%dd %dh %dm %ds", a[0], a[1], a[2], a[3]);
}
public static void main(String args[]) {
String s1 = "11d 24h 13m 12s";
String s2 = "1d 0h 0m 3s";
System.out.println(calculate(s1, s2));
}
}
This gives output:
13d 0h 13m 15s
The above implementation doesn't provide any checking for malformed input.

- 19,631
- 4
- 30
- 57
-
This seemed to work for me thanks. But just to be picky, im using this in a bukkit server plugin and when i use the command to return the value in a message, the result has a small symbol on the end. If you know bukkit any chance you know how to fix this. If not, then thats fine. Thank you anyway – user3208218 Feb 17 '15 at 21:32