我遵循getDiffDateMap
了计算 2 个日期和整数返回之间Map
的差异的方法,这些整数分别代表毫秒、秒、分钟、小时、天、月和年。
public static Map<Integer, String> getDiffDateMap(String dateA, String dateB) {
Calendar cal = Calendar.getInstance();
Map<Integer,String> out = new LinkedHashMap<Integer, String>();
long timeInMillA = 0;
long timeInMillB = 0;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date convertedDateA;
Date convertedDateB;
try {
convertedDateA = dateFormat.parse(dateA);
cal.setTime(convertedDateA);
timeInMillA = cal.getTimeInMillis();
convertedDateB = dateFormat.parse(dateB);
cal.setTime(convertedDateB);
timeInMillB = cal.getTimeInMillis();
} catch (ParseException e) {
e.printStackTrace();
}
long mili = timeInMillB - timeInMillA;
long sec = mili/1000;
long min = sec/60;
long hour = min/60;
long day = hour/24;
long week = day/7;
long month = day/31; // ????
long year = month/12;
out.put(7, mili + "");
out.put(6, sec + "");
out.put(5, min + "");
out.put(4, hour + "");
out.put(3, day + "");
out.put(2, week + "");
out.put(1, month + "");
out.put(0, year + "");
return out;
}
我的问题是根据实际天数计算月份:
long month = day/31; // or 30
例如:
Map<Integer,String> out = getDiffInMillsec("2012-9-01 20:9:01", "2012-10-01 20:10:01");
System.out.println(Arrays.asList(out));
我得到输出:月数和它的 0[{7=2592060000, 6=2592060, 5=43201, 4=720, 3=30, 2=4, 1=0, 0=0}]
在哪里1
。因为差异只有 30 天。我需要添加什么流程来解决这个问题?有什么建议么?