1
val theDate=sdf.parse(selectedDate)

            theDate?.let {

                val selectedDateInMin=theDate.time / 60000
                val selectedDateInYear=(theDate.time/ 31536000000)
                val selectedDateInMonth=(theDate.time)/2629746000
                val selectedDateInDay=(theDate.time)/86400000

                val currentDate=sdf.parse(sdf.format(System.currentTimeMillis()))
                currentDate?.let {

                    val currentDateInMin=currentDate.time/60000
                    val currentDateInYear=(currentDate.time/ 31536000000)
                    val currentDateInMonth=(currentDate.time)/2629746000
                    val currentDateInDay=(currentDate.time)/86400000

                    val differenceInMin = currentDateInMin-selectedDateInMin
                    val differenceInYear=currentDateInYear-selectedDateInYear-1

                    val leap_days=(differenceInYear*(0.24)).toInt()

                    val differenceInMonth=currentDateInMonth-selectedDateInMonth-(differenceInYear*12)

                    val differenceInDay=currentDateInDay-selectedDateInDay-(differenceInYear*365)-(differenceInMonth*31)-leap_days

When I am calculating difference in Days then I have to subtract the number of days included in the difference of month . Then how will I calculate whether the month has 28 days or 31 days or 30 days and what no. I have to multiply with the differenceInMonth ? How will I genearlize the formula of getting exact number of days in certain period of months for e.g. How many days in 2 months or 4 months ?

Nandini Agarwal
  • 91
  • 1
  • 10
  • Check-out this thread. This might help: https://stackoverflow.com/questions/23323792/android-days-between-two-dates – Mayur Gajra Jan 24 '22 at 16:02

1 Answers1

1
import java.text.SimpleDateFormat
import java.time.Duration

val sdf = SimpleDateFormat("yyyy-MM-dd hh:mm:ss")

val theDate= sdf.parse("2022-03-14 17:09:18")
val now = sdf.parse(sdf.format(System.currentTimeMillis()))

val duration = Duration.between(now.toInstant(), theDate.toInstant()).toDays()

println("$duration days")
lukas.j
  • 6,453
  • 2
  • 5
  • 24
  • This is not working..!! – Nandini Agarwal Jan 24 '22 at 17:41
  • Can you specify what? Does it not compile? Does it give the wrong result? Just shouting is not helpful... – lukas.j Jan 24 '22 at 18:16
  • Actually it is returning wrong result. val currentDate=sdf.parse(sdf.format(System.currentTimeMillis())) currentDate?.let { val differenceInDay=Duration.between(currentDate.toInstant(), theDate.toInstant()).toDays() day_tv?.text=differenceInDay.toString() } } },year,month,day) for e.g., When I am selectting date 7 mar 2003 it is returning 18 yeas 10 months -6889 days and with my code it is returning 18 yeas 10 months 14 days. – Nandini Agarwal Jan 25 '22 at 05:32
  • It seems that your code is not correct. The value for the period staring, and including, 07 March 2003 to, but not including, 25 January 2022 is 6899 days, which is 18 years, 10 months, 18 days. – lukas.j Jan 25 '22 at 08:28