tags:

views:

92

answers:

4

How to get the current time in Android?

When i use

int hours = java.sql.Time.this.getHours();

i get the error:

No enclosing instance of the type Time is accessible in scope
A: 
int hours = new Time(System.currentTimeMillis()).hour;
Thorstenvv
THANKS PERFEKT!!!
Hans Wurst
A: 

The an instance of the Calendar Class is set to the current date and time.

tobsen
I don't get the right time
Hans Wurst
+1  A: 

If you just want the current timestamp, you can use:

long millis = System.currentTimeMillis()

You can also get other time related values such as the uptime or total elapsed time since the last boot (including sleep time) from android.os.SystemClock.

Erich Douglass
A: 

int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);

public static final int HOUR_OF_DAY Since: API Level 1 Field number for get and set indicating the hour of the day. HOUR_OF_DAY is used for the 24-hour clock. E.g., at 10:04:15.250 PM the HOUR_OF_DAY is 22.