如何计算2个日期之间的实际月数? [英] How to calculate properly actual month count between 2 dates?

查看:246
本文介绍了如何计算2个日期之间的实际月数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循方法 getDiffDateMap ,它计算2个日期之间的差异,并返回代表毫秒,秒的整数的 Map 分钟,小时,天,月和年。

I have followed method getDiffDateMap that calculates difference between 2 dates and returns Map of Integers that represent milliseconds, seconds, minutes, hours, days, months and years respectively.

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

我的问题是从实际日数计算月份:

My problem is to calculate month from actual day count:

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)); 

我得到输出: [{7 = 2592060000,6 = 2592060,5 = 43201,4 = 720,3 = 30,2 = 4,1 = 0,0 = 0}] 其中 1 是月数和因为差距只有30天。需要添加什么流程来解决这个问题?任何建议?

I get output: [{7=2592060000, 6=2592060, 5=43201, 4=720, 3=30, 2=4, 1=0, 0=0}] where 1 is month count and its 0. because difference is 30 days only. What flow need I add to fix this problem? Any suggestions?

推荐答案


我已经按照方法getDiffDateMap计算2个日期和返回的地图代表毫秒,秒,分,小时,天,月和年的整数。

I have followed method getDiffDateMap that calculates difference between 2 dates and returns Map of Integers that represent milliseconds, seconds, minutes, hours, days, months and years respectively.

不要重新发明轮:)

Joda时间有代码来做到这一切,更多。例如:

Joda Time has code to do all this and more. For example:

LocalDateTime start = ...;
LocalDateTime end = ...;
Period difference = new Period(start, end, PeriodType.yearMonthDayTime());
int months = difference.getMonths(); // etc

请注意,您不能几个月,当你刚刚将不同的转换为几毫秒 - 因为月数将取决于开始/结束日期。 (30天可能是或可能不是一个月,例如...)

Note that you can't get at the number of months when you've just converted the different to a number of milliseconds - as the number of months will depend on the start/end date. (30 days may or may not be a month, for example...)

我强烈建议您在整个Java代码中使用Joda Time,而不是 java.util。* 。这是一个更好的API,而且很有希望意味着你很少 - 甚至需要编写自己的日期/时间处理代码。

I'd strongly advise you to use Joda Time throughout your Java code, in preference to java.util.*. It's a much better API, and one which will hopefully mean you rarely-if-ever need to write your own date/time handling code.

这篇关于如何计算2个日期之间的实际月数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆