1

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;
    }
};
Sandeep
  • 455
  • 4
  • 26

3 Answers3

2

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();
Falke Design
  • 10,635
  • 3
  • 15
  • 30
  • I am getting following error `java.util.Date cannot be cast to android.app.DatePickerDialog$OnDateSetListener` at new DatePickerDialog line – Sandeep Oct 06 '19 at 20:39
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Oct 07 '19 at 10:48
  • @OleV.V.sorry, i'm with you. I didn't know that this is outdate. Thx for the info – Falke Design Oct 08 '19 at 08:03
0

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.

Phil
  • 76
  • 6
  • That's fine, but how can I set these day, month, year in the calendar dialog (DatePickerDialog) – Sandeep Oct 06 '19 at 04:13
0

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();
        }

    }
};
Sandeep
  • 455
  • 4
  • 26