How to create DatePickerDialog with multiple different type of date formats in US and UK android.
In this tutorial we are creating a simple DatePickerDialog but at the date getting time we have modify the date formats into multiple formats including month name show. So here is the complete step by step tutorial for Change DatePickerDialog selected date format in android.
How to Change DatePickerDialog selected date format in android.
Code for MainActivity.java file.
package com.datepickerdialogformat_android_examples.com; import android.app.AlertDialog; import android.app.DatePickerDialog; import android.app.Dialog; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.app.DialogFragment; import android.widget.TextView; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.text.DateFormat; public class MainActivity extends Activity { Button button; TextView textview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub DialogFragment dialogfragment = new datepickerClass(); dialogfragment.show(getFragmentManager(), "DatePickerDialog"); } }); } public class datepickerClass extends DialogFragment implements DatePickerDialog.OnDateSetListener{ @Override public Dialog onCreateDialog(Bundle savedInstanceState){ final Calendar calendar = Calendar.getInstance(); int day = calendar.get(Calendar.DAY_OF_MONTH); int month = calendar.get(Calendar.MONTH); int year = calendar.get(Calendar.YEAR); DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,this,year,month,day); return datepickerdialog; } @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub TextView textview = (TextView) getActivity().findViewById(R.id.textView1); Calendar calander2 = Calendar.getInstance(); calander2.setTimeInMillis(0); calander2.set(year, monthOfYear, dayOfMonth, 0, 0, 0); Date SelectedDate = calander2.getTime(); DateFormat dateformat_US = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US); String StringDateformat_US = dateformat_US.format(SelectedDate); textview.setText(StringDateformat_US + "\n"); DateFormat dateformat_UK = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.UK); String StringDateformat_UK = dateformat_UK.format(SelectedDate); textview.setText( textview.getText() + StringDateformat_UK + "\n"); } } }
Code for activity_main.xml layout file.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.datepickerdialogformat_android_examples.com.MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Click Here to open DatePickerDialog" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button1" android:layout_centerHorizontal="true" android:layout_marginBottom="55dp" android:text="Selected Date Show Here" android:textAppearance="?android:attr/textAppearanceLarge" /> </RelativeLayout>
Screenshot: