Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin: Getting the difference betweeen two dates (now and previous date)

Sorry if similar questions have been asked too many times, but it seems that there's one or more issues with every answer I find.

I have a date in the form of a String: Ex.: "04112005"

This is a date. 4th of November, 2005.

I want to get the difference, in years and days, between the current date and this date.

The code I have so far gets the year and just substracts them:

fun getAlderFraFodselsdato(bDate: String): String {
    val bYr: Int = getBirthYearFromBirthDate(bDate)
    var cYr: Int = Integer.parseInt(SimpleDateFormat("yyyy").format(Date()))

    return (cYr-bYr).toString()
}

However, naturally, this is quite innacurate, since the month and days aren't included.

I've tried several approaches to create Date, LocalDate, SimpleDate etc. objects and using these to calcualate the difference. But for some reason I haven't gotten any of them to work.

I need to create a Date (or similar) object of the current year, month and day. Then I need to create the same object from a string containing say, month and year (""04112005""). Then I need to get the difference between these, in years, months and days.

All hints are appreciated.

like image 347
Hfrav Avatar asked Feb 21 '20 11:02

Hfrav


People also ask

How do you find the difference between two dates on Kotlin?

This is what I do: long diff = date1. getTime() - date2. getTime(); Date diffDate = new Date(diff);

How can I get the difference between two dates in Android?

This example demonstrates how do I get the difference between two dates in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do you find the difference between two date objects?

Calculate the no. of days between two dates, divide the time difference of both the dates by no. of milliseconds in a day (1000*60*60*24) Print the final result using document.


1 Answers

I would use java.time.LocalDate for parsing and today along with a java.time.Period that calculates the period between two LocalDates for you.
See this example:

fun main(args: Array<String>) {
    // parse the date with a suitable formatter
    val from = LocalDate.parse("04112005", DateTimeFormatter.ofPattern("ddMMyyyy"))
    // get today's date
    val today = LocalDate.now()
    // calculate the period between those two
    var period = Period.between(from, today)
    // and print it in a human-readable way
    println("The difference between " + from.format(DateTimeFormatter.ISO_LOCAL_DATE)
            + " and " + today.format(DateTimeFormatter.ISO_LOCAL_DATE) + " is "
            + period.getYears() + " years, " + period.getMonths() + " months and "
            + period.getDays() + " days")
}

The output for a today of 2020-02-21 is

The difference between 2005-11-04 and 2020-02-21 is 14 years, 3 months and 17 days
like image 61
deHaar Avatar answered Sep 24 '22 23:09

deHaar