1303

How can I get the current time and date in an Android app?

3
  • 8
    43 answers! While many of them were good when they were written, the good answer to use in 2018 is here. Commented Sep 26, 2018 at 12:11
  • 1
    @OleV.V. How about in 2023? Commented Mar 27, 2023 at 23:23
  • 3
    @user16217248, I still recommend the answer by Basil Bourque that I linked to earlier. Commented Mar 27, 2023 at 23:35

44 Answers 44

1
2
5

Try this to get the current date and time in an easy way:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z"); String currentDateandTime = sdf.format(new Date()); 

Enter image description here

Sign up to request clarification or add additional context in comments.

Comments

4

Try to use the below code:

 Date date = new Date(); SimpleDateFormat dateFormatWithZone = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",Locale.getDefault()); String currentDate = dateFormatWithZone.format(date); 

Comments

4

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

3

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

Are you sure? The Date class was deprecated in 1997.
3

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

3
 //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

How does that get the current time and date?
edited the code..hadn't explained what currentTimeMillis is
2
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

2

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

2

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

1

You can get your local time with GMT time from this function

public String getCurrentDate() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd hh:mm a zzz"); Date date = new Date(); sdf.setTimeZone(TimeZone.getTimeZone("GMT+6:00")); return sdf.format(date); } 

Comments

1

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

1

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

1

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.

Comments

0

we can get time by Date() also

Comments

1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.