textViewの右矢印の日付を押していると、datePickerのダイアログにその日付を設定する必要があります。リフレッシュを押すと現在の日付に設定されますが、私は左矢印と右矢印を押します。私はそれを達成するために何をすべきですか?Datepickerの現在の日付が日付ピッカーのダイアログで強調表示される
iv_refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count = 0;
getDate(count);
setDateTimeField();
}
});
rightarrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDate(1);
}
});
leftarraow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDate(-1);
}
});
tv_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
icount=0;
DOBPickerDialog.show();
}
});
public long 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);
return elapsedDays;
}
private void getDate(int i) {
count = count + i;
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, count);
currenttoday = dateFormat.format((calendar.getTime()));
// currenttoday = currenttoday.replace("-","/");
tv_date.setText(currenttoday);
// Toast.makeText(LandingActivity.this, currenttoday, Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setAction("DateActivity");
intent.putExtra("date", currenttoday);
sendBroadcast(intent);
}
/**
* setDatetimefield method is used to set the selected date
* from the datepicker to the text view
*/
private void setDateTimeField() {
java.util.Calendar newCalendar = java.util.Calendar.getInstance();
DOBPickerDialog = new DatePickerDialog(LandingActivity.this,R.style.pickertheme, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
String selectDate = dateFormatter.format(newDate.getTime());
tv_date.setText(selectDate);
try {
Date date1 = dateFormatter.parse(currenttoday);
String newDat = dateFormat.format(newDate.getTime());
Date date2 = dateFormatter.parse(newDat);
long diffDays = printDifference(date1, date2);
icount = (int) diffDays;
getDate(icount);
} catch (ParseException e) {
e.printStackTrace();
}
}
}, newCalendar.get(java.util.Calendar.YEAR), newCalendar.get(java.util.Calendar.MONTH), newCalendar.get(java.util.Calendar.DAY_OF_MONTH));
}