Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android studio中将多个EditText值转换为一个日期字符串?_Android_Datetime_Android Edittext_Jodatime_Android Jodatime - Fatal编程技术网

如何在android studio中将多个EditText值转换为一个日期字符串?

如何在android studio中将多个EditText值转换为一个日期字符串?,android,datetime,android-edittext,jodatime,android-jodatime,Android,Datetime,Android Edittext,Jodatime,Android Jodatime,这是一个年龄计算器项目,我需要知道如何在android studio中将多个EditText值转换为一个日期字符串?请记住,我使用的是“joda时间库”,它没有显示结果。我不知道我哪里做错了!我已经忘记了我现在无法修复的一切,希望你们能帮助我。谢谢 public void dateOfBirth(){ String day = editTextDay.getText().toString().trim(); String month = editTextMonth.getTex

这是一个年龄计算器项目,我需要知道如何在android studio中将多个EditText值转换为一个日期字符串?请记住,我使用的是“joda时间库”,它没有显示结果。我不知道我哪里做错了!我已经忘记了我现在无法修复的一切,希望你们能帮助我。谢谢

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



}
public void dateof birth(){
String day=editTextDay.getText().toString().trim();
字符串月份=editTextMonth.getText().toString().trim();
字符串year=editTextYear.getText().toString().trim();
SimpleDataFormat SimpleDataFormat=新的SimpleDataFormat(“日/月/年”);
字符串sDate=日+“/”+月+“/”+年;
长日期=System.currentTimeMillis();
字符串eDate=simpleDateFormat.format(日期);
试一试{
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();
如果(开始日期)在Joda时间全力以赴
刚才我运行上述代码段时,输出是:

7年10个月0天

由于您对年、月和日感兴趣,因此不需要
DateTime
对象(尽管它们可以工作)。它们也包括一天中的时间。只需使用
LocalDate

SimpleDateFormat
Date
的设计很差,而且很早就过时了,前者尤其是出了名的麻烦。我建议您远离这些类,也不要将时间点表示为从纪元开始的
long
毫秒计数。好的选择是:

  • 由于您已经在使用Joda Time,请与Joda Time保持联系
  • 迁移到java.time,现代java日期和时间API

  • 这就是我解决问题的方法-

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

    您应该向
    setText
    .Do
    textViewDay.setText(String.valueOf(days))提供一个字符串
    相反。我对你的评论有点困惑。你能给我解释一下更多的细节吗?既然你使用的是Joda Time,我建议你全力以赴,忘记那些设计拙劣且麻烦的类
    Date
    SimpleDateFormat
    。Joda Time的优越性远远超过你所能提供的所有功能eed。请指定代码的预期结果以及观察到的结果的差异。逐字引用任何错误消息。寻求调试帮助的问题(“为什么此代码不工作?”)必须包括所需的行为、特定问题或错误以及在问题本身中重现它所需的最短代码。(来源)
    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);