12

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
Hans Wurst
  • 141
  • 1
  • 2
  • 8
  • See this post http://stackoverflow.com/questions/2271131/display-current-time-and-date-in-android-application – Ally Aug 31 '10 at 16:35

7 Answers7

16
int hours = new Time(System.currentTimeMillis()).getHours();
Thorstenvv
  • 5,376
  • 1
  • 34
  • 28
13

Try this:

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.

Alex K
  • 8,269
  • 9
  • 39
  • 57
user223697
  • 193
  • 7
5

My favorite sample:

Time dtNow = new Time();
dtNow.setToNow();
int hours = dtNow.hour;
String lsNow = dtNow.format("%Y.%m.%d %H:%M");
String lsYMD = dtNow.toString();    // YYYYMMDDTHHMMSS
animuson
  • 53,861
  • 28
  • 137
  • 147
JohnnyLinTW
  • 166
  • 2
  • 4
4

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
  • 51,744
  • 11
  • 75
  • 60
2

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

tobsen
  • 5,328
  • 3
  • 34
  • 51
1

Calendar cal = Calendar.getInstance(); // get current time in a Calendar

then you can do lots with the Calendar instance, such as get the Hours or the Minutes - like:

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

This is recommended when you have to localize to many locales, and print data in multiple formats, or do operations on dates.

Andrew Mackenzie
  • 5,477
  • 5
  • 48
  • 70
  • This is currently the best answer as getHours() / getDate() etc have been depreciated. – Ravi Jun 04 '16 at 06:50
1

Just adding a little to Andrew's reply. The later part of the code increments the hour if your time zone is in daylight savings mode. The HOUR_OF_DAY is in 24 hour format.

    Calendar currentTime    = Calendar.getInstance()                ;
    int hour                = currentTime.get(Calendar.HOUR_OF_DAY) ; 
    int minute              = currentTime.get(Calendar.MINUTE)      ;
    int second              = currentTime.get(Calendar.SECOND)      ;
    long milliDiff          = currentTime.get(Calendar.ZONE_OFFSET) ;
    // Got local offset, now loop through available timezone id(s).
    String [] ids           = TimeZone.getAvailableIDs()            ;
    for (String id : ids) 
            {
       TimeZone tz = TimeZone.getTimeZone(id)                   ;
       if (tz.getRawOffset() == milliDiff)
          {  // Found a match, now check for daylight saving
          boolean inDs    = tz.inDaylightTime(new Date())   ;
          if (inDs)       { hour += 1 ; }
          if (hour == 25) { hour  = 1 ; }
          break                                             ;
          }
        }
Brian Fleming
  • 63
  • 1
  • 6