-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
  • 34,719
  • 24
  • 185
  • 251
Muhammad Maqsoodur Rehman
  • 32,743
  • 34
  • 81
  • 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
  • 131
  • 1
  • 8
1

use:

SimpleDateFormat sdf=new SimpleDateFormat("hh:mm:ss");
String time=sdf.format(new Date());
vipul mittal
  • 17,053
  • 3
  • 39
  • 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,191
  • 7
  • 40
  • 73
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,607
  • 2
  • 15
  • 14