Pick date and time and save in SharedPreferences

This post describes how to set date and time together using DatePickerDialog and TimePickerDialog and save the date and time set in SharedPreferences.

In activity_main.xml add a Button and a TextView.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="25dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PICK TIME"
        android:textSize="25dp"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

In MainActivity.java use following code.

package com.example.dialog;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    static SharedPreferences sp;
    static Calendar now = Calendar.getInstance();
    static String date_time = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button1 = findViewById(R.id.button);

        sp = getSharedPreferences("sp", MODE_PRIVATE);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyDatePickerFragment fragment1 = new MyDatePickerFragment();
                fragment1.show(getSupportFragmentManager(), "dp");
            }
        });
    }

    public static class MyDatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            int y, m, d = 0;
            if (sp.getString("year", ""). equals("")){
                y = now.get(Calendar.YEAR);
            } else {
                y = (int)Double.parseDouble(sp.getString("year", ""));
            }
            if (sp.getString("month", ""). equals("")){
                m = now.get(Calendar.MONTH);
            } else {
                m = (int)Double.parseDouble(sp.getString("month", ""));
            }
            if (sp.getString("day", ""). equals("")){
                d = now.get(Calendar.DAY_OF_MONTH);
            } else {
                d = (int)Double.parseDouble(sp.getString("day", ""));
            }

            return new DatePickerDialog(getActivity(), this, y, m, d);
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {
            int mon = month +1;
            date_time = day + "/" + mon + "/" + year;

            sp.edit().putString("year", String.valueOf(year)).commit();
            sp.edit().putString("month", String.valueOf(month)).commit();
            sp.edit().putString("day", String.valueOf(day)).commit();

            TimePickerFragment fragment2 = new TimePickerFragment();
            fragment2.show(getActivity().getSupportFragmentManager(), "tp");
        }
    }

    public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            int h, min = 0;
            now = Calendar.getInstance();
            if (sp.getString("hour", ""). equals("")){
                h = now.get(Calendar.HOUR_OF_DAY);
            } else {
                h = (int)Double.parseDouble(sp.getString("hour", ""));
            }
            if (sp.getString("min", ""). equals("")){
                min = now.get(Calendar.MINUTE);
            } else {
                min = (int)Double.parseDouble(sp.getString("min", ""));
            }

            return new TimePickerDialog(getActivity(), this, h, min, DateFormat.is24HourFormat(getActivity()));
        }

        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            sp.edit().putString("hour", String.valueOf(hourOfDay)).commit();
            sp.edit().putString("min", String.valueOf(minute)).commit();

            date_time = date_time + ", " + hourOfDay + ":" + minute;

            TextView textview1 = getActivity().findViewById(R.id.textView);
            textview1.setText(date_time);
        }
    }

}

Now run the app. It will show DatePickerDialog when button is clicked. It will show TimePickerDialog when date is set. And when time is set, it will display the date and time set in TextView.