How are you old – date calendar age Java Android example

Calendar dateOfYourBirth = new GregorianCalendar(1998, Calendar.SEPTEMBER, 17); Calendar today = Calendar.getInstance(); Android example.

public class HoriziontalScrollActivity extends Activity {
	TextView txtV;
	Context cntx;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txtV = (TextView)findViewById(R.id.idLabel);
        cntx = this;
        StringBuilder strBuild = new StringBuilder();
 
        // enter your date of birth
        Calendar dateOfYourBirth = new GregorianCalendar(1998, Calendar.SEPTEMBER, 17);
        Calendar today = Calendar.getInstance();
        int yourAge = today.get(Calendar.YEAR) - dateOfYourBirth.get(Calendar.YEAR);
        dateOfYourBirth.add(Calendar.YEAR, yourAge);
        if (today.before(dateOfYourBirth)) {
            yourAge--;
        }  
        strBuild.append("You are " + yourAge + " old!");
         txtV.setText(strBuild);
    }
}
This entry was posted in Android Examples Code and tagged , , , , . Bookmark the permalink.


Comments are closed.



14,128 views