Java 将字符串值从onclick日期列表传递给其他函数

Java 将字符串值从onclick日期列表传递给其他函数,java,android,string,date,Java,Android,String,Date,我需要yyyyMMDD格式的日期,因此我将日期转换为所需格式 String YY = Integer.toString(year); String MM = String.format("%02d", month + 1); String DD = String.format("%02d", day); String selecteddate = YY; selecteddate = selecteddate.concat(MM); final String selecteddate1

我需要yyyyMMDD格式的日期,因此我将日期转换为所需格式

String YY = Integer.toString(year); 
String MM = String.format("%02d", month + 1); 
String DD = String.format("%02d", day); 
String selecteddate = YY; 
selecteddate = selecteddate.concat(MM); 
final String selecteddate1 = selecteddate.concat(DD);
我想使用selecteddata1作为查找日期差异的起始日期。

如果您有Calendar对象,则可以调用getTime方法转换并格式化为任何字符串,例如:

Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMDD");
System.out.println("Formatted date " + dateFormat.format(calendar.getTime()));  
System.out.println("Date Object " + calendar.getTime());
@尼克

试试这个

DatePickerDialog mDatePicker = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
            year = selectedyear;
            month = selectedmonth;
            day = selectedday;
            String date = "" + day + "-" + (month+1) + "-" + year;
            stardate.setText(date);

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

      try {

        Date date1 = simpleDateFormat.parse(""+day+"/"+(month+1)+"/"+year");
        Date date2 = simpleDateFormat.parse("13/10/2013"); //If you want to use the current date, use "new Date()"

    printDifference(date1, date2);

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

    }
        }
    }, year, month, day);
    mDatePicker.setTitle("Please select date");
    mDatePicker.getDatePicker().

    setMaxDate(System.currentTimeMillis());
    mDatePicker.show();
}
});

//1 minute = 60 seconds
    //1 hour = 60 x 60 = 3600
    //1 day = 3600 x 24 = 86400
    public void printDifference(Date startDate, Date endDate){

        //milliseconds
        long different = endDate.getTime() - startDate.getTime();

        System.out.println("startDate : " + startDate);
        System.out.println("endDate : "+ endDate);
        System.out.println("different : " + different);

        long secondsInMilli = 1000;
        long minutesInMilli = secondsInMilli * 60;
        long hoursInMilli = minutesInMilli * 60;
        long daysInMilli = hoursInMilli * 24;

        long elapsedDays = different / daysInMilli;
        different = different % daysInMilli;

        long elapsedHours = different / hoursInMilli;
        different = different % hoursInMilli;

        long elapsedMinutes = different / minutesInMilli;
        different = different % minutesInMilli;

        long elapsedSeconds = different / secondsInMilli;

        System.out.printf(
            "%d days, %d hours, %d minutes, %d seconds%n", 
            elapsedDays,
            elapsedHours, elapsedMinutes, elapsedSeconds);

    }

我想使用selecteddata1作为查找日期差异的起始日期。请解释清楚,以便我们可以帮助您:我无法在selecteddate1字符串中传递数据,以便在另一个函数中查找日期差异谢谢,但我正在从calander获取输入,并将字符串转换为YYMMDD格式,因此无法解算谢谢,但我当前仅使用一个活动,因此应用程序停止…我只想计算日期差给了我很多错误。Plz发送了告诉我从android日历中选择开始日期和结束日期作为当前日期的代码,并找到日期差