【问题标题】:Subtract two dates and get the result in years, months and days [duplicate]减去两个日期并得到年、月和日的结果[重复]
【发布时间】:2014-07-23 04:54:55
【问题描述】:

我编写了下面的代码,它将给出两个日期之间的差异,结果将以年、月和日为单位。

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

所以如果我减去 (2014 年 2 月 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 来解决这个问题吗???

【问题讨论】:

标签: java datetime


【解决方案1】:

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

    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;

【讨论】:

    【解决方案2】:

    您可以轻松地找到两个日期之间的天数,但找到月份可能会更加棘手: 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 time 轻松完成。

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

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

    但是,如果您想计算两个日期之间的天数、周数、月数和年数,那么您需要一个 Period 默认情况下,这会将两个日期时间之间的差异分成几部分,例如“1 个月, 2 周 4 天 7 小时”。

     Period p = new Period(startDate, endDate);
    

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

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

    此示例将返回不返回任何周或时间字段,因此上一个示例变为“1 个月零 18 天”。

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

    【讨论】:

    • 如果您想知道否决票:“我可以在不使用 joda time 的情况下解决这个问题吗???”。每次添加一天似乎 - 嗯 - 对我来说不是最佳选择。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 2015-08-20
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多