# Calculate no of days between two dates Kotlin

**URL:** https://discuss.kotlinlang.org/t/calculate-no-of-days-between-two-dates-kotlin/9826
**Category:** Android
**Created:** [October 13, 2018, 7:26am UTC](https://discuss.kotlinlang.org/t/calculate-no-of-days-between-two-dates-kotlin/9826 "2018-10-13T07:26:02Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![aliyark145](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/aliyark145/32/5596_2.png) [@aliyark145](https://discuss.kotlinlang.org/u/aliyark145)
#### Post date: [October 13, 2018, 7:26am UTC](https://discuss.kotlinlang.org/t/calculate-no-of-days-between-two-dates-kotlin/9826/1 "2018-10-13T07:26:02Z")

</div>

I want to calculate no of days between two dates.Dates are in the form of string likes this “2018-04-13T20:00:00.0400” ?

---

<div class="post-metadata">

### Author: ![madmax1028](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/madmax1028/32/3335_2.png) [@madmax1028](https://discuss.kotlinlang.org/u/madmax1028)
#### Post date: [October 13, 2018, 8:45am UTC](https://discuss.kotlinlang.org/t/calculate-no-of-days-between-two-dates-kotlin/9826/2 "2018-10-13T08:45:20Z")

</div>

Choose the one you like best 🙂

```auto
val date1 = LocalDateTime.parse("2018-04-13T20:00:00.0400")
val date2 = <<other date>>

println(ChronoUnit.DAYS.between(date1, date2))
println(Duration.between(date1, date2).toDays())
println(date1.until(date2, ChronoUnit.DAYS))

```

---

<div class="post-metadata">

### Author: ![aliyark145](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/aliyark145/32/5596_2.png) [@aliyark145](https://discuss.kotlinlang.org/u/aliyark145)
#### Post date: [October 13, 2018, 11:47am UTC](https://discuss.kotlinlang.org/t/calculate-no-of-days-between-two-dates-kotlin/9826/3 "2018-10-13T11:47:39Z")

</div>

This results in this exception  
java.time.format.DateTimeParseException: Text ‘2018-04-13T20:00:00.0400’ could not be parsed, unparsed text found at index 10  
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1952)  
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)  
at java.time.LocalDate.parse(LocalDate.java:394)  
at java.time.LocalDate.parse(LocalDate.java:379)  
at waleed.com.eventstoday.ui.home.EventAdapter.calculateNoOfDays(EventAdapter.kt:46)  
at waleed.com.eventstoday.ui.home.EventAdapter.onBindViewHolder(EventAdapter.kt:34)  
at waleed.com.eventstoday.ui.home.EventAdapter.onBindViewHolder(EventAdapter.kt:20)  
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673)  
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)  
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)  
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)  
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)  
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)  
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)  
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)  
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)  
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)  
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)  
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3225)  
at android.view.View.measure(View.java:23169)  
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)  
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)  
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1187)  
at android.widget.LinearLayout.onMeasure(LinearLayout.java:706)  
at android.view.View.measure(View.java:23169)  
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)  
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)  
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)  
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)  
at android.view.View.measure(View.java:23169)  
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)  
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)  
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)  
at android.view.View.measure(View.java:23169)  
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)  
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)  
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)  
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)  
at android.view.View.measure(View.java:23169)  
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)  
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)  
at android.view.View.measure(View.java:23169)  
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)  
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)  
at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)  
at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)  
at android.view.View.measure(View.java:23169)  
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)  
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)  
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:716)  
at android.view.View.measure(View.java:23169)  
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2718)  
2018-10-11 19:09:27.062 5355-5355/waleed.com.eventstoday E/AndroidRuntime: at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1572)  
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1855)  
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)  
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)  
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)  
at android.view.Choreographer.doCallbacks(Choreographer.java:761)  
at android.view.Choreographer.doFrame(Choreographer.java:696)  
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)  
at android.os.Handler.handleCallback(Handler.java:873)  
at android.os.Handler.dispatchMessage(Handler.java:99)  
at android.os.Looper.loop(Looper.java:193)  
at android.app.ActivityThread.main(ActivityThread.java:6669)  
at java.lang.reflect.Method.invoke(Native Method)  
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

---

<div class="post-metadata">

### Author: ![aliyark145](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/aliyark145/32/5596_2.png) [@aliyark145](https://discuss.kotlinlang.org/u/aliyark145)
#### Post date: [October 13, 2018, 11:50am UTC](https://discuss.kotlinlang.org/t/calculate-no-of-days-between-two-dates-kotlin/9826/4 "2018-10-13T11:50:55Z")

</div>

Oh found it … sorry i was making mistake it is using LocalDateTime and i was using LocalDate !!! Thanks @mamax1028

---

<div class="post-metadata">

### Author: ![beezerbtt](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/beezerbtt/32/5777_2.png) [@beezerbtt](https://discuss.kotlinlang.org/u/beezerbtt)
#### Post date: [October 15, 2018, 3:12pm UTC](https://discuss.kotlinlang.org/t/calculate-no-of-days-between-two-dates-kotlin/9826/5 "2018-10-15T15:12:46Z")

</div>

Strictly speaking this is not Kotlin.  
These are the Java 8 java.time classes being re-used in a Kotlin program.  
Of course, Kotlin’s philosophy based on the recent KotlinConf is partly Reuse as the opening speaker remarked.

However, and strictly to answer the question, it should be qualified whether or not the Kotlin standard library has any data specific stuff (for want of a better word) that replaces that of Java 8.

Furthermore, and based on the recent Oracle changes in Java licensing, I am wondering if those that will switch out of Java, will then have to refactor code that Kotlin relies on? such as the example above.
