0
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class TimeAddition {
    public static void main(String args[]) throws ParseException {
        String i = "0:10";
        String a = "0:15";

// Converting String to Date format

        DateFormat sdf = new SimpleDateFormat("hh:mm");
        Date iTime = sdf.parse(i);
        Date aTime = sdf.parse(a);

// Adding two date time only by getting time

        long totalTime = iTime.getTime() + aTime.getTime();

        System.out.println("Total time in millisecond = " + totalTime);

        long millis = totalTime;

// Converting result in milliseconds to "hh:mm:ss"

        String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
            TimeUnit.MILLISECONDS.toMinutes(millis)
                    - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
            TimeUnit.MILLISECONDS.toSeconds(millis)
                    - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));

    System.out.println("Time format " + hms);
}

}

// output expected 00:25:00

Shamith S Kumar
  • 97
  • 1
  • 1
  • 7
  • Finding difference using same method is possible, but why addition is not working? – Shamith S Kumar Dec 26 '16 at 10:15
  • 1
    You can add time duration to timestamp and get new timestamp, you can add time duration to another time duration and get the sum of durations, but you can't sum two timestamps, it's logically wrong. So, my advise: thing again about what are trying to sum. – Alexander.Furer Dec 26 '16 at 10:26
  • Actually I found the answer by using DateFormat sdf = new SimpleDateFormat("hh:mm"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); – Shamith S Kumar Dec 28 '16 at 06:24

4 Answers4

1

It seems that you want to add durations and there is a class in Java 8+ that represents that:

Duration d1 = Duration.ofMinutes(10);
Duration d2 = Duration.ofMinutes(15);
Duration d = d1.plus(d2); //25 minutes

You would then need to format it for output. There are several questions on SO around this, for example:

Note that if the Duration is less than a day, you can use:

LocalTime.MIDNIGHT.plus(d).format(DateTimeFormatter.ofPattern("HH:mm:ss"))
Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
0
long totalTime = iTime.getTime() + aTime.getTime() - (2 * sdf.parse("0:0").getTime());

should solve the problem.

Date.getTime Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

user3161880
  • 1,037
  • 6
  • 13
0

When you call getTime() on a Date object it return number of milliseconds since January 1, 1970, 00:00:00 GMT.

So when you call getTime() two times in below statement

long totalTime = iTime.getTime() + aTime.getTime();

You have added milliseconds since January 1, 1970, 00:00:00 GMT two times. So you have to reduce it once to get the desired value. Please see below code snippet:

... //your code
String b = "0:00";

// Converting String to Date format

DateFormat sdf = new SimpleDateFormat("hh:mm");
... //your code
Date bTime = sdf.parse(b);
// Adding two date time only by getting time

long totalTime = iTime.getTime() + aTime.getTime() -bTime.getTime(); //subtracting extra value
System.out.println("Total time in millisecond = " + totalTime);

long millis = totalTime;
// Converting result in milliseconds to "hh:mm:ss"

sdf = new SimpleDateFormat("HH:mm:ss");
String hms = sdf.format(new Date(totalTime));

System.out.println("Time format " + hms);

I have taken another approach for converting your totalTime to desired format.

SachinSarawgi
  • 2,632
  • 20
  • 28
  • Now what if you do this on the day of a daylight savings time switchover? What exactly is the meaning of adding two times together anyway? – Erwin Bolwidt Dec 26 '16 at 10:48
  • @Erwin I am not able to understand `on the day of a daylight savings time switchover?`. Could you please explain little bit more. I only tried to solve the problem, I dont know deep concepts. – SachinSarawgi Dec 26 '16 at 10:52
0

Using "sdf.setTimeZone(TimeZone.getTimeZone("UTC"));" will help to get the total time

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

class TimeAddition {
public static void main(String args[]) throws ParseException {
    String b = "0:10";
    String a = "0:15";

// Converting String to datetime

    DateFormat sdf = new SimpleDateFormat("hh:mm");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date bTime = sdf.parse(b);
      Date aTime = sdf.parse(a);

// Adding two time only by getting time

    long totalTime = bTime.getTime() + aTime.getTime();

     System.out.println("Total time in millisecond = " + totalTime);

     long millis = totalTime;

// Converting result in milliseconds to "hh:mm:ss"

    String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
            TimeUnit.MILLISECONDS.toMinutes(millis)
                    - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
            TimeUnit.MILLISECONDS.toSeconds(millis)
                    - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));

    System.out.println("Time format " + hms);   
 }

}

Output produced as 00:25:00

Shamith S Kumar
  • 97
  • 1
  • 1
  • 7