How do I translate data from Calendar to Date?

For the test, I call the calendar to select the date

Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">

<TextView android:id="@+id/currentDateTime"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:textSize="18sp" />

<Button android:id="@+id/dateButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Изменить дату"
        android:onClick="setDate"  />

public class MainActivity extends AppCompatActivity {

TextView currentDateTime;
Calendar dateAndTime=Calendar.getInstance();
@Override
public void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);
    setContentView(R.layout.activity_main);
    currentDateTime=(TextView)findViewById(R.id.currentDateTime);
    setInitialDateTime();
}

// окно для выбора даты
public void setDate(View v) {
    new DatePickerDialog(MainActivity.this, d,
            dateAndTime.get(Calendar.YEAR),
            dateAndTime.get(Calendar.MONTH),
            dateAndTime.get(Calendar.DAY_OF_MONTH))
            .show();
}

// установка текущей даты для textview
private void setInitialDateTime()
{
    SimpleDateFormat sdf = new SimpleDateFormat("dd - MM - yyyy");
    String date = sdf.format(new Date().getTime());
    currentDateTime.setText(date);
}

//обработчик выбора даты
DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        dateAndTime.set(Calendar.YEAR, year);
        dateAndTime.set(Calendar.MONTH, monthOfYear);
        dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        setInitialDateTime();
    }
};
}

How do I convert this calendar data to Date format ? so that you can save the selected date in the SQL database Roughly speaking, I want to fill the textview with a date from the database, but to write the date to the database, it needs to be somehow converted from the calendar format

As a result of the responses received, part of the code turned into

    private void setInitialDateTime()
{
    SimpleDateFormat sdf = new SimpleDateFormat("dd - MM - yyyy");
    String date = sdf.format(dateAndTime.getTime());
    currentDateTime.setText(date);
}
Author: Howling, 2017-10-06

2 answers

To do this, there is a getTime() method for the Calendar object.

 1
Author: Eugene Krivenja, 2017-10-06 08:47:16

I will clarify the answers of my colleagues.

We must not forget to take into account that the getTime() method returns Date normalized to UTC/Greenwich Mean Time, while Calendar contains the current time zone (TimeZone), in other words, it means that the returned getTime() object Date loses information about the time zone.

Read more here

It's not good or bad - you just have to keep it in mind.

 1
Author: Barmaley, 2017-10-06 09:43:05