HOW TO SELECT DAYS BETWEEN TWO DATETIME IN ANDROID?

ezra heywood

Total Post:145

Points:1019
Posted by  ezra heywood
 1016  View(s)
Ratings:
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

  1. Allen Scott

    Post:46

    Points:324
    Re: How to Select days between two datetime in android?

    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";
        }
    }

      Modified On Apr-06-2018 05:01:17 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!