title: Impossible to calculate a difference of time in Android using System.currentTimeMillis()
body: <p>I'm developing a little Android app to calculate the time it takes to hit a square with another square using the accelerometer of the phone.</p>
  <p>I have a custom View defined as follows : (I removed several declarations for readability purpose)</p>
  <pre><code>public class BallView extends View implements SensorEventListener {
  
      private long startTime = 0;
      private long endTime = 0;
      private long time = 0;
  
      public BallView( Context context) {
          super( context);
          init( context );
      }
  
      private void init(Context context) {
          size = 200;
          orignSize = size;
          rectBall.set(currentX,currentY,currentX+size,currentY+size);
          rectviseur.set(getWidth()/2-10,getHeight()/2-10,getWidth()/2-5,getHeight()/2-5);
          vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
          startT();
      }
  
      @Override
      protected void onSizeChanged(int w, int h, int oldw, int oldh) {
          super.onSizeChanged(w, h, oldw, oldh);
  
          currentX = 0;
          currentY = 0;
      }
  
      @Override
      protected void onDraw(Canvas canvas) {
  
          rectviseur.set(getWidth()/2-4,getHeight()/2-4,getWidth()/2+4,getHeight()/2+4);
          super.onDraw(canvas);
          paintViseur.setColor(Color.GREEN);
          paint.setColor(Color.RED);
          rectBall.set(currentX,currentY,currentX+size,currentY+size);
          canvas.drawRect(rectBall,paint);
          canvas.drawRect(rectviseur,paintViseur);
          paint.setTextSize(20);
  
          canvas.drawText(String.valueOf(time),300,300,paint);
      }
  
      private void moveImage( float x, float y ) {
          currentX += (int) x;
          currentY += (int) y;
  
          if ( currentX &lt; 0 ) {
              currentX = 0;
          } else if ( currentX + size &gt; getWidth() ){
              this.currentX = getWidth() - size;
          }
  
          if ( currentY &lt; 0 ) {
              currentY = 0;
          } else if ( currentY + size &gt; getHeight() ){
              currentY = getHeight() - size;
          }
  
          invalidate();
      }
  
      @Override
      public void onAccuracyChanged(Sensor arg0, int arg1) {}
  
      @Override
      public void onSensorChanged(SensorEvent event) {
          if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER ) {
              x = event.values[0];
              y = event.values[1];
              z = event.values[2];
          }
          if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE ) {
              xg = event.values[0];
              yg = event.values[1];
              zg = event.values[2];
          }
  
          this.moveImage( -x*5, -(z-(float)5.5)*5 );
          hit();
      }
  
      public void hit(){
          if (rectBall.contains(rectviseur)) {
              //vibrate
              if (Build.VERSION.SDK_INT &gt;= 26) {
                  vibrator.vibrate(VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE));
              } else {
                  vibrator.vibrate(200);
              }
  
  
              //replace the rectangle
              size = size-20;
              currentX = random.nextInt(getWidth()-size);
              currentY = random.nextInt(getHeight()-size);
              if (size &lt; 8) {
                  size= orignSize;
              }
  
              stopT();
              time = time(startTime, endTime);
  
              startT();
          }
      }
  
      private void startT() {
          startTime = System.currentTimeMillis();
      }
  
      private void stopT() {
          endTime = System.currentTimeMillis();
      }
  
      private long time(long start, long end) {
         return (end - start);
      }
  }
  </code></pre>
  <p>I have 3 variables <code>startTime</code> to store the current time when the game is launched (or restarted), <code>endTime</code> to store the current time when the square was hit, and <code>time</code> to calculate the time it took.</p>
  <p>I affect <code>startTime</code> and <code>endTime</code> by calling respectively <code>startT()</code> and <code>stopT()</code>. The first affectation of <code>startTime</code> is in the <code>init</code> method. And I display the result with <code>canvas.drawText(String.valueOf(time),300,300,paint);</code></p>
  <p>As you can see when the the square hit the other square the method <code>hit()</code> is called. At the end of this method, I call <code>stopT()</code> to store the time when the square was hit and I call the <code>time</code> method to calculate the time it took. Then I recall <code>startT()</code> because the game restart.</p>
  <p>So technically the <code>startTime</code> is supposed to contains something like &quot;1607303475300&quot; and <code>endTime</code> something like &quot;1607303477350&quot;. But I get completely incoherent result in <code>time</code> everytime. Like 5, 7 or 8 while the time is in millisecond which is impossible. I really can't understand why. I tried several solution, none of them worked so I'm seeking for your help.</p>
  <p><strong>Edit :</strong><br />
  Also here's my MainActivity class that uses my BallView:</p>
  <pre><code>public class MainActivity extends Activity {
  
      private BallView ballView;
      private SensorManager sensorManager = null;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          ballView = new BallView(this);
          ballView.setBackgroundColor(Color.BLACK);
          setContentView(ballView);
  
          sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          sensorManager.registerListener(
                  ballView,
                  sensorManager.getDefaultSensor( Sensor.TYPE_ACCELEROMETER ),
                  sensorManager.SENSOR_DELAY_GAME );
          sensorManager.registerListener(
                  ballView,
                  sensorManager.getDefaultSensor( Sensor.TYPE_GYROSCOPE ),
                  sensorManager.SENSOR_DELAY_GAME );
      }
  
      @Override
      protected void onPause() {
          super.onPause();
          sensorManager.unregisterListener( ballView );
      }
  }
  </code></pre>