13

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 
1

8 Answers 8

16
int hours = new Time(System.currentTimeMillis()).getHours(); 
Sign up to request clarification or add additional context in comments.

5 Comments

System.currentTimeMillis() return long and Time don't have a long argument! sorry but this code is wrong.
this code is correct, since the question is about java.sql.Time which has an appropriate constructor.
On Android java.sql.time has no public hours member, and you must use getHours().
As I said, it does not work. After alt-Enter a pop up menu appears with two choices: "create class", "create inner class"
getHours() method of Time class is deprecated since API 16.
12

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.

Comments

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 

1 Comment

I think this is faster than Calendar.getInstance(). Thanks
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.

Comments

2

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

Comments

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.

1 Comment

This is currently the best answer as getHours() / getDate() etc have been depreciated.
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 ; } } 

Comments

1

java.time

In March 2014, Java 8 introduced the modern, java.time date-time API which supplanted the error-prone legacy java.util date-time API. Any new code should use the java.time API*.

Solution using modern date-time API

You can get the current time using LocalTime#now and then get the values of individual time units from it. You can also format it in various ways. If you want to get the epoch seconds and epoch milliseconds, you can get them from Instant#now.

Demo:

public class Main { public static void main(String[] args) { // ZoneId.systemDefault() returns the system default time zone. // Replace it as per your requirement e.g. ZoneId.of("Europe/London"). LocalTime time = LocalTime.now(ZoneId.systemDefault()); System.out.println(time); // Formatted string value of time System.out.println(time.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM))); System.out.println(time.format(DateTimeFormatter.ofPattern("HH:mm:ss"))); // If you want to get values of time units e.g. hour from this object System.out.println(time.getHour()); // If you want to get the epoch milliseconds and epoch seconds Instant instant = Instant.now(); System.out.println(instant.toEpochMilli()); System.out.println(instant.getEpochSecond()); } } 

Output from a sample run when executed on a system at UTC:

17:16:11.916501 5:16:11 PM 17:16:11 17 1729790171993 1729790171 

Online Demo

Learn more about the modern Date-Time API from Trail: Date Time.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.