How can I get the current time and date in an Android app?
- 843 answers! While many of them were good when they were written, the good answer to use in 2018 is here.Anonymous– Anonymous2018-09-26 12:11:09 +00:00Commented Sep 26, 2018 at 12:11
- 1@OleV.V. How about in 2023?CPlus– CPlus2023-03-27 23:23:04 +00:00Commented Mar 27, 2023 at 23:23
- 3@user16217248, I still recommend the answer by Basil Bourque that I linked to earlier.Anonymous– Anonymous2023-03-27 23:35:29 +00:00Commented Mar 27, 2023 at 23:35
44 Answers
For a 12-hour clock with suffix "AM" or "PM":
DateFormat df = new SimpleDateFormat("KK:mm:ss a, dd/MM/yyyy", Locale.getDefault()); String currentDateAndTime = df.format(new Date()); For a 24-hour clock with suffix "AM" or "PM":
DateFormat df = new SimpleDateFormat("HH:mm:ss a, dd/MM/yyyy", Locale.getDefault()); String currentDateAndTime = df.format(new Date()); To remove the suffix, just remove "a" written with the time format.
Comments
Try this code. It displays the current date and time.
Date date = new Date(System.currentTimeMillis()); SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm aa", Locale.ENGLISH); String var = dateFormat.format(date)); 1 Comment
You can get the time & date separately from Calendar.
// You can pass time zone and Local to getInstance() as parameter Calendar calendar = Calendar.getInstance(); int currentHour = calendar.get(Calendar.HOUR_OF_DAY); int currentMinute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); int date = calendar.get(Calendar.DAY_OF_MONTH); int month = calendar.get(Calendar.MONTH); int year = calendar.get(Calendar.YEAR); Comments
//currentTimeMillis is System.currentTimeMillis() long totalSeconds = currentTimeMillis / 1000; int currentSecond = (int)totalSeconds % 60; long totalMinutes = totalSeconds / 60; int currentMinute = (int)totalMinutes % 60; long totalHours = totalMinutes / 60; int currentHour = (int)totalHours % 12; TextView tvTime = findViewById(R.id.tvTime); tvTime.setText((currentHour + OR - TIME YOU ARE FROM GMT) + ":" + currentMinute + ":" + currentSecond); 2 Comments
String DataString = DateFormat.getDateInstance(DateFormat.SHORT).format(Calendar.getInstance().getTime()); To get the short date formatted String in the localised format of the unit.
I can't understand why so many answers use hardcoded date and time formats when the OS/Java supplies correct localisation of date and time. Isn't it better always to use the formats of the device than of the programmer?
It also supplies the reading of dates in localised formats:
DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT); Date date = null; try { date = format.parse(DateString); } catch(ParseException e) { } Then it is up to the user setting the format to show the dates and time and not you. Regardless of languages, etc., there are different formats in different countries with the same language.
Comments
Here are a few ways to get time and date:
public static void getCurrentTimeUsingDate() { Date date = new Date(); String strDateFormat = "hh:mm:ss a"; DateFormat dateFormat = new SimpleDateFormat(strDateFormat); String formattedDate= dateFormat.format(date); Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show(); } Time using Calender
public static void getCurrentTimeUsingCalendar() { Calendar cal = Calendar.getInstance(); Date date=cal.getTime(); DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); String formattedDate=dateFormat.format(date); Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show(); } Local time and date
public static void getCurrentTime(){ System.out.println("-----Current time of your time zone-----"); LocalTime time = LocalTime.now(); Toast.makeText(this, time, Toast.LENGTH_SHORT).show(); } Zone wise Time
public static void getCurrentTimeWithTimeZone(){ Toast.makeText(this, "Current time of a different time zone using LocalTime", Toast.LENGTH_SHORT).show(); ZoneId zoneId = ZoneId.of("America/Los_Angeles"); LocalTime localTime=LocalTime.now(zoneId); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); String formattedTime=localTime.format(formatter); Toast.makeText(this,formattedTime , Toast.LENGTH_SHORT).show(); } Easy way to get the current time and date
import java.util.Calendar Date currentTime = Calendar.getInstance().getTime(); Comments
Kotlin
Here are various ways in to get the current date time in Kotlin.
fun main(args: Array<String>) { println(System.currentTimeMillis()) // Current milliseconds val date = Calendar.getInstance().time // Current date object val date1 = Date(System.currentTimeMillis()) println(date.toString()) println(date1.toString()) val now = Time(System.currentTimeMillis()) // Current time object println(now.toString()) val sdf = SimpleDateFormat("yyyy:MM:dd h:mm a", Locale.getDefault()) println(sdf.format(Date())) // Format current date println(DateFormat.getDateTimeInstance().format(System.currentTimeMillis())) // using getDateTimeInstance() println(LocalDateTime.now().toString()) // Java 8 println(ZonedDateTime.now().toString()) // Java 8 } Comments
There is a ISO8601Utils utilities class in the com.google.gson.internal.bind.util package, so if you Gson in your app you can use this.
It supports milliseconds and time zones, so it's a pretty good option right out of the box.
Comments
You can get the current date and time using this code:
val current_data_time= SimpleDateFormat("MMMMddyyyyHHmm", Locale.getDefault()) val currentDateandTime: String = current_data_time.format(Date()) If you use MMMM: Then month name shows e.g. "March"
If you use MM: Then number shows e.g. "3"
dd for day and yyyy for year
If you want only the last two digits then yy.
If you change month and year first and last then need to change MMMM and dd and yyyy left and right, e.g., 12/3/2021 12:12 dd/MM/YYYY HH:mm
Comments
In Kotlin, you can get current time data using Calendar.
val calendar = Calendar.getInstance() This will give this output, Thu Apr 06 17:38:57 GMT+05:30 2023
Get current time hour, minute and second value like this.
val hour = calendar.get(Calendar.HOUR_OF_DAY) val minute = calendar.get(Calendar.MINUTE) val second = calendar.get(Calendar.SECOND) val currentTime = "$hour:$minute:$second" Output of currentTime will be this 17:38:57.
