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 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 int hours = new Time(System.currentTimeMillis()).getHours(); 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 Calendar.getInstance(). ThanksCalendar 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.
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 ; } } java.timeIn 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*.
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 Learn more about the modern Date-Time API from Trail: Date Time.