I am a beginner in Android and I need code to get today number (from local date).
Like this way:
A var
is Iday
. And today is 8th October so Iday
should be equal to 8
.
How to get date in Android (Kotlin)
1.1k views Asked by e88990 At
2
There are 2 answers
1
On
You can use Callendar
and get
method.
import java.util.*
val cal = Calendar.getInstance()
val dayOfMonth = cal[Calendar.DAY_OF_MONTH]
println(dayOfMonth) // 8 -> 8th October 2020
getInstance()
:
Gets a calendar using the default time zone and locale. The Calendar returned is based on the current time in the default time zone with the default locale.
Deprecated:
import java.util.*
val currentTime = Calendar.getInstance().time
println(currentTime.date) // 8
LocalDate#getDayOfMonth
Output:
If you want to get today's date at some other time zone, use
LocalDate now(ZoneId zone)
as shown below:Output: