Revision: 4561 http://sourceforge.net/p/vexi/code/4561 Author: mkpg2 Date: 2013-10-10 03:20:24 +0000 (Thu, 10 Oct 2013) Log Message: ----------- Date. Support diff for days.
Modified Paths: -------------- branches/vexi3/org.vexi-library.value/src/main/java/org/vexi/value/Date.java Modified: branches/vexi3/org.vexi-library.value/src/main/java/org/vexi/value/Date.java =================================================================== --- branches/vexi3/org.vexi-library.value/src/main/java/org/vexi/value/Date.java 2013-10-02 21:47:04 UTC (rev 4560) +++ branches/vexi3/org.vexi-library.value/src/main/java/org/vexi/value/Date.java 2013-10-10 03:20:24 UTC (rev 4561) @@ -402,7 +402,10 @@ case PART_QUARTER: int quarters = getPart(PART_QUARTER) - from.getPart(PART_QUARTER); return years*4 +quarters; - + case PART_DAY: + Calendar c1 = asCalendar(); + Calendar c2 = from.asCalendar(); + return (int)daysBetween(c1, c2); } throw new Error("Unsupported dateDiff: "+partName(part)); } @@ -432,4 +435,33 @@ public String toString() { return format(); } + + + + + + + + static public long daysBetween(final Calendar startDate, final Calendar endDate) { + if(startDate.after(endDate)){ + return -daysBetween(endDate, startDate); + } + + int MILLIS_IN_DAY = 1000 * 60 * 60 * 24; + long endInstant = endDate.getTimeInMillis(); + int presumedDays = + (int) ((endInstant - startDate.getTimeInMillis()) / MILLIS_IN_DAY); + Calendar cursor = (Calendar) startDate.clone(); + cursor.add(Calendar.DAY_OF_YEAR, presumedDays); + long instant = cursor.getTimeInMillis(); + if (instant == endInstant) + return presumedDays; + + final int step = instant < endInstant ? 1 : -1; + do { + cursor.add(Calendar.DAY_OF_MONTH, step); + presumedDays += step; + } while (cursor.getTimeInMillis() <= endInstant); + return presumedDays; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ October Webinars: Code for Performance Free Intel webinars can help you accelerate application performance. Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from the latest Intel processors and coprocessors. See abstracts and register > http://pubads.g.doubleclick.net/gampad/clk?id=60134071&iu=/4140/ostg.clktrk _______________________________________________ Vexi-svn mailing list Vexi-svn@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/vexi-svn