Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. public class MainActivity extends Activity {
  2.  
  3. Button btn;
  4.  
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9.  
  10. btn = (Button) findViewById(R.id.btn);
  11. btn.setOnClickListener(new View.OnClickListener() {
  12. @Override
  13. public void onClick(View v) {
  14. PasteData();
  15. }
  16. });
  17. }
  18.  
  19. private void PasteData(){
  20. Calendar c = Calendar.getInstance();
  21. int dp = c.get(Calendar.DAY_OF_MONTH);
  22. final int mp = c.get(Calendar.MONTH);
  23. int yp = c.get(Calendar.YEAR);
  24.  
  25. DatePickerDialog dpd = new DatePickerDialog(MainActivity.this, AlertDialog.THEME_HOLO_DARK, new DatePickerDialog.OnDateSetListener() {
  26. @Override
  27. public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
  28. Intent i = new Intent(MainActivity.this, SecondActivity.class);
  29. i.putExtra("DayB",dayOfMonth);
  30. i.putExtra("MonthB",month);
  31. i.putExtra("YearB",year);
  32. btn.setText(dayOfMonth+"/"+(month+1)+"/"+year);
  33. }
  34. },dp,mp,yp);
  35. c.add(Calendar.SECOND, 1);
  36. dpd.getDatePicker().setMaxDate(c.getTimeInMillis());
  37. dpd.getDatePicker().setMinDate(0);
  38. dpd.show();
  39. }
  40. }
  41.  
  42. public class SecondActivity extends Activity {
  43.  
  44. TextView tvAge;
  45.  
  46. @Override
  47. protected void onCreate(Bundle savedInstanceState) {
  48. super.onCreate(savedInstanceState);
  49. setContentView(R.layout.activity_second);
  50.  
  51. Bundle BB = this.getIntent().getExtras();
  52.  
  53. int d = BB.getInt("DayB");
  54. int m = BB.getInt("MonthB");
  55. int y = BB.getInt("YearB");
  56. CalculateAge(d,m,y);
  57. }
  58.  
  59. private void CalculateAge (int dayBd, int monthBd, int yearBd){
  60. Calendar c = Calendar.getInstance();
  61. int ActDay = c.get(Calendar.DAY_OF_MONTH);
  62. int ActMonth = c.get(Calendar.MONTH);
  63. int ActYear = c.get(Calendar.YEAR);
  64.  
  65. int age = ActYear-yearBd;
  66. if ( monthBd>ActMonth){
  67. age--;
  68. }else if (ActMonth==monthBd){
  69. if (dayBd>ActDay){
  70. age--;
  71. }
  72. }
  73.  
  74. tvAge = (TextView) findViewById(R.id.tvAge);
  75. tvAge.setText(age);
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement