0

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

Beka
  • 341
  • 1
  • 5
  • 10

3 Answers3

0

Instead of using 2 simpledate format, you can parse them on same date format after parsing, you get 2 dates. You can find the miliseconds difference with :

`

long differenceAsMs = higherDate.getTime() - lowerDate.getTime();
int differenceAsDay = differenceAsMs / (1000 * 60 * 60 * 24);

` Hope this helps.

Warwicky
  • 281
  • 4
  • 9
0

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

Courtesy Link

Community
  • 1
  • 1
SearchForKnowledge
  • 3,663
  • 9
  • 49
  • 122
  • 1
    thanks it working.for example different is 9 days.now how i can write increment method 1 2 3 4 etc.... – Beka Oct 10 '14 at 14:04
  • @Beka You're welcome. Please clarify the request. Also, I suggest opening a new question so more people have visibility to your new request. Happy Coding! – SearchForKnowledge Oct 10 '14 at 14:19
  • SearchForKnowledge i opened new question but nobody answered...(this is a my question http://stackoverflow.com/questions/26306228/android-datetime-format-increment-day-between-two-datetimes – Beka Oct 11 '14 at 09:59
0

Use joda time library: JodaOrg/joda-time · GitHub

LocalDate date1 = new LocalDate(2014, 5, 25);
LocalDate date2 = new LocalDate(2014, 6, 15);

Period period = new Period(date1, date2, PeriodType.yearMonthDay());

int daysBetween = period.getDays();
int monthsBetween = period.getMonths();
int yearsBetween = period.getYears();

Note that period will return negative numbers if date2's date is before date1's date, it'll return positive numbers otherwise.

Mel
  • 1,730
  • 17
  • 33