How to get Current Date in Android Kotlin

Sudish Kumar
2 min readFeb 11, 2021

Hi friends, I have been using current date or today date in my app. It is easy to implement . For getting today date we will use Calendar.getInstance() it gives us or initialize with current date and time.

It’s use

We use today date for comparing dates, showing today date and shaving current date. Let’s understand with, we all know flipkart, Amazon And so many shopping apps these all take today date. On booking time they stores today date and time. And sometimes we get coupons in the coupons there are today date and expiry date has been given and it’s also compared.

Let’s coding

In .kt file(MainActivity.kt)

val calender = Calendar.getInstance()

val year = calender.get(Calendar.YEAR)
val month = calender.get(Calendar.MONTH) + 1
val day = calender.get(Calendar.DAY_OF_MONTH)
val date = "$year-$month-$day"
print(date)

Adding 5 days in current date

Val fiveDaysExtra = day + 5

There is one thing to keep in mind day value should not be extra or enough to month value like, day value is 33 and month value is 31 in this situation you need to compare days value from month value if day value is greater than month value then add 1 in month.

Like,

val calender = Calendar.getInstance()
val year = calender.get(Calendar.YEAR)
val month = calender.get(Calendar.MONTH) + 1
val dayOfMonth = calender.get(Calendar.DAY_OF_MONTH)
val day = calender.get(Calendar.DAY_OF_MONTH)
val date = "$year-$month-$day"
val allDaysInMonth = calender.getActualMaximum(Calendar.DATE)

// Adding and subtracting five days in today date
val addFiveDaysExtra = day + 5
// one thing should be remember we will check is date is greater than month day of month or or not it or days may be
if (addFiveDaysExtra > allDaysInMonth ){
val addOneInMonth = month + 1
val subDate = addFiveDaysExtra - allDaysInMonth
val filterDate = "$year-$addOneInMonth-$subDate"
Toast.makeText(this, "Five days extra in the current date or today is $filterDate", Toast.LENGTH_SHORT).show()
}else{
val fiveDaysExtraDate = "$year-$month-$addFiveDaysExtra"
// one thing should be remember we will check is date is greater than month day of month or or not
Toast.makeText(this, "Five days extra in the current date or today is $fiveDaysExtraDate", Toast.LENGTH_SHORT).show()
}

I hope you understood is importance and use and how to get current date.

Thank you so much. Wish you good luck🤞🤞🤞🤞.

--

--

Sudish Kumar

Teach lead, Flutter Developer, Android Developer, Node Js Developer