Friday, April 1, 2016

Difference Between Two Dates in Android - CheckIn & CheckOut

"Jai Saraswati Maa"
Hi..... Dear All we are trying to provide here some examples  of Android App Related....
Hope you would Like..........
This example will display the date difference
We are using Android Studio 1.5.1
Minimum API 19 and Target API is 23
Credit goes to Vipul Walia (Student)
 A- Basic Layout 



B - Layout file activity_main.xml is 
<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout 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:orientation="vertical"    
     tools:context="com.exam.ravi.datedifferenceex1.MainActivity">

    <Button        
       android:id="@+id/changeDate"  
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"   
       android:text="Check-In-Date"      
       android:layout_gravity="center_horizontal"  
       android:layout_marginLeft="15dp" 
       android:layout_marginRight="15dp"        
       android:layout_marginTop="15dp" />

    <TextView     
        android:id="@+id/Output" 
        android:layout_width="wrap_content"      
        android:layout_height="wrap_content"     
        android:text=""        
        android:hint="M-D-YYYY"        
        android:textAppearance="?android:attr/textAppearanceLarge"      
        android:layout_gravity="center_horizontal"     
        android:layout_marginTop="20dp" />
    <Button        
   android:id="@+id/changeDate1"        android:layout_width="match_parent"     
   android:layout_height="wrap_content"        android:text="Check-Out-Date"        
   android:layout_gravity="center_horizontal"        android:layout_marginTop="15dp"        
   android:layout_marginLeft="15dp"        android:layout_marginRight="15dp" />

    <TextView 
       android:id="@+id/Output2"        android:layout_width="wrap_content"       
       android:layout_height="wrap_content"        android:text=""       
       android:hint="M-D-YYYY"        
       android:textAppearance="?android:attr/textAppearanceLarge" 
       android:layout_gravity="center_horizontal"       
       android:layout_marginTop="20dp" />

    <Button    
    android:layout_width="fill_parent"       
    android:layout_height="wrap_content"        android:text="Difference"      
    android:id="@+id/button"        android:layout_marginTop="20dp"       
    android:onClick="showDiff"        android:layout_marginLeft="15dp"      
    android:layout_marginRight="15dp" />

    <TextView   
     android:id="@+id/output3"        android:layout_width="wrap_content"       
     android:layout_height="wrap_content"        
     android:textAppearance="?android:attr/textAppearanceLarge"       
     android:hint="Difference is"        android:layout_gravity="center_horizontal"      
     android:layout_marginTop="30dp" />

</LinearLayout>

C- MainActivity.java is

package com.exam.ravi.datedifferenceex1;
import android.app.DatePickerDialog;
import android.app.Dialog;
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.TextView;
import android.widget.Toast;

@SuppressWarnings("ALL")
public class MainActivity extends AppCompatActivity {

     TextView Output1;
     Button changeDate1;
     int year1,year2;
     int month1,month2;
     int day1,day2;
     TextView Output2;
     Button changeDate2;
     TextView Output3;
    static final int DATE_PICKER_ID1 = 111;
    static final int DATE_PICKER_ID2 = 222;
    @Override    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Output1 = (TextView) findViewById(R.id.Output);
        changeDate1 = (Button) findViewById(R.id.changeDate);
        Output2 = (TextView) findViewById(R.id.Output2);
        changeDate2 = (Button) findViewById(R.id.changeDate1);
        Output3 = (TextView) findViewById(R.id.output3);

        changeDate1.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                // On button click show datepicker dialog               
                    showDialog(DATE_PICKER_ID1);
            }
        });
        changeDate2.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                // On button click show datepicker dialog         
                    showDialog(DATE_PICKER_ID2);
            }
        });
    }
    @Override    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_PICKER_ID1:
                // open datepicker dialog.                
               // set date picker for year==0,month==0,day==0      
                 // add pickerListener listner to date picker      
               return new DatePickerDialog(this, pickerListener1, 0, 0, 0);
            case DATE_PICKER_ID2:
                // open datepicker dialog.              
                // set date picker for year==0,month==0,day==0      
                // add pickerListener listner to date picker     
            return new DatePickerDialog(this, pickerListener2, 0, 0, 0);
        }
        return null;
    }
        /*  BELOW IS THE FIRST DATEPICKEDIALOG LISTER FOR TAKING DIFFERET DATE    */
    private DatePickerDialog.OnDateSetListener pickerListener1 = new 
                                              DatePickerDialog.OnDateSetListener() 
   {

        // when dialog box is closed, below method will be called.      
  @Override      
          public void onDateSet(DatePicker view, int selectedYear,
                              int selectedMonth, int selectedDay) {

            year1  = selectedYear;
            month1 = selectedMonth;
            day1   = selectedDay;

            // Show selected date           
                  Output1.setText(new StringBuilder().append(month1 + 1)
                    .append("-").append(day1).append("-").append(year1)
                    .append(" "));

        }

    };

    /* BELOW IS THE SECOND DATEPICKEDIALOG LISTER FOR TAKING DIFFERET DATE     */  
  private DatePickerDialog.OnDateSetListener pickerListener2 = new
                                                 DatePickerDialog.OnDateSetListener() 
   {

        // when dialog box is closed, below method will be called.        
    @Override       
       public void onDateSet(DatePicker view, int selectedYear,
                              int selectedMonth, int selectedDay) {

            year2  = selectedYear;
            month2 = selectedMonth;
            day2   = selectedDay;

            // Show selected date          
                Output2.setText(new StringBuilder().append(month2 + 1)
                    .append("-").append(day2).append("-").append(year2)
                    .append(" "));
        }
    };
        // COUNT DAY DIFFERENCE    // WE ASSUME DEFAULT MONTH DAYS 30  
  public void showDiff(View view){
        int Days = 0;
        if(month2 == month1){
            if(day2>day1){
                Days = day2 - day1;
            }
        }
        else if(month2>month1){
            if(day2>day1){
                int m = month2-month1;
                Days = m*30 + (day2 - day1);
            }
        }
        else          
        Toast.makeText(this,"Invalid Out Date",Toast.LENGTH_LONG).show();
        Output3.setText(String.valueOf(Days) + " Days");
    }
}

No comments:

Post a Comment