-3

I am using this Java method here to get the current time:

final Date d = new Date();
d.getTime();

1390283202624

What I am getting a numeric figure of datatype long. What I need is the exact time in the format hh:mm:ss. And in the end I also have to perform arithmetic on the figure obtained.

Any clue? Also is this a reliable way of obtaining time on Android phone because I am getting a constant value here?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Muhammad Maqsoodur Rehman
  • 33,681
  • 34
  • 84
  • 124

4 Answers4

4
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
String formattedDate = sdf.format(d);
System.out.println(formattedDate);
talhaocakci
  • 141
  • 1
  • 9
1

use:

SimpleDateFormat sdf=new SimpleDateFormat("hh:mm:ss");
String time=sdf.format(new Date());
vipul mittal
  • 17,343
  • 3
  • 41
  • 44
1

Use Calendar class.

Calendar c = Calendar.getInstance(); 
int seconds = c.get(Calendar.SECOND);

See this question. Calendar class contain all desired information.

Community
  • 1
  • 1
Faizan Mubasher
  • 4,427
  • 11
  • 45
  • 81
1
Calendar instance = Calendar.getInstance();
    int hour = instance.get(Calendar.HOUR);
    int minute = instance.get(Calendar.MINUTE);
    int second = instance.get(Calendar.SECOND);
user543
  • 3,623
  • 2
  • 16
  • 14