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 version = "1.0" encoding = "utf-8" ?>
< RelativeLayout 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.
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.