So i'm new to Android development. I've been working on this small software called Tombstone calculator. the user suppose to type in three information. either two information will be used to fill in the last one. Give a Birth Date or Death Date it will compute the years, month, days they lived. otherwise if the Birthday and years lived are provided it will compute the Death date. I already have three Calendar Object set up for those user input, but i'm not sure how to use those to get the result i want. Here is the code i got for now. Also for some reason the onClick method doesn't work.
public class MainActivity extends AppCompatActivity {
private EditText birthdayText;
private EditText deathText;
private EditText yearsLived;
private Button compute;
private TextView LivedYears;
/*
private Date bi;
private Date de;
private Date li;
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
compute = findViewById(R.id.compute);
birthdayText = findViewById(R.id.birthday);
deathText = findViewById(R.id.death);
yearsLived = findViewById(R.id.lived);
LivedYears = findViewById(R.id.LivedYears);
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd ");
final SimpleDateFormat livedFormat = new SimpleDateFormat("yy-MM-dd");
compute.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String birthday = birthdayText.getText().toString();
String death = deathText.getText().toString();
String lived = yearsLived.getText().toString();
try{
if(death.matches("")){
Calendar calBi = Calendar.getInstance();
Date bi = dateFormat.parse(birthday);
calBi.setTime(bi);
Calendar calLi = Calendar.getInstance();
Date li = livedFormat.parse(lived);
calLi.setTime(li);
}else if(birthday.matches("")){
Calendar calDe = Calendar.getInstance();
Date de = dateFormat.parse(death);
calDe.setTime(de);
Calendar calLi = Calendar.getInstance();
Date li = livedFormat.parse(lived);
calLi.setTime(li);
}else if (lived.matches("")){
Calendar calBi = Calendar.getInstance();
Date bi = dateFormat.parse(birthday);
calBi.setTime(bi);
Calendar calDe = Calendar.getInstance();
Date de = dateFormat.parse(death);
calDe.setTime(de);
long diff = calDe.getTimeInMillis() - calBi.getTimeInMillis();
LivedYears.setText(Long.toString(diff));
}
}catch (ParseException e){
e.printStackTrace();
}
}
});
}
}