Find Days Between Two Dates in Android
Last Updated :
06 Feb, 2022
There are many travels and accommodation-based Android applications which we use to check and buy tickets for any traveling and hotel booking. Inputs such as origin, traveling destination, selection of hotel, and trip dates are needed to book appropriate options. In the case of hotel booking, the number of days is calculated by finding the difference between the two dates provided they are separated by at least one day and the check-out date is after the check-in date.
So in this article, we will show you how you could find the difference between two given dates in Android. Follow the below steps once the IDE is ready.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add a TextView that we will use to display the difference between the two dates.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text_view_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:textSize="30sp"/>
</RelativeLayout>
Step 3: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package org.geeksforgeeks.difftwodays
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import java.text.SimpleDateFormat
class MainActivity : AppCompatActivity() {
@SuppressLint("SimpleDateFormat", "SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Declaring and initializing
// the TextView from the layout file
val mTextView = findViewById<TextView>(R.id.text_view_1)
// Declaring two date strings
val mDate1 = "01/01/2022"
val mDate2 = "01/08/2022"
// Creating a date format
val mDateFormat = SimpleDateFormat("MM/dd/yyyy")
// Converting the dates
// from string to date format
val mDate11 = mDateFormat.parse(mDate1)
val mDate22 = mDateFormat.parse(mDate2)
// Finding the absolute difference between
// the two dates.time (in milli seconds)
val mDifference = kotlin.math.abs(mDate11.time - mDate22.time)
// Converting milli seconds to dates
val mDifferenceDates = mDifference / (24 * 60 * 60 * 1000)
// Converting the above integer to string
val mDayDifference = mDifferenceDates.toString()
// Displaying the result in the TextView
mTextView.text = "The difference between two dates is $mDayDifference days"
}
}
Output:
You can see that the difference between the two dates is displayed in the TextView as shown below.
Similar Reads
How to Get Current Time and Date in Android? Many times in android applications we have to capture the current date and time within our android application so that we can update our date according to that. In this article, we will take a look at how to get the current Time and Date in our android application. Note: This Android article covered
3 min read
Date and Time Formatting in Android Date and Time in Android are formatted using the SimpleDateFormat library from Java, using Calendar instance which helps to get the current system date and time. The current date and time are of the type Long which can be converted to a human-readable date and time. In this article, it's been discus
5 min read
Calendar View App in Android with Kotlin Calendar View is seen in most travel booking applications in which the user has to select the date of the journey. For the selection of the date, this view is used. In this article, we will take a look at How to implement Calendar View within our Android application using Kotlin. A sample video is g
3 min read
Material Design Date Picker in Android using Kotlin Material Design Components (MDC Android) offers designers and developers a way to implement Material Design in their Android applications. Developed by a core team of engineers and UX designers at Google, these components enable a reliable development workflow to build beautiful and functional Andro
3 min read
Android - onDateChangeListener in CalendarView In our day-to-day life, we are using android applications many times, many applications use calendars somehow, for creating a schedule or for creating reminders or date-of-birth chosen in forms. Sometimes we select the date but forgot to confirm it, because of this problem user sets the wrong date.
3 min read