JAVA计算两个日期之间的天数

 

public

class CalcDate { /** * @param args * @author wuyaowen * function calculate distance of two date given * such as 2009.03.05 ~ 2005.09.07 */ public int calcDate(String dateBegin, String dateEnd){ int distance = 0;           // 时间之间的天数

String db = dateBegin;  // 开始日期

String de = dateEnd;    // 结束日期

int strby = 0; // substring of begin date year 开始日期的年份
int strbm = 0; // 开始日期的月份
int strbd = 0; // 开始日期的日子
int strey = 0; int strem = 0; int stred = 0;

// 类型转换

strby = Integer.parseInt(db.substring(0, 4));

strbm = Integer.parseInt(db.substring(5, 7));

strbd = Integer.parseInt(db.substring(8, 10));

strey = Integer.parseInt(de.substring(0, 4));

strem = Integer.parseInt(de.substring(5, 7));

stred = Integer.parseInt(de.substring(8, 10));

if (stred < strbd){

stred = stred + 30;

strem = strem -1;

}

if (strem < strbm){

strem = strem + 12;

strey = strey -1;

}

distance = (strey - strby)*365 + (strem - strbm)*30 + stred - strbd;

return distance;

}

public static void main(String[] args) { // TODO Auto-generated method stub

CalcDate cd =

new CalcDate(); int dates = cd.calcDate( "2005-09-07" , "2009-03-05" );

System.

out .print(dates/365 + " years " + dates%365/30 + " months " + dates%365%30 + " dates" );

/**

debugging int test = 1287%365/30; System.out.print("/n" + test); //test subString() String str = "C12natown"; String subStr = str.substring(1, 3); System.out.println(Integer.parseInt(subStr) - 1);

*/

}

// End of main()

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值