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

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

问题描述

用于计算两个日期和重新$ P $整数的回报率地图之间的差异

我按照方法 getDiffDateMap psent毫秒,秒,​​分钟,小时,天,分别几个月甚至几年。

 公共静态地图<整数,字符串> getDiffDateMap(字符串dateA,字符串dateB){    日历CAL = Calendar.getInstance();
    地图<整数,字符串> OUT =新的LinkedHashMap<整数,字符串>();
    长timeInMillA = 0;
    长timeInMillB = 0;    SimpleDateFormat的日期格式=新的SimpleDateFormat(YYYY-MM-DD HH:MM:SS);    日期convertedDateA;
    日期convertedDateB;    尝试{
        convertedDateA = dateFormat.parse(dateA);
        cal.setTime(convertedDateA);
        timeInMillA = cal.getTimeInMillis();
        convertedDateB = dateFormat.parse(dateB);
        cal.setTime(convertedDateB);
        timeInMillB = cal.getTimeInMillis();    }赶上(ParseException的E){
        e.printStackTrace();
    }    长毫= timeInMillB - timeInMillA;
    长秒=毫/ 1000;
    龙敏=秒/ 60;
    长小时=分钟/ 60;
    漫长的一天=小时/ 24;
    漫长的一周= / 7天;
    漫长的一个月= / 31天; // ????
    漫长的一年=月/ 12;    out.put(7,米利+);
    out.put(6秒+);
    out.put(5,分钟+);
    out.put(4,小时+);
    out.put(3,天+);
    out.put(2,每周+);
    out.put(1,月+);
    out.put(0,+一年);    返回的;
}

我的问题是,从实际的天数计算月:

 长月=日/ 31日; //或30

例如:

 地图<整数,字符串>出= getDiffInMillsec(2012年9月1日20:9:01,20时10分01秒2012-10-01);    的System.out.println(Arrays.asList(出));

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


解决方案

  

我按照一个计算两个日期之间的差值,并返回地图分别重新present毫秒,秒,​​分钟,小时,天,月,年的整数方法getDiffDateMap。


不要重新发明轮子:)

乔达时间有code ++做的这一切和更多。例如:

  LocalDateTime开始= ...;
LocalDateTime结束= ...;
期差=新周期(开始,结束,PeriodType.yearMonthDayTime());
INT个月= difference.getMonths(); //等

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

我强烈建议你使用乔达时间整个的Java code,在preference到的java.util。* 。这是一个的的更好的API,和一个希望这将意味着你很少-IF-以往任何时候都需要编写自己的日期/时间处理code。

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

For example:

Map<Integer,String> out = getDiffInMillsec("2012-9-01 20:9:01", "2012-10-01 20:10:01");

    System.out.println(Arrays.asList(out)); 

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?

解决方案

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.

Don't reinvent the wheel :)

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

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

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.

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

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