Skip to content

Instantly share code, notes, and snippets.

@shuson
Created July 18, 2014 08:01
Show Gist options
  • Save shuson/698df7ce32580d692ed3 to your computer and use it in GitHub Desktop.
Save shuson/698df7ce32580d692ed3 to your computer and use it in GitHub Desktop.
Calculate Datetime difference with output in this format "xx year(s) xx month(s) xx day(s)"
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.Duration;
import org.joda.time.Period;
class DatetimeDiffWithFormat{
/**
* @param DateTime start
* @param DateTime end
* @param UnitDMYENUM unit
* @return String result with format like "xx year(s) xx month(s) xx day(s)"
*/
public String minusDate(DateTime start,DateTime end, UnitDMYENUM unit){
if (start==null || end ==null) return "";
String years = " year(s) ";
String months = " month(s) ";
String days = " day(s)";
StringBuffer result = new StringBuffer("");
if(start.isAfter(end)){
return result.toString();
}
if(unit==null){
Period btw = Period.fieldDifference(start.toLocalDate(), end.toLocalDate().plusDays(1));
int y = btw.getYears();
int m = btw.getMonths();
int d = btw.getDays();
if(y!=0){
result.append(y);
result.append(years);
}
if(m!=0){
result.append(m);
result.append(months);
}
if(d!=0){
result.append(d);
result.append(days);
}
}else if(unit == UnitDMY.DAY){
Duration duration = new Duration(start, end.plusDays(1));
result.append((int)duration.getStandardDays());
result.append(days);
}else if(unit == UnitDMY.MONTH){
Period btw = Period.fieldDifference(start.toLocalDate(), end.toLocalDate().plusDays(1));
int y = btw.getYears();
int m = btw.getMonths();
if(y>=1){
m += y*12;
}
if(m!=0){
result.append(m);
result.append(months);
}
}else if(unit == UnitDMY.YEAR){
Period btw = Period.fieldDifference(start.toLocalDate(), end.toLocalDate().plusDays(1));
int y = btw.getYears();
if(y!=0){
result.append(y);
result.append(years);
}
}
return result.toString().trim();
}
public enum UnitDMY{
DAY,
MONTH,
YEAR;
}
}
@Test
public void testMinusDateDateTimeDateTimeUnitDMY() {
DateTime start1 = new DateTime(2013,2,1,0,0);
DateTime end1 = new DateTime(2013,2,1,0,0);
assertEquals("1 day(s)",DateTimeFormatTool.minusDate(start1, end1, UnitDMY.DAY));
System.out.println("Start: "+start1);
System.out.println("End: "+end1);
System.out.println("result: "+DateTimeFormatTool.minusDate(start1, end1, UnitDMY.DAY));
DateTime start2 = new DateTime(2012,2,1,0,0);
DateTime end2 = new DateTime(2013,2,28,0,0);
assertEquals("394 day(s)",DateTimeFormatTool.minusDate(start2, end2, UnitDMY.DAY));
System.out.println("Start: "+start2);
System.out.println("End: "+end2);
System.out.println("result: "+DateTimeFormatTool.minusDate(start2, end2));
DateTime start3 = new DateTime(2012,2,1,0,0);
DateTime end3 = new DateTime(2012,2,10,0,0);
assertEquals("10 day(s)",DateTimeFormatTool.minusDate(start3, end3, null));
DateTime start4 = new DateTime(2013,2,1,0,0);
DateTime end4 = new DateTime(2013,2,28,0,0);
assertEquals("1 month(s)",DateTimeFormatTool.minusDate(start4, end4, null));
DateTime start5 = new DateTime(2012,2,1,0,0);
DateTime end5 = new DateTime(2012,2,29,0,0);
assertEquals("1 month(s)",DateTimeFormatTool.minusDate(start5, end5, null));
DateTime start6 = new DateTime(2012,3,1,0,0);
DateTime end6 = new DateTime(2012,4,29,0,0);
assertEquals("1 month(s) 29 day(s)",DateTimeFormatTool.minusDate(start6, end6, null));
DateTime start7 = new DateTime(2012,3,1,0,0);
DateTime end7 = new DateTime(2012,3,30,0,0);
assertEquals("30 day(s)",DateTimeFormatTool.minusDate(start7, end7, null));
DateTime start8 = new DateTime(2012,3,1,0,0);
DateTime end8 = new DateTime(2014,3,30,0,0);
assertEquals("2 year(s) 30 day(s)",DateTimeFormatTool.minusDate(start8, end8, null));
DateTime start9 = new DateTime(2012,3,1,0,0);
DateTime end9 = new DateTime(2014,4,3,0,0);
assertEquals("2 year(s) 1 month(s) 3 day(s)",DateTimeFormatTool.minusDate(start9, end9, null));
DateTime start10 = new DateTime(2012,3,1,0,0);
DateTime end10 = new DateTime(2014,4,3,0,0);
assertEquals("25 month(s)",DateTimeFormatTool.minusDate(start10, end10, UnitDMY.MONTH));
DateTime start11 = new DateTime(2012,3,1,0,0);
DateTime end11 = new DateTime(2012,3,3,0,0);
assertEquals("",DateTimeFormatTool.minusDate(start11, end11, UnitDMY.MONTH));
DateTime start12 = new DateTime(2012,3,1,0,0);
DateTime end12 = new DateTime(2012,3,3,0,0);
assertEquals("",DateTimeFormatTool.minusDate(start12, end12, UnitDMY.YEAR));
DateTime start13 = new DateTime(2012,3,1,0,0);
DateTime end13 = new DateTime(2013,3,3,0,0);
assertEquals("1 year(s)",DateTimeFormatTool.minusDate(start13, end13, UnitDMY.YEAR));
DateTime start14 = new DateTime(2012,3,1,0,0);
DateTime end14 = new DateTime(2012,9,3,0,0);
assertEquals("",DateTimeFormatTool.minusDate(start14, end14, UnitDMY.YEAR));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment