0

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();
                }




            }
        });


    }
}
Dr Mido
  • 2,414
  • 4
  • 32
  • 72
  • diff their values: `calB.getTime() - calA.getTime()` ... – Martin Zeitler Mar 11 '19 at 20:11
  • For date math like this consider throwing away the long outmoded and notoriously troublesome `Date`, `Calendar` and `SimpleDateFormat` classes and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Mar 12 '19 at 00:17
  • You are really asking a couple of related questions, each of which has been asked and answered many times on Stack Overflow and elsewhere already. Please use your search engine and find some good answers. Consider answers using `LocalDate` and either `Period` or `ChronoUnit.YEARS` the best. – Ole V.V. Mar 12 '19 at 00:23
  • Setting up a `Calendar` object for the period of time lived doesn’t make sense. A `Calendar` is a calendar date (and time and timezone and more, it’s truly bloated), it cannot represent a length of time. The `Period` class from java.time is for that. – Ole V.V. Mar 12 '19 at 00:26
  • Possible duplicate of [How do I calculate someone's age in Java?](https://stackoverflow.com/questions/1116123/how-do-i-calculate-someones-age-in-java) – Ole V.V. Mar 12 '19 at 00:27

0 Answers0