How to calculate total number days difference between to dates in Android - java, Android

I am looking for a suitable solution to calculate number of days given two given dates: I am working a Andriod app. After go through stackoverflow.com pages I found julien solution very effective :
https://stackoverflow.com/questions/20165564/calculating-days-between-two-dates-with-in-java
Most for other links are just measuring dates different within a month. when to count days within couple of month those are not working attest don't meet my demand. So her i found org.joda.time library for   Java. than looking for what available for Android & get this 'joda-time:joda-time:2.9.9' So, I add this to app gradle Sync . there the code wich is implemented in an editText field for comparing it exceed certain date or not .



package android.exapmple.com.mydateset;

import android.app.DatePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class MainActivity extends AppCompatActivity implements View.OnFocusChangeListener{
    EditText lmpEditDate;
    Calendar calendar;
    int day, month, year;
    private SimpleDateFormat sdf;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        calendar = Calendar.getInstance(Locale.getDefault());
        day = calendar.get(Calendar.DAY_OF_MONTH);
        month = calendar.get(Calendar.MONTH);
        year = calendar.get(Calendar.YEAR);

        lmpEditDate = (EditText) findViewById(R.id.setdate);
        lmpEditDate.setOnFocusChangeListener(this);

    }

    @Override    public void onFocusChange(View v, boolean hasFocus) {
       // Toast.makeText(this, "Hello : "+ day + "/"+ month + "/" + year , Toast.LENGTH_SHORT).show();
        if(hasFocus== true) {
            DatePickerDialog datePickerDialog = new DatePickerDialog(this, dateListener, year, month, day);
            datePickerDialog.show();
        }

    }

    private int dateDifference(String PrviousDate) {
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        Calendar cal = Calendar.getInstance();
        String timeStamp = df.format(cal.getTime());

        DateTimeFormatter dateStringFormat = DateTimeFormat
                .forPattern("dd/MM/yyyy");
        DateTime firstTime = dateStringFormat.parseDateTime(PrviousDate);
        DateTime secondTime = dateStringFormat.parseDateTime(timeStamp);
        int days = Days.daysBetween(new LocalDate(firstTime),
                new LocalDate(secondTime)).getDays();

        //Toast.makeText(this, "Days "+ days, Toast.LENGTH_SHORT).show();        return days;

    }

    private DatePickerDialog.OnDateSetListener dateListener =
            new DatePickerDialog.OnDateSetListener() {
                @Override                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                    sdf = new SimpleDateFormat("dd/MM/yyyy");
                    calendar.set(year,month,dayOfMonth);
                    String newDate = sdf.format(calendar.getTime());
                    lmpEditDate.setText(newDate);

                    int numbDays = dateDifference(newDate);
                    if(numbDays < 42){
                       Toast.makeText(getApplicationContext(), " LMP date is invalid ", Toast.LENGTH_SHORT).show();
                        lmpEditDate.setText("");
                    }
                    else {
                        lmpEditDate.setText(newDate);
                    }


                }
            };
}

Comments

Popular posts from this blog

Android Date picker dialog setMinDate(long) and setMaxDate(long) issue

How to solve Android Studio rendering problem