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
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.
Now we write the layout of Activity class
Screen shot
Source code and Apk file download from the following link
HERE
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;
}
}
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());
}
}
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
Screen shot
Source code and Apk file download from the following link
HERE
12 Comments
Getting error in MainActivity.java
ReplyDeleteAgeCalculation can not be resolved to a type.
I totally tested it and its work warm good. Please download Source code and Apk for testing and easily implementation
DeleteHello,
ReplyDeleteI am facing an issue with datepicker dialogue.
It is setting the date even when clicking outside of the dialogue.
Please First of all simply run a small code of date picker which are easily available on google then try this apps. Thanks
DeleteIf you want to know your actual age..........
ReplyDeleteDownload Age Calculator now from Softonic: 100% safe and virus free. More than 2399 downloads this month.
Age Calculator APK
if you want to track your any of your friends cell phone. then download the cell phone tracker apk for free.
ReplyDelete@Saadat Rahim: Great job bro !
ReplyDeleteNice Tutorial but need some changes in AgeCalculation() Functions brother.
ReplyDeleteimport 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;
}
}
Thanks Prashant Mali.
ReplyDeleteage calculator
ReplyDeletehow to calculate age in months
ReplyDeletecan u please provide the code for calculating age in months
ReplyDelete