简体   繁体   中英

Set date in DatePickerDialog from String

How can I set date in a DatePicker from a string (eg: 02/10/19):

Following is the code:

  iqp_editDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new DatePickerDialog(ActivityClass.this, (DatePickerDialog.OnDateSetListener) date1, y, m, d).show();
        }
    });

 DatePickerDialog.OnDateSetListener date1 = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {      
        y = year;
        m = month;
        d = dayOfMonth;
        dateMonth = month + 1;
        dateYear = year;
    }
};

Update: misunderstood the question:

Calendar cal = Calendar.getInstance()
cal.set(Calendar.HOUR_OF_DAY,18);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.DATE,2);
cal.set(Calendar.MONTH,9); //Month -1

//Or to set it from a String:
String string = "02/10/19";
DateFormat format = new SimpleDateFormat("dd/MM/yy", Locale.getDefault());
Date date = format.parse(string);
cal.setTimeInMillis(date.getTime());

new DatePickerDialog(getContext(),date1, cal 
                                    .get(Calendar.YEAR), cal .get(Calendar.MONTH),
                                    cal .get(Calendar.DAY_OF_MONTH)).show();

A string is a character array. Meaning that you could just make a loop and set the different day, month and year values to different parts of the string.

String date = "02/19/19";
String year = "";
for(int i = 0; i < date.length; i++)
{
  month += date.charAt(i);
  ...
}

Then you'd tell it when to switch from adding to month to day to year when it encounters '/'

if(date.charAt(i) == '/')
{
  ...
}

at the end of it all if you need to make it into an int then do

int month = Integer.parseInt("month");

sorry I've gotta be going somewhere so I couldn't just write the code out for ya but I'm sure you can figure it out from what I gave ya.

Following code works fine for me:

new DatePickerDialog(Activityclass.this, date1, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)).show();

DatePickerDialog.OnDateSetListener date1 = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        view.setMinDate(1559327400);
        y = year;
        m = month;
        d = dayOfMonth;
        dateMonth = month + 1;
        dateYear = year;
        iqp_editDate.setText(dayOfMonth + "/" + (month + 1) + "/" + year);
        try {
            epoch = new java.text.SimpleDateFormat("MM/dd/yyyy").parse(m + "/" + dayOfMonth + "/" + year).getTime() / 1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
};

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM