Android Datetime picker example
In this post I’ll will be showing how to use android picker to pick date and time for android application. You can use this code to pick date and time and use the selected time to store in a database or set it to Android textview to show selected values.
Make a new android application or you can use your existing application as their’s only one activity and one layout file to make this picker work.
Make a new Layout file in your application with name datepicker.xml and edit it as below. This file is the design file and it contains two textview to show the date and time and two buttons to display the respective pickers.
datepicker.xml :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/txttime" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="52dp" android:textColor="#1c1c1c" android:textSize="25sp" android:textStyle="bold" /> <Button android:id="@+id/btntimepicker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffc107" android:layout_marginTop="5dp" android:text="Pick Time" android:textColor="#fff" android:textStyle="bold" /> <TextView android:id="@+id/txtdate" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:textColor="#1c1c1c" android:textSize="25sp" android:textStyle="bold" /> <Button android:id="@+id/btndatepicker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffc107" android:text="Pick Date" android:layout_marginTop="5dp" android:textColor="#fff" android:textStyle="bold" /> </LinearLayout>
Make a new java class in your project with name DateTimePicker.java and edit it as following. I’m converting selected date and time in different formats and show it on textviews respectively. If you only need the code to display the Date and time picker for your project, write this :
Code to show only Datepicker :
DatePickerDialog dd = new DatePickerDialog(DateTimePicker.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { } }, year, month, day); dd.show();
Code to show only Timepicker :
TimePickerDialog td = new TimePickerDialog(DateTimePicker.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { } }, hour, min, DateFormat.is24HourFormat(DateTimePicker.this) ); td.show();
DateTimePicker.java :
Here ‘s the full code of my activity which will set picked date and time on textviews.
package h.datetimepicker; import android.app.DatePickerDialog; import android.app.TimePickerDialog; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; 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.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import h.passwordvault.R; public class DateTimePicker extends AppCompatActivity { static int hour, min; TextView txtdate, txttime; Button btntimepicker, btndatepicker; java.sql.Time timeValue; SimpleDateFormat format; Calendar c; int year, month, day; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.datepicker); c = Calendar.getInstance(); hour = c.get(Calendar.HOUR_OF_DAY); min = c.get(Calendar.MINUTE); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); txtdate = (TextView) findViewById(R.id.txtdate); txttime = (TextView) findViewById(R.id.txttime); btndatepicker = (Button) findViewById(R.id.btndatepicker); btntimepicker = (Button) findViewById(R.id.btntimepicker); btndatepicker.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Get Current Date DatePickerDialog dd = new DatePickerDialog(DateTimePicker.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { try { SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); String dateInString = dayOfMonth + "/" + (monthOfYear + 1) + "/" + year; Date date = formatter.parse(dateInString); txtdate.setText(formatter.format(date).toString()); formatter = new SimpleDateFormat("dd/MMM/yyyy"); txtdate.setText(txtdate.getText().toString()+"\n"+formatter.format(date).toString()); formatter = new SimpleDateFormat("dd-MM-yyyy"); txtdate.setText(txtdate.getText().toString()+"\n"+formatter.format(date).toString()); formatter = new SimpleDateFormat("dd.MMM.yyyy"); txtdate.setText(txtdate.getText().toString()+"\n"+formatter.format(date).toString()); } catch (Exception ex) { } } }, year, month, day); dd.show(); } }); btntimepicker.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { TimePickerDialog td = new TimePickerDialog(DateTimePicker.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { try { String dtStart = String.valueOf(hourOfDay) + ":" + String.valueOf(minute); format = new SimpleDateFormat("HH:mm"); timeValue = new java.sql.Time(format.parse(dtStart).getTime()); txttime.setText(String.valueOf(timeValue)); String amPm = hourOfDay % 12 + ":" + minute + " " + ((hourOfDay >= 12) ? "PM" : "AM"); txttime.setText(amPm + "\n" + String.valueOf(timeValue)); } catch (Exception ex) { txttime.setText(ex.getMessage().toString()); } } }, hour, min, DateFormat.is24HourFormat(DateTimePicker.this) ); td.show(); } }); } }
AndroidManisfest.xml :
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="h.datetimepicker"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".DateTimePicker" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>