Thursday 14 September 2017

Different Type of Date Format in Android | Show Date Picker in EditBox

About Example:- Here is the basic example that "How to Show different Types of Date Format  in Android Application By Coding"

Basic Requirement:-
1. MainActivity.java  | activity_main.xml
Steps are:-

Step 1:Add a new project and open build.gradle and add one line inside dependencies:-

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        compile 'com.android.support:design:26.+'    }) 
}

Step 2:  Open activity_main.xml and write following code:-


<?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:id="@+id/layout"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="com.example.supriyabharti.calendarexample.MainActivity">
    <TextView
        android:id="@+id/tv_currentDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="14 October 2017" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/dFormat2"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/dFormat3"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/dFormat4"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/dFormat5"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editName"
        android:layout_margin="30dp"
        android:singleLine="true"
        android:hint="Name"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edit_date"
        android:layout_margin="30dp"
        android:singleLine="true"
        android:clickable="true"
        android:editable="false"
        android:hint="Birthday"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnSubmit"
        android:text="Submit"
        android:layout_margin="30dp"/>
</LinearLayout>

Step 3:Open MainActivity.java and write following code:-

package com.example.supriyabharti.calendarexample;

import java.text.SimpleDateFormat;
import android.app.DatePickerDialog;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    EditText edtDate,edtName;
    TextView tvCurrentdate,dFormat2,dFormat3,dFormat4,dFormat5;
    Button btnSubmit;
     Calendar c;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edtName=(EditText)findViewById(R.id.editName);
        edtDate=(EditText)findViewById(R.id.edit_date);
        tvCurrentdate=(TextView)findViewById(R.id.tv_currentDate);
        dFormat2=(TextView)findViewById(R.id.dFormat2);
        dFormat3=(TextView)findViewById(R.id.dFormat3);
        dFormat4=(TextView)findViewById(R.id.dFormat4);
        dFormat5=(TextView)findViewById(R.id.dFormat5);
        btnSubmit=(Button)findViewById(R.id.btnSubmit);
         c=Calendar.getInstance();
        SimpleDateFormat sf1=new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); //14-09-2017       
        String dateFormat1= sf1.format(c.getTime());
        tvCurrentdate.setText(dateFormat1);

        SimpleDateFormat sf2=new SimpleDateFormat("dd MMMM yyyy"); //14 September 2017        
        String dateFormat2= sf2.format(c.getTime());
        dFormat2.setText(dateFormat2);

        SimpleDateFormat sf3=new SimpleDateFormat("dd.MM.yyyy");  //14.09.2017        
        String dateFormat3=sf3.format(c.getTime());
        dFormat3.setText(dateFormat3);

        SimpleDateFormat sf4=new SimpleDateFormat("dd/MM/yyyy");  //14/09/2017        
        String dateFormat4=sf4.format(c.getTime());
        dFormat4.setText(dateFormat4);

        SimpleDateFormat sf5=new SimpleDateFormat("dd MMM yyyy"); //14 sept 2017        
        String dateFormat5=sf5.format(c.getTime());
        dFormat5.setText(dateFormat5);
        tvCurrentdate.setText(dateFormat1);
        final DatePickerDialog.OnDateSetListener  datePickerDialog=new DatePickerDialog.OnDateSetListener() {
            @Override           
            public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                c.set(Calendar.DAY_OF_MONTH,i2);
                c.set(Calendar.MONTH,i1);
                c.set(Calendar.YEAR,i);
                updateLavel();

            }
        };
        edtDate.setOnClickListener(new View.OnClickListener() {
            @Override            
            public void onClick(View view) {
            new DatePickerDialog(MainActivity.this,datePickerDialog,c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)).show();

            }
        });
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override           
             public void onClick(View view) {
                if(edtName.getText().toString().equalsIgnoreCase("") || edtDate.getText().toString().equalsIgnoreCase("")){
                    Snackbar snackbar=Snackbar.make(view,"Please fill All Field.!!",Snackbar.LENGTH_LONG);
                    snackbar.show();
                }else {
                    Snackbar snackbar=Snackbar.make(view,"Submit Successfully",Snackbar.LENGTH_LONG);
                    snackbar.show();
                    edtName.setText("");
                    edtDate.setText("");
                }
            }
        });


    }
    //method...    public void updateLavel(){
        SimpleDateFormat dateFormat1=new SimpleDateFormat("dd MMM yyyy");
        edtDate.setText(dateFormat1.format(c.getTime()));
    }

}

Step 4:Output are:-




1 comment:

  1. Itne bde page ko koi ni pdhta

    chote-2 page banao taki kisi ko boaring feel na ho

    or 1 page ko dusre se link kr k rakkho

    ReplyDelete