androidandroid-date

Parse the String to Date and format


In the fragment I have a DatePickerDialog.OnDateSetListener. If I choose a Date, I send it to the presenter in the form of a String. Then I want to parse String to Date in Presenter and before save in the database change the format of Date.

SavePresenter:

 @Override
public void addTaskClick(String text, String date, String time, boolean status) {
    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yy");
    Date updateDate = null;
    try {
        updateDate = format.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Log.e("Save Presenter", updateDate.);
    realmService.addTask(text, updateDate, time, status, this);
}

This is what I have now: E/Save Presenter: Tue Dec 17 00:00:00 GMT+01:00 2019 I want to have this: 17 12 2019 Or something similar. I need it to compare saved dates with current date to sort tasks by date. I also tried the DateFormat but the result was the same.


Solution

  • SimpleDateFormat("dd/MM/yy"); won't do the trick try instead SimpleDateFormat("yyyy-MM-dd'T' HH:mm:ss.SSSZ");

    enter image description here

    you need to specify the date on the required formats

    Taken from SimpleDateFormat