Click here to Skip to main content
15,747,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There is to be two activities , one activity should accept name and D.O.B and then i click the submit button which will take me to another activity where it shows the age (just the number nothing else) , the zodiac sign or picture and the birthstone. I have been trying to get the age calculated based on the date of birth but it just prints eg: 12/24/2022 , so it doesn't calculate anything. I have been trying to use a date picker dialogue but it wont show up when i click on the input space for D.O.B. Can i get some help?

What I have tried:

         private lateinit var sname: TextView
         private lateinit var sage: TextView
         private lateinit var sBstone: TextView
         private lateinit var sczodiac: TextView
         private lateinit var swelcome: TextView

        sname = findViewById(
        sage = findViewById(
        sBstone = findViewById(
        sczodiac = findViewById(
        swelcome = findViewById(

val bundle = intent.extras

        if (bundle != null) {
            sname.text = "Name = ${bundle.getString("name")}"
            sage.text = "Age = ${bundle.getString("dob")}"
            sBstone.text = "BirthStone = ${bundle.getString("dob")}"
            sczodiac.text = "Chinese Zodiac  = ${bundle.getString("dob")}"
            swelcome.text = "Welcome ${bundle.getString("name")}"

            // calculate and display age, birthstone, and zodiac sign here


    private fun Printage(view: View) {

        val myCalendar = Calendar.getInstance()
        val year = myCalendar.get(Calendar.YEAR)
        val month = myCalendar.get(Calendar.MONTH)
        val day = myCalendar.get(Calendar.DAY_OF_MONTH)

        DatePickerDialog(this, { datePicker, year, month, day ->

            val selectedDate = "$day/${month + 1}/$year"

            val inputDOB2 = findViewById<TextView>(
            inputDOB2.text = selectedDate

            val dob = Calendar.getInstance()
            dob.set(year, month, day)

            var age = myCalendar.get(Calendar.YEAR) - dob.get(Calendar.YEAR)
            if (myCalendar.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)) {

            val textage = findViewById<TextView>(
            textage.text = "$sage "

        }, 2023, 5, 3).show()

Updated 13-Feb-23 1:12am
Richard MacCutchan 12-Feb-23 3:08am    
The last few lines of the above code look incorrect.
            val textage = findViewById<TextView>(
            textage.text = "$sage " // this should be the age value

        }, 2023, 5, 3).show() // what is this?
chilly45 12-Feb-23 3:23am    
okay so i was trying to get the date picker to show , isn't show() how it should be used to show the calendar view?
$sage is the textview for the age . wait i forgot to put something
Richard MacCutchan 12-Feb-23 3:53am    
Sorry, I forgot this is Kotlin, not Java. Is the 's' character in "$sage " some formatting type? Actually that should be "$age ", not "$sage ", Just shows you need to take care when creating variable names.
chilly45 12-Feb-23 4:11am    
i tried doing what you said , this is how it looks after, the date picker didnt show up so im not sure whats wrong, i just put it in manually
chilly45 12-Feb-23 3:58am    
oh no that was just a random name so i dont get them mixed up , so your saying it should be $age in stead? okay i will try that.

1 solution

Solution was given at Stack - Calculate age from BirthDate[^]

Using Kotlin
fun getAge(year: Int, month: Int, dayOfMonth: Int): Int {
    return Period.between(
                LocalDate.of(year, month, dayOfMonth),

return getAge;

Imports needed to use java.time (java 8+)
import java.time.LocalDate;
import java.time.Period

There's API Desugaring now in Android, which makes (a subset of) java.time directly available (no backport library needed anymore) to API levels below 26 (not really down to version 1, but will do for most of the API levels that should be supported nowadays).

For projects supporting Java 6 or 7, this functionality is available via the ThreeTenBP[^],
while there is special version, the ThreeTenABP [^] for API levels below 26 in Android.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900