Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid DatePicker "0's" problem when using SQLite
    text
    copied!<p>I am working with android + SQLite and DatePicker Widget.</p> <p>The DatePicker does not format correctly for my SQLite insert commands. I.e, if the chosen date has a month or day less than 10, it does not insert the 0's. For example if I choose the date "1st January 2010", the format of the month and date is 1 and 1. This clashes with the usual SQL format of YYYY-MM-DD.</p> <p>I tried to concatenate 0's into the integers when they are less than 10 by casting them to strings and prefixing o's by doing the following:</p> <pre><code> // the callback received when the user "sets" the date in the dialog private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { if(monthOfYear &lt; 10) { String mm = Integer.toString(monthOfYear); String m = "0" + mm; mMonth = Integer.parseInt(m); } else{mMonth = monthOfYear;} if (dayOfMonth &lt;10) { String dd = Integer.toString(dayOfMonth); String d = "0" + dd; dayOfMonth = Integer.parseInt(d); } else{mDay = dayOfMonth;} mYear = year; updateDisplay(); } }; // updates the date in the TextView private void updateDisplay() { mDateDisplay.setText( new StringBuilder() // Month is 0 based so add 1 .append(mYear).append("-") .append(mMonth + 1).append("-") .append(mDay).append("") ); selecteddate = (String) mDateDisplay.getText(); } </code></pre> <p>I was expecting this to convert 2010-1-1 to 2010-01-01. It doesnt though. Does anyone have have a simpler way of doing this so that I can get the Date into the correct format before sending it to the sqlite table?</p> <p>Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload