现在你要做的是计算两个日期之间的差异并保存差异,这个差异将用于从下一个字段中减去。
例如:我们必须显示年、月、周……等等。在计算两个日期之间的年数后,我们将从月份中减去年数,对于下一个字段也是如此...日期时间的层次结构如下...
年-月-周-日-时-分-秒
现在是sn-p
/**
*
* @param context which activity its calling
* @param currentDateTime current time
* @param futureDateTime future time from which we have to calculate difference
* @param selectedUnitsFromSettings which units we have to find difference such as years,weeks....etc
* which will be stored in list...
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static HashMap dateBasedOnUnitCalculator(
Context ctx, LocalDateTime currentDateTime,
LocalDateTime futureDateTime, List<String> selectedUnitsFromSettings) {
//to store the dates
Date currentTime = currentDateTime.toDateTime().toDate();
Date futureTime = futureDateTime.toDateTime().toDate();
//to store the units
String currentUnit = "";
String prevUnit = "";
//to store the value which you want to remove
int prevValue = 0;
//to store the calculated values in hashmap
HashMap units = new HashMap();
for(int i = 0; i < selectedUnitsFromSettings.size(); i++){
//to store the current unit for calculation of future date
currentUnit = selectedUnitsFromSettings.get(i);
//to remove higher unit from new future date we will use prevUnit
if(i > 0){
prevUnit = selectedUnitsFromSettings.get(i-1);
futureTime = getDateForPreviousUnit(futureTime,prevUnit,prevValue);
}
//now calculate the difference
if(currentUnit.equals("Year")){
Years q = Years.yearsBetween(new DateTime(currentTime.getTime()), new DateTime(futureTime.getTime()));
int years = q.getYears();
prevValue = years;
units.put("Year", prevValue);
}else if(currentUnit.equals("Month")){
Months w = Months.monthsBetween(new DateTime(currentTime.getTime()), new DateTime(futureTime.getTime()));
int months = w.getMonths();
prevValue = months;
units.put("Month", prevValue);
}else if(currentUnit.equals("Week")){
Weeks e = Weeks.weeksBetween(new DateTime(currentTime.getTime()), new DateTime(futureTime.getTime()));
int weeks = e.getWeeks();
prevValue = weeks;
units.put("Week", prevValue);
}else if(currentUnit.equals("Day")){
Days r = Days.daysBetween(new DateTime(currentTime.getTime()), new DateTime(futureTime.getTime()));
int days = r.getDays();
prevValue = days;
units.put("Day", prevValue);
}else if(currentUnit.equals("Hour")){
Hours a = Hours.hoursBetween(new DateTime(currentTime.getTime()), new DateTime(futureTime.getTime()));
int hours = a.getHours();
prevValue = hours;
units.put("Hour", prevValue);
}else if(currentUnit.equals("Minute")){
Minutes s = Minutes.minutesBetween(new DateTime(currentTime.getTime()), new DateTime(futureTime.getTime()));
int minutes = s.getMinutes();
prevValue = minutes;
units.put("Minute", prevValue);
}else if(currentUnit.equals("Second")){
Seconds d = Seconds.secondsBetween(new DateTime(currentTime.getTime()), new DateTime(futureTime.getTime()));
int seconds = d.getSeconds();
prevValue = seconds;
units.put("Second", prevValue);
}
}
return units;
}
计算上一个单元的未来时间
/**
*
* @param futureTime the future date which will be modified
* @param prevString which unit value to be reduced
* @param prevValue how much to reduce from the current unit
* @return
*/
private static Date getDateForPreviousUnit(Date futureTime,
String prevString, int prevValue) {
Date calculatedDate = futureTime;
Constants.showLog(TAG, "prev string is "+prevString);
if(prevString.equals("Year")){
calculatedDate = new DateTime(futureTime).minusYears(prevValue).toDate();
}else if(prevString.equals("Month")){
calculatedDate = new DateTime(futureTime).minusMonths(prevValue).toDate();
}else if(prevString.equals("Week")){
calculatedDate = new DateTime(futureTime).minusWeeks(prevValue).toDate();
}else if(prevString.equals("Day")){
calculatedDate = new DateTime(futureTime).minusDays(prevValue).toDate();
}else if(prevString.equals("Hour")){
calculatedDate = new DateTime(futureTime).minusHours(prevValue).toDate();
}else if(prevString.equals("Minute")){
calculatedDate = new DateTime(futureTime).minusMinutes(prevValue).toDate();
}else if(prevString.equals("Second")){
calculatedDate = new DateTime(futureTime).minusSeconds(prevValue).toDate();
}
return calculatedDate;
}