Home > DeveloperSection > Forums > How to Select days between two datetime in android?
ezra heywood
ezra heywood

Total Post:145

Points:1019
Posted on    November-06-2014 10:54 PM

 Android Date  Time  DateTime 
Ratings:


 1 Reply(s)
 563  View(s)
Rate this:
i have two datetime 09/01/2014 and 09/10/2014.i want to check days between there datetimes. i wrote function witch can to change datetime format

public static String dateFormatterforLukka(String inputDate)

        {

 

            String inputFormat = "MM/dd/yyyy";

            String outputFormat = "d MMM";

 

            Date parsed = null;

            String outputDate = "";

            try

            {

                SimpleDateFormat df_input = new SimpleDateFormat(inputFormat,

                        new Locale("en", "US"));

                SimpleDateFormat df_output = new SimpleDateFormat(outputFormat,

                        new Locale("en", "US"));

 

 

                parsed = df_input.parse(inputDate);

                outputDate = df_output.format(parsed);

            }

            catch (Exception e)

            {

                outputDate = inputDate;

            }

            return outputDate;

        }

but i do not know how i can solve my problem if anyone knows solution please help me



Allen Scott

Total Post:36

Points:254
Posted on    November-07-2014 1:27 AM

You can try this function:

public String getDateDiffString(Date dateOne, Date dateTwo)
{
    long timeOne = dateOne.getTime();
    long timeTwo = dateTwo.getTime();
    long oneDay = 1000 * 60 * 60 * 24;
    long delta = (timeTwo - timeOne) / oneDay;

    if (delta > 0) {
        return "dateTwo is " + delta + " days after dateOne";
    }
    else {
        delta *= -1;
        return "dateTwo is " + delta + " days before dateOne";
    }
}

Don't want to miss updates? Please click the below button!

Follow MindStick