0

its an age calculator project I need to know how to convert multiple EditText values into one date String in an android studio? keep in mind I am using "joda-time library" it's not showing the result. I don't know where I did a mistake! I have forgotten everything I can not fix now hope you guys can help me with that. thanks

public void dateOfBirth(){

    String day = editTextDay.getText().toString().trim();
    String month = editTextMonth.getText().toString().trim();
    String year = editTextYear.getText().toString().trim();

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
    String sDate = day+"/"+month+"/"+year;

    long date = System.currentTimeMillis();
    String eDate = simpleDateFormat.format(date);



    try {
       Date date1 = simpleDateFormat.parse(sDate);
       Date date2 =simpleDateFormat.parse(eDate);
        /* long eDate = System.currentTimeMillis();
        Date date2 = simpleDateFormat.parse(String.valueOf((eDate)));*/

        long startDate = date1.getTime();
        long endDate =date2.getTime();

        if (startDate<=endDate){

           Period period = new Period(startDate, endDate, PeriodType.yearMonthDay());
           int years = period.getYears();
           int months =period.getMonths();
           int days = period.getDays();

            textViewDay.setText(days);
            textViewMonth.setText(months);
            textViewYear.setText(years);

        }


    } catch (ParseException e) {
        e.printStackTrace();
    }



}
  • You should provide a String to `setText`. Do `textViewDay.setText(String.valueOf(days));` instead. – Kidus Feb 11 '20 at 18:14
  • i am bit confused with your comment can you please explain to me a little more details. – MD Tayobur Rahman Feb 11 '20 at 18:29
  • Since you are using Joda-Time, I recommend you go all-in and forget about the classes `Date` and`SimpleDateFormat`, which are poorly designed and troublesome. Joda-Time is far superior and offers all the functionality you need. – Ole V.V. Feb 11 '20 at 18:50
  • Please specify the expected result of your code and how observed result differs. Quote any error message literally. *Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself.* (from [What topics can I ask about here?](https://stackoverflow.com/help/on-topic)) – Ole V.V. Feb 11 '20 at 19:23

2 Answers2

1

Go all-in on Joda-Time

    String day = "11";
    String month = "4";
    String year = "2012";

    String sDate = "" + year + '-' + month + '-' + day;
    LocalDate dob = new LocalDate(sDate);

    LocalDate today = new LocalDate();

    if (dob.isBefore(today)) {
        Period period = new Period(dob, today, PeriodType.yearMonthDay());
        int years = period.getYears();
        int months = period.getMonths();
        int days = period.getDays();

        System.out.println("" + years + " years " + months + " months " + days + " days");
    }

When I ran the above snippet just now, the output was:

7 years 10 months 0 days

Since you are interested in years, months and days, you don’t need DateTime objects (though they would work). They include time of day too. Just use LocalDate.

The classes SimpleDateFormat and Date are poorly designed and long outdated, the former in particular notoriously troublesome. I recommend you stay away from those, and also from representing a point in time as a long count of milliseconds since the epoch. The good choices are:

  1. Stay with Joda-Time since you are already using Joda-Time.
  2. Migrate to java.time, the modern Java date and time API.
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

this is how i solve my problem -

String day = editTextDay.getText().toString().trim();
    String month = editTextMonth.getText().toString().trim();
    String year = editTextYear.getText().toString().trim();

  //  SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");


    String BirthDate ="" + year + '-' + month + '-' + day;
    LocalDate sDate = new LocalDate(BirthDate);
    LocalDate today = new LocalDate();

    Period period = new Period(sDate, today, PeriodType.yearMonthDay());
    int years = period.getYears();
    int months =period.getMonths();
    int days = period.getDays();

    textViewDay.setText(""+days);
    textViewMonth.setText(""+months);
    textViewYear.setText(""+years);