안드로이드

[android] DatePicker

여미미 2021. 1. 6. 11:17

layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <DatePicker
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/datePicker"
        android:datePickerMode="spinner"
        android:calendarViewShown="false"/>

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


</LinearLayout>

 

 

activity

public class MainActivity extends AppCompatActivity {
    private SimpleDateFormat mFormat = new SimpleDateFormat("yyyy.M.d"); // 날짜 포맷
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DatePicker datePicker = findViewById(R.id.datePicker);
        TextView dateTx = findViewById(R.id.date);
        Date date = new Date();
        String time = mFormat.format(date);
        dateTx.setText(time);
    }
}