求出字符“2013-9-8”与“2013-10-1”相差的天数

package zuoye;

import java.util.Calendar;

public class DateSub {
public static void main(String[] args) {
String date1 = "2013-9-8";
String date2 = "2013-10-1";
String[] d1 = date1.split("-");//将字符串拆分
int y1 = Integer.parseInt(d1[0]);//进行转换
int m1 = Integer.parseInt(d1[1]);
int da1 = Integer.parseInt(d1[2]);
String[] d2 = date2.split("-");
int y2 = Integer.parseInt(d2[0]);
int m2 = Integer.parseInt(d2[1]);
int da2 = Integer.parseInt(d2[2]);
Calendar c = Calendar.getInstance();
c.set(y1, (m1 - 1), da1);//设置值
int x = c.get(Calendar.DAY_OF_YEAR);//获取该天为该年中的第几天,然后相减即为结果
c.set(y2, (m2 - 1), da2);
int y = c.get(Calendar.DAY_OF_YEAR);
System.out.println("2013-9-8到2013-10-1有:" + (y - x) + "天");
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值