Reputation: 89
I built a date picker that will let the user chose the date and return it in such ("dd-MM-yyy");. however, the date picker starts with 1990 year, I’m trying to change the default start date to the current day.
private void SetDatePicker() {
DatePickerDialog.OnDateSetListener listner = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
currentDate = new SimpleDateFormat("dd/MM/yyyy");
currentday = new SimpleDateFormat("dd");
currentMonth = new SimpleDateFormat("mm");
currentYear = new SimpleDateFormat("yy");
todayDate = new Date();
String thisDate = currentDate.format(todayDate);
Log.w ("Date", thisDate);
DateStore.set (Calendar.DAY_OF_MONTH, Integer.parseInt (currentday.format(todayDate)));
DateStore.set (Calendar.MONTH, Integer.parseInt (currentMonth.format(todayDate)));
DateStore.set (Calendar.YEAR, Integer.parseInt (currentYear.format(todayDate)));
DayofMonth = DateStore.get (Calendar.DAY_OF_MONTH);
MonthOfMonth = DateStore.get (Calendar.MONTH) + 1;
mYear = DateStore.get (Calendar.YEAR);
String DateString = String.valueOf (dayOfMonth) + "-" + String.valueOf (MonthOfMonth) + "-" + String.valueOf (year);
Log.w ("Date Test" , DateString);
}
};
DatePickerDialog DatePickerDioalg = new DatePickerDialog (getActivity() ,0,listner ,Integer.parseInt (currentday.format(todayDate)),
Integer.parseInt (currentMonth.format(todayDate)),Integer.parseInt (currentYear.format(todayDate)));
DatePickerDioalg.show();
}
Here is the XML
<EditText
android:id="@+id/edittext_date"
android:onclick="pickdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.8"
android:background="#FFFF"
android:gravity="center"
android:hint="10-09-2017"
android:textSize="16sp"/>
Upvotes: 0
Views: 9685
Reputation: 875
try this method for date picker
public void pickdate(View button) {
// Get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
edittext_date.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
and this for time picker
public void picktime(View button) {
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
edittext_time.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
timePickerDialog.show();
}
Upvotes: 6
Reputation: 1897
You should try this one for getting current date:
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
String todate= dateFormat.format(currentdate());
String finalto = todate.toString(); //here you get current date
private Date currentdate() {
final Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 0);
return cal.getTime();
}
Upvotes: 0