To get the current time in Android, you can use the Calendar
class and the System.currentTimeMillis()
method. Here's an example of how you could use these to get the current time and display it in the format "HH:mm:ss":
import java.util.Calendar; Calendar calendar = Calendar.getInstance(); long millis = System.currentTimeMillis(); calendar.setTimeInMillis(millis); String time = String.format("%02d:%02d:%02d", calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND)); System.out.println("Current time: " + time);
This code will output the current time in the format "HH:mm:ss", for example "16:34:05" for 4:34:05 PM.
Alternatively, you can use the Date
and SimpleDateFormat
classes to get the current time and format it in a specific way. Here's an example of how you could do this:
import java.text.SimpleDateFormat; import java.util.Date; SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); String time = sdf.format(new Date()); System.out.println("Current time: " + time);
This code will also output the current time in the format "HH:mm:ss".
Note that the time returned by these examples is based on the device's system clock, which may not be accurate if the device's clock is not set correctly.