Noo Noo
Noo Noo

Reputation: 35

Return current day in Android Studio using Kotlin

I am using Kotlin in Android Studio and want to show the user the current day at the home screen (of my "app"). Following the Build your first app tutorial I figured I would change the message it displays to what I want (the day), however I don't know how to get the current day.

Searching online I found the fun getDay(): Int function in the documentation but I don't know what to do with it.

Upvotes: 1

Views: 506

Answers (2)

Bernhard Josephus
Bernhard Josephus

Reputation: 715

Use the code below to get the current day:

fun getCurrentDay(): Int {
    val value: Calendar = Calendar.getInstance()
    return value.get(Calendar.DAY_OF_WEEK)
}

It should return the current date in a week(1-7).

You could also use other constants to fill in .get()

I haven't tested the code yet, but hopefully it works.

Upvotes: 1

the_dani
the_dani

Reputation: 2524

Calendar.getInstance().get(Calendar.DAY_OF_MONTH), eg. 12 for today.

Another possibility would be Calendar.DAY_OF_WEEK for weekdays.

Upvotes: 0

Related Questions