0

I have this code for my splash screen

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            startActivity(new Intent(Splash.this, Home.class));
            Log.i(TAG, "Exiting Splash Screen");
            finish();
        }
    }, TIMEOUT_TIME);

is it possible to get what is the current time at TIMEOUT_TIME from inside the run(){}?

Vishnu T B
  • 118
  • 3
  • 15

3 Answers3

1

AFAIK, Handler doesn't provide any convenient method to do this. You have to save the initial time and calcualte elapsed time. Like this.

Community
  • 1
  • 1
Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109
0

Using this function you will get current time at timeout.

new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
                String currentDateandTime = sdf.format(new Date());
                Log.e("Demo","==================================="+currentDateandTime);
            }
        }, 3000);
Darshan Mistry
  • 3,294
  • 1
  • 19
  • 29
  • not current time, the time in between the TIMEOUT_TIME and the time of starting the Handler – Vishnu T B Jul 07 '15 at 07:15
  • Date currentDate = new Date(System.currentTimeMillis()); using this line you have to get current time.you add this line in onCreate() method and also in run() method of handler.To get time difference long diffInMs = endDate.getTime() - startDate.getTime(); long diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMs) – Darshan Mistry Jul 07 '15 at 08:39
0

To be more precise, I would use nanoTime() method rather than currentTimeMillis():

long startTime = System.nanoTime();
myCall(); 
long stopTime = System.nanoTime();
System.out.println(stopTime - startTime);
Shoeb Siddique
  • 2,805
  • 1
  • 22
  • 42