내 응용 프로그램에서 날짜 선택기를 사용하여 날짜를 설정하고 싶습니다. 날짜 선택기 최대 날짜를 시스템 날짜에 따라 오늘 날짜로 설정하고 싶습니다. 날짜 선택기 최대 날짜를 오늘 날짜로 설정하는 방법을 모르겠습니다. 누구나 알 수 있습니까? 이 문제를 해결하기 위해.
내 날짜 선택기 코딩은 다음과 같습니다.
private int pYear;
private int pMonth;
private int pDay;
static final int DATE_DIALOG_ID = 0;
final Calendar c = Calendar.getInstance();
pYear = c.get(Calendar.YEAR);
pMonth = c.get(Calendar.MONTH);
pDay = c.get(Calendar.DAY_OF_MONTH);
// Date picker
public Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
DatePickerDialog.OnDateSetListener pDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
pYear = year;
pMonth = monthOfYear;
pDay = dayOfMonth;
e_dob.setText(new StringBuilder()
// to set date in editext
.append(pDay).append("/").append(pMonth + 1)
.append("/").append(pYear).append(" "));
}
};
return new DatePickerDialog(this, pDateSetListener, pYear, pMonth,
pDay);
}
return null;
}
답변
사용 setMaxDate()
.
예를 들어 return new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay)
다음과 같이 문을 교체 합니다.
DatePickerDialog dialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
dialog.getDatePicker().setMaxDate(new Date().getTime());
return dialog;
답변
오늘 날짜 (& 시간)를 가져 와서 최대 날짜로 적용하십시오.
Calendar c = Calendar.getInstance();
c.set(2017, 0, 1);//Year,Mounth -1,Day
your_date_picker.setMaxDate(c.getTimeInMillis());
또한 우리는 이것을 할 수 있습니다 (System.currentTimeMillis () 대 Calendar 메서드에 대한 이 Stackoverflow 답변 확인 )
long now = System.currentTimeMillis() - 1000;
dp_time.setMinDate(now);
dp_time.setMaxDate(now+(1000*60*60*24*7)); //After 7 Days from Now
답변
이 줄을 바꿀 수 있습니다.
return new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
그들에 의해 :
DatePickerDialog dpDialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
DatePicker datePicker = dpDialog.getDatePicker();
Calendar calendar = Calendar.getInstance();//get the current day
datePicker.setMaxDate(calendar.getTimeInMillis());//set the current day as the max date
return dpDialog;
답변
이 시도
너무 많은 솔루션을 시도했지만 둘 다 작동하지 않았습니다. 반나절을 낭비한 후 마침내 솔루션을 만들었습니다.
이 코드는 최소 및 최대 날짜, 월 및 연도가있는 DatePickerDialog를 표시합니다.
final Calendar calendar = Calendar.getInstance();
DatePickerDialog dialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker arg0, int year, int month, int day_of_month) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, (month+1));
calendar.set(Calendar.DAY_OF_MONTH, day_of_month);
String myFormat = "dd/MM/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.getDefault());
your_edittext.setText(sdf.format(calendar.getTime()));
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
dialog.getDatePicker().setMinDate(calendar.getTimeInMillis());// TODO: used to hide previous date,month and year
calendar.add(Calendar.YEAR, 0);
dialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());// TODO: used to hide future date,month and year
dialog.show();
출력 :-
이전 및 미래 달력 비활성화
답변
private int pYear;
private int pMonth;
private int pDay;
static final int DATE_DIALOG_ID = 0;
/**inside oncreate */
final Calendar c = Calendar.getInstance();
pYear= c.get(Calendar.YEAR);
pMonth = c.get(Calendar.MONTH);
pDay = c.get(Calendar.DAY_OF_MONTH);
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
pYear, pMonth-1, pDay);
}
return null;
}
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(pYear, pMonth-1, pDay);
break;
}
}
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// write your code here to get the selected Date
}
};
작동해야합니다. 감사
답변
이 코드를 메인 메소드에서 직접 사용하면 도움이되기를 바랍니다.
@RequiresApi(api = Build.VERSION_CODES.N)
public DatePickerDialog datePickerDialog_db()
{
DatePickerDialog pickerDialog = new DatePickerDialog(getApplicationContext(),your view, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
pickerDialog.getDatePicker().setMaxDate(Calendar.getInstance().getTimeInMillis());
pickerDialog.show();
return null;
}
답변
내 맞춤 datepickerdialog를 확인하십시오.
https://github.com/Redman1037/EightFoldsDatePickerDialog
import android.app.DatePickerDialog;
import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/**
* Created by Manohar on 14/08/2017.
*/
public class EightFoldsDatePickerDialog extends DatePickerDialog {
private DatePickerDialog datePickerDialog;
private SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
@RequiresApi(api = Build.VERSION_CODES.N)
public EightFoldsDatePickerDialog(@NonNull Context context) {
super(context);
datePickerDialog = new DatePickerDialog(context);
}
@RequiresApi(api = Build.VERSION_CODES.N)
public EightFoldsDatePickerDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
datePickerDialog = new DatePickerDialog(context, themeResId);
}
public EightFoldsDatePickerDialog(@NonNull Context context, @Nullable OnDateSetListener listener, int year, int month, int dayOfMonth) {
super(context, listener, year, month, dayOfMonth);
datePickerDialog = new DatePickerDialog(context, listener, year, month, dayOfMonth);
}
public EightFoldsDatePickerDialog(@NonNull Context context, int themeResId, @Nullable OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth) {
super(context, themeResId, listener, year, monthOfYear, dayOfMonth);
datePickerDialog = new DatePickerDialog(context, themeResId, listener, year, monthOfYear, dayOfMonth);
}
public void show() {
datePickerDialog.show();
}
public void setMinDate(int year, int month, int day) {
String minDate = "" + day + "-" + getmonth(month) + "-" + year;
try {
datePickerDialog.getDatePicker().setMinDate(dateFormat.parse(minDate).getTime());
} catch (ParseException e) {
e.printStackTrace();
}
}
public void setMaxDate(int year, int month, int day) {
String maxDate = "" + day + "-" + getmonth(month) + "-" + year;
try {
datePickerDialog.getDatePicker().setMaxDate(dateFormat.parse(maxDate).getTime());
} catch (ParseException e) {
e.printStackTrace();
}
}
public void setTodayAsMaxDate() {
datePickerDialog.getDatePicker().setMaxDate(Calendar.getInstance().getTime().getTime());
}
public void setTodayAsMinDate() {
datePickerDialog.getDatePicker().setMinDate(Calendar.getInstance().getTime().getTime());
}
private String getmonth(int month) {
switch (month) {
case 1:
return "January";
case 2:
return "February";
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
case 6:
return "June";
case 7:
return "July";
case 8:
return "August";
case 9:
return "September";
case 10:
return "October";
case 11:
return "November";
case 12:
return "December";
}
return "January";
}
}
용법
EightFoldsDatePickerDialog.java를 프로젝트에 복사하십시오.
EightFoldsDatePickerDialog datePickerDialog=new EightFoldsDatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
//Do something with date
Toast.makeText(this, "Year "+year+" month "+month+" day "+dayOfMonth, Toast.LENGTH_SHORT).show();
}
},year,month,day); // give any year , month , day values, this will be opened by default in dialog
datePickerDialog.setMinDate(2017,8,7); //arguments are year , month , date (use for setting custom min date)
datePickerDialog.setMaxDate(2017,8,25); //arguments are year , month , date (use for setting custom max date)
// datePickerDialog.setTodayAsMinDate(); // sets today's date as min date
// datePickerDialog.setTodayAsMaxDate(); // sets today's date as max date
datePickerDialog.show();