Java 如何将datePickerDialogue设置为当前日期?
我希望用户从日期选择器对话框中选择日期,并使用所选日期。 我已经做了一个日期选择器对话,唯一的问题是当对话打开日期显示1990年1月1日,然后用户必须来到今年。我想打开今天日期的对话框。 下面是我的代码-Java 如何将datePickerDialogue设置为当前日期?,java,android,datepickerdialog,Java,Android,Datepickerdialog,我希望用户从日期选择器对话框中选择日期,并使用所选日期。 我已经做了一个日期选择器对话,唯一的问题是当对话打开日期显示1990年1月1日,然后用户必须来到今年。我想打开今天日期的对话框。 下面是我的代码- public class MyClass extends AppCompatActivity { Calendar cal; Button selectDate; DatePickerDialog datePicker; @Override pr
public class MyClass extends AppCompatActivity {
Calendar cal;
Button selectDate;
DatePickerDialog datePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trains_bw_stations);
selectDate=findViewById(R.id.select_date);
selectDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cal=Calendar.getInstance();
int day=cal.get(Calendar.DAY_OF_MONTH);
int month=cal.get(Calendar.MONTH);
int year=cal.get(Calendar.YEAR);
datePicker=new DatePickerDialog(TrainsBwStations.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int mYear, int mMonth, int mDayOfMonth) {
Log.d("Rail","Year-"+mYear);
Log.d("Rail","Month-"+mMonth);
Log.d("Rail","Day-"+mDayOfMonth);
}
},day,month,year);
datePicker.show();
}
});
}
}
试试这个:
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into datepicker
datePicker.init(year, month, day, null);
请用这个
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String date = df.format(c.getTime());
datePickerDialog.updateDate(Integer.parseInt(date.split("/")[2]),Integer.parseInt(date.split("/")[1]),Integer.parseInt(date.split("/")[0]));
datePickerDialog.setTitle("");
datePickerDialog.show();
试试下面的代码
public class MyClass extends AppCompatActivity {
Calendar cal;
Button selectDate;
DatePickerDialog datePicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trains_bw_stations);
selectDate=findViewById(R.id.select_date);
selectDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cal = Calendar.getInstance();
final DatePickerDialog.OnDateSetListener dateA = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, monthOfYear);
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
}
};
new DatePickerDialog(MyClass.this, dateA, cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
}
在这里,您可以获取字符串格式的日期,以后您可以根据需要进行转换
//Defining it in Class Activity before onCreate.. or you can define in onCreate..
private Calendar calendar;
private int year, month, day;
String formattedDate, toDate, fromDate, pickUpdate;
//Inside onCreate...
calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
//on button click or any view click.. paste the below code..
toTV = (TextView) findViewById(R.id.toTV);
toTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//have a look at **to** after YourActivity.this
DatePickerDialog datePickerDialog = new DatePickerDialog(YourActivity.this, to, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
//This is used to set the date to select current and future date not the past dates..uncomment if you want to use it...
//datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
datePickerDialog.show();
}
});
//outside oncreate which is a calender pop-up...
//Here **to** is object which called in onClick of dateBT
DatePickerDialog.OnDateSetListener to = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
String day = dayOfMonth + "";
if (dayOfMonth < 9) {
day = "0" + dayOfMonth;
}
String monthString = (String.valueOf(month + 1));
if (month < 9) {
monthString = "0" + monthString;
}
formattedDate = String.valueOf(year) + "/" + monthString
+ "/" + day;
dateTV.setText(formattedDate);
date = formattedDate;
}
};
谢谢Omi,成功了。你能解释一下这里发生了什么吗?欢迎!我认为您必须在@Override方法public void onDateSet中设置当前日期,就像我在cal.setCalendar.YEAR中所做的那样;请说明为什么这是一个解决方案。只有代码的答案帮助不大。
//Defining it in Class Activity before onCreate.. or you can define in onCreate..
private Calendar calendar;
private int year, month, day;
String formattedDate, toDate, fromDate, pickUpdate;
//Inside onCreate...
calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
//on button click or any view click.. paste the below code..
toTV = (TextView) findViewById(R.id.toTV);
toTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//have a look at **to** after YourActivity.this
DatePickerDialog datePickerDialog = new DatePickerDialog(YourActivity.this, to, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
//This is used to set the date to select current and future date not the past dates..uncomment if you want to use it...
//datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
datePickerDialog.show();
}
});
//outside oncreate which is a calender pop-up...
//Here **to** is object which called in onClick of dateBT
DatePickerDialog.OnDateSetListener to = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
String day = dayOfMonth + "";
if (dayOfMonth < 9) {
day = "0" + dayOfMonth;
}
String monthString = (String.valueOf(month + 1));
if (month < 9) {
monthString = "0" + monthString;
}
formattedDate = String.valueOf(year) + "/" + monthString
+ "/" + day;
dateTV.setText(formattedDate);
date = formattedDate;
}
};