Android Age Calculator

In this application we calculate total year, months and day of our age.
You simply enter Data of Birth to calculate it.
Let start the fun
First we Create AgeCalcualtion class where we add all calculation method to calculate Year, Months and Day from date of Birth

public class AgeCalculation {
private int startYear;
private int startMonth;
private int startDay;
private int endYear;
private int endMonth;
private int endDay;
private int resYear;
private int resMonth;
private int resDay;
public String getCurrentDate()
{
Calendar c=Calendar.getInstance();
endYear=c.get(Calendar.YEAR);
endMonth=c.get(Calendar.MONTH);
endMonth++;
endDay=c.get(Calendar.DAY_OF_MONTH);
return endDay+”:”+endMonth+”:”+endYear;
}
public void setDateOfBirth(int sYear, int sMonth, int sDay)
{
startYear=sYear;
startMonth=sMonth;
startMonth++;
startDay=sDay;

}
public void calcualteYear()
{
resYear=endYear-startYear;

}

public void calcualteMonth()
{
if(endMonth>=startMonth)
{
resMonth= endMonth-startMonth;
}
else
{
resMonth=endMonth-startMonth;
resMonth=12+resMonth;
resYear–;
}

}
public void calcualteDay()
{

if(endDay>=startDay)
{
resDay= endDay-startDay;
}
else
{
resDay=endDay-startDay;
resDay=30+resDay;
//resMonth–;
}
}

public String getResult()
{
return resDay+”:”+resMonth+”:”+resYear;
}

}


Now we Activity class where we add a button . When user click on the button Date Picker Dialog will be display. When user Set Data of Birth and enter Set button .All calculation will be done from AgeCalculation class.
Here is the activity class code.

public class MainActivity extends Activity implements OnClickListener{
private Button btnStart;
static final int DATE_START_DIALOG_ID = 0;
private int startYear=1970;
private int startMonth=6;
private int startDay=15;
private AgeCalculation age = null;
private TextView currentDate;
private TextView birthDate;
private TextView result;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
age=new AgeCalculation();
currentDate=(TextView) findViewById(R.id.textView1);
currentDate.setText( age.getCurrentDate());
birthDate=(TextView) findViewById(R.id.textView2);
result=(TextView) findViewById(R.id.textView3);
btnStart=(Button) findViewById(R.id.button1);
btnStart.setOnClickListener(this);

}

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_START_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
startYear, startMonth, startDay);
}
return null;
}

private DatePickerDialog.OnDateSetListener mDateSetListener
= new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
startYear=selectedYear;
startMonth=selectedMonth;
startDay=selectedDay;
age.setDateOfBirth(startYear, startMonth, startDay);
birthDate.setText(“”+selectedDay+”:”+(startMonth+1)+”:”+startYear);
calculateAge();
}
};
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
showDialog(DATE_START_DIALOG_ID);
break;

default:
break;
}
}
private void calculateAge()
{
age.calcualteYear();
age.calcualteMonth();
age.calcualteDay();
Toast.makeText(getBaseContext(), “click the resulted button”+age.getResult() , Toast.LENGTH_SHORT).show();
result.setText(age.getResult());
}
}



Now we write the layout of Activity class

<br /> <relativelayout xmlns:andro xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"></p> <p><button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="Date Of Birth" /></p> <p><textView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/button1" android:layout_marginTop="23dp" android:text="Current Date" /></p> <p><textView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:layout_marginTop="18dp" android:text="Birth Date" /></p> <p><textView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView2" android:layout_marginTop="16dp" android:text="Result" /></p> <p></relativeLayout></p> <p>

Screen shot


Source code and Apk file download from the following link
HERE

Related Posts

Earn Cash on Fiverr: Unlock Your Earning Potential in the Gig Economy

  Have you ever dreamed of turning your skills into cold, hard cash? Well, buckle up, because we’re about to embark on a journey through the exciting…

Become a Freelancer on Guru: How to Land High-Paying Gigs

  Are you ready to take control of your career and dive into the world of freelancing? Look no further than Guru, a platform brimming with opportunities…

how to earn 5 dollar per day from doing micro job

 There are many website which offer micro job . and you can earn 2 or 3 dollar per day from these website when you work few hours…

How to Create blogger website or Blog in Mobile : Complete Tutorial Step by Step

 Blogger blog or website can create easily without any special skills required. Second it’s no need domain and web hosting cost. Therefore creating and develope blogger website…

LinkedIn How to complete 500 connection in one week: Earning Online Unique Method

How You will Complete 500 connection within week. To learn the trick Please watch the video tutorial, then you will easily complete 500 connection within week  1….

How to earn money from used mobile buy sell bussiness

Almost every person of the word are using mobile. many people use 2 or 3 phone at same time. and mostly people change phone after any phone…

This Post Has 12 Comments

  1. Nice Tutorial but need some changes in AgeCalculation() Functions brother.

    import java.util.Calendar;

    /**
    * Created by Prash on 9/6/2017.
    */

    public class AgeCalculation {
    private int startYear;
    private int startMonth;
    private int startDay;
    private int endYear;
    private int endMonth;
    private int endDay;
    private int resYear;
    private int resMonth;
    private int resDay;

    public String getCurrentDate()
    {
    Calendar c= Calendar.getInstance();
    endYear=c.get(Calendar.YEAR);
    endMonth=c.get(Calendar.MONTH);
    endMonth++;
    endDay=c.get(Calendar.DAY_OF_MONTH);
    return endDay+":"+endMonth+":"+endYear;
    }
    public void setDateOfBirth(int sYear, int sMonth, int sDay)
    {
    startYear=sYear;
    startMonth=sMonth;
    //startMonth++;
    startDay=sDay;

    }
    public void calcualteYear()
    {
    resYear=endYear-startYear;
    }

    public void calcualteMonth()
    {
    if(endMonth>=startMonth)
    {
    if(startMonth == endMonth){

    if(endDay>=startDay){
    resMonth = 0;
    } else {
    resMonth= endMonth-startMonth;
    resMonth=12+resMonth-1;
    }
    resYear–;
    } else {
    resMonth= endMonth-startMonth;

    if(resMonth == 1){
    resMonth = 0;
    } else {
    resMonth = resMonth – 1;
    }
    }
    }
    else
    {
    resMonth=endMonth-startMonth;
    resMonth=12+resMonth-1;
    resYear–;
    }
    }
    public void calcualteDay()
    {

    if(endDay>=startDay)
    {
    resDay= endDay-startDay;
    }
    else
    {
    resDay=endDay-startDay;
    resDay=30+resDay;
    //resMonth–;
    }
    }

    public String getResult()
    {
    return resDay+":"+resMonth+":"+resYear;
    }

    public int getDay(){
    return resDay;
    }

    public int getMonth(){
    return resMonth;
    }

    public int getYear(){
    return resYear;
    }

    }

Leave a Reply

Your email address will not be published. Required fields are marked *