I have a textView and I want to get date from textView to datePicker. The date pattern in textView is dd/mm/yy.
You can use any date and time picker library. Using https://github.com/wdullaer/MaterialDateTimePicker library you can achieve.
Updated:
String date = tv.getText().toString(); // get text from the textview
String[] sep = date.split("/"); // split the text by '/'
int date = Integer.parseInt(sep[0]); // this will contain date
int month = Integer.parseInt(sep[1]); // this will contain month
int year = Integer.parseInt(sep[2]); // this will contain year
int year, month, day;
int yearr, motTh, dayofmonth;
String newDate="";
ImageView img_startDate;//initialize
year = now.get(Calendar.YEAR);
month = now.get(Calendar.MONTH);
day = now.get(Calendar.DAY_OF_MONTH);
TextView tv_startDate;//initialize
img_startDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerDialog datePickerDialog = new DatePickerDialog(ReportsActivity.this,
dateListener, year, month, day);
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
datePickerDialog.show();
}
DatePickerDialog.OnDateSetListener dateListener =
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {
now.set(year, month, dayOfMonth);
yearr = datePicker.getYear();
motTh = datePicker.getMonth();
dayofmonth = datePicker.getDayOfMonth();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
newDate = simpleDateFormat.format(now.getTime());//newDate is string
tv_startDate.setText(String.valueOf(newDate));
}
};
});
tv_startDate.getText().toString()// get value from text view to datepicker
Try this.
void setDateToPicker(String d)
{
Calendar calendar = Calendar.getInstance(); //use Calender instance for getting the text date.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy");
try
{
Date date = dateFormat.parse(d); //parse your string date into a date object
calendar.setTime(date); //set your time into the Calender object
} catch (ParseException e)
{
e.printStackTrace();
}
DatePickerDialog picker = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
//call back when a date is selected by user
}
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)); //set the day, month and year from the calender object which have the date set.
picker.show();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.