sneaky
sneaky

Reputation: 2201

How to format mySQL date to show in datepicker in android?

When I get a date from mySQL, its in the format 2013-06-05, but when I try to display it in a datepicker dialog, I am getting the year 1933 and the month and day is also not right...

The variable Setdate would be the string "2013-06-05".

        public void onClick(View v) {
            // TODO Auto-generated method stub
            String Setdate = dateButton.getText().toString();
            SimpleDateFormat sfd = new SimpleDateFormat(Setdate);
            Calendar myCalendar = sfd.getCalendar();
            new DatePickerDialog(context, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show();
        }

Does anyone know how to fix this?

Thanks

Upvotes: 1

Views: 2916

Answers (3)

To convert String to date you can use SimpleDateFormat's parse function.

String myFormat = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
Date dateObj = sdf.parse(dateStr);
Calendar myCal = Calendar.getInstance();
myCal.setTime(dateObj);

To store the date in a DatePicker object you need to extract the month, day, and year using the Calendar.get() function. Then use DatePicker.init() to set the date and create an OnDateChangedListener to know when the user has changed the date.

int month = myCal.get(Calendar.MONTH);
int day = myCal.get(Calendar.DATE);
int year = myCal.get(Calendar.YEAR);
mDp.init(year, month, day, new OnDateChangedListener() {
      public void onDateChanged(DatePicker view, int year,
         int monthOfYear, int dayOfMonth) {
          // To-Do (what ever I need to do when the date is changed).
         }
         });

to get the date back out of the DatePicker object.

Date myDate = new Date(mDp.getYear() - 1900, mDp.getMonth(),
       mDp.getDayOfMonth());

convert it back into an ISO formatted date or formatted for the correct locale.

String myFormat = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
String dateAddedISO = sdf.format(myDate);
String dateAdded = DateFormat.getDateInstance().format(myDate);

Upvotes: 1

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

You can try something like this

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    Date date=df.parse("2013-06-05");
    String text = df.format(date);
    System.out.println("The date is: " + text);

Upvotes: 0

Karakuri
Karakuri

Reputation: 38595

The constructor for SimpleDateFormat takes in a pattern string, not the actual date string. The pattern string describes the format it should use when parsing date strings or converting a Date to a string representation.

Try this instead:

String format = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(format);
String dateString = dateButton.getText().toString();
Date date = sdf.parse(dateString);
Calendar myCalendar = Calendar.getInstance();
myCalendar.setTime(date);
new DatePickerDialog(context, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show();

Another thought: If you are in control of how the dates are stored, perhaps consider storing them as longs representing milliseconds since Epoch, which is what Date and Calendar classes use internally anyway. It can often make for easier code, for example:

long time = ...
Calendar myCalendar = Calendar.newInstance();
myCalendar.setTimeInMillis(time);
new DatePickerDialog(context, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show();

Upvotes: 2

Related Questions