减去两个日期并获得年,月和日的结果

时间:2014-06-03 00:15:31

标签: java datetime

我写了下面的代码,它会给我两个日期之间的区别,结果将是几年,几个月和几天。

对我来说这是很好的代码,但问题是几个月因为它固定为30天

所以如果我减去 (2014年1月1日) - (2014年1月1日) 应该是一个月 但是用我的代码就可以了 1个月 1天

import java.util.*;

public class Main
{
public static void main(String[] args)
{
    int y_d = 365;
    int m_d = 30;


    Scanner scn = new Scanner(System.in);

    System.out.println("Enter your first date: ");

    System.out.print("Day   : ");
    int day1 = scn.nextInt();

    System.out.print("Month : ");
    int month1 = scn.nextInt();

    System.out.print("Year  : ");
    int year1 = scn.nextInt();

    Calendar cal1 = Calendar.getInstance();
    cal1.set(year1,month1-1,day1);

    System.out.println();
    System.out.println("Enter your second date: ");

    System.out.print("Day   : ");
    int day2 = scn.nextInt();

    System.out.print("Month : ");
    int month2 = scn.nextInt();

    System.out.print("Year  : ");
    int year2 = scn.nextInt();

    Calendar cal2 = Calendar.getInstance();
    cal2.set(year2,month2-1,day2);

    long milis1 = cal1.getTimeInMillis();
    long milis2 = cal2.getTimeInMillis();

    long day = milis2 - milis1;

    long days = day / (24*60*60*1000);

    System.out.println();
    System.out.println("The difference : " + days + " days");

    long y_r = (days % y_d);
    long m_r = (y_r % m_d);

    System.out.println("The result is approximatly");
    System.out.println(days/y_d + " year(s)");
    System.out.println(y_r/m_d + " month(s)");
    System.out.println(m_r + " day(s)");
}
}

我可以在不使用joda time ???

的情况下解决此问题

2 个答案:

答案 0 :(得分:0)

我不太确定你在寻找什么(一些额外的日期差异会有所帮助),但这应该让你朝着正确的方向前进:

    long year3;
    long month3;
    long day3 = day2 - day1;

    Calendar cal3 = Calendar.getInstance();
    cal3.set(Calendar.YEAR, year2);
    if(day3 < 0) {
            //Assuming that if the days are now negative, we'll "sacrifice" a month to bring them back positive.
            month2 -= 1;
            cal3.set(Calendar.MONTH, month2-1);
            day3 += cal3.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
    month3 = month2 - month1;
    if(month3 < 0) {
            //If the months went negative, we need to convert a year into months to make them positive again.
            month3 += 12;
            year2 -=1;
    }
    year3 = year2 - year1;

答案 1 :(得分:-1)

您可以轻松找到两个日期之间的天数,但查找月份可能会更棘手: http://tripoverit.blogspot.com.au/2007/07/java-calculate-difference-between-two.html

/** Using Calendar **/  
//assert: startDate must be before endDate  
public static long daysBetween(Calendar startDate, Calendar endDate) {  
  Calendar date = (Calendar) startDate.clone();  
  long daysBetween = 0;  
  while (date.before(endDate)) {  
    date.add(Calendar.DAY_OF_MONTH, 1);  
    daysBetween++;  
  }  
  return daysBetween;  
}  
}  

查找日期之间的月份差异: Java Date month difference

但是,这可以使用joda时间轻松完成。

如果您只想要两个日期之间的整天天数,那么您可以在Joda-Time 1.4版本中使用新的Days类。

  Days d = Days.daysBetween(startDate, endDate);
  int days = d.getDays();

如果您想计算两个日期之间的天数,周数,月数和年数,那么您需要一个句点默认情况下,这会将两个日期时间之间的差异拆分为多个部分,例如&#34; 1个月,2周,4天和7个小时&#34;

 Period p = new Period(startDate, endDate);

您可以使用PeriodType控制提取哪些字段。

Period p = new Period(startDate, endDate, PeriodType.yearMonthDay());

此示例将返回不返回任何周或时间字段,因此前一个示例变为&#34; 1个月和18天&#34;。

更多信息: http://joda-time.sourceforge.net/faq.html#datediff