[转]java中如何利用时间间隔算日期

4 篇文章 0 订阅
2 篇文章 0 订阅
今天公司要要我实现一个功能是时间段1 时间段2 时间段3 时间段4这4个时间段的设备缺陷率盒和缺陷数量显示在dhtmlx grid的一个吧表格中,说来惭愧居然不知道如何利用时间间隔计算问题.在网上找了许久终于找出了一个办法现在记录下来.时间段1是现在的日期,2,3,4分别在前一时间段上面减去3个周.当然这里面也包含了Calendar Date 和字符串之间的相互转换

String a[] = time.split("年");
String year = a[0];
String b = a[1];
String c[] = b.split("月");
String month = c[0];
String d = c[1];
String day = d.substring(0,d.indexOf("日"));
//时间段1
if(Integer.parseInt(month) > 10 && Integer.parseInt(day) > 10){
firstTime = year + "-" + month + "-" + day;
}else if(Integer.parseInt(month) > 10 && Integer.parseInt(day) < 10){
firstTime = year + "-" + month + "-0" + day;
}else if(Integer.parseInt(month) < 10 && Integer.parseInt(day) > 10 ){
firstTime = year + "-0" + month + "-" + day;
}else if(Integer.parseInt(month) < 10 && Integer.parseInt(day) < 10){
firstTime = year + "-0" + month + "-0" + day;
}
//时间段2
try {
Date date = df.parse(firstTime);
Calendar c2 = Calendar.getInstance();
c2.setTime(date);
int d2 = c2.get(Calendar.DAY_OF_MONTH);
d2 = d2 - 21;
c2.set(Calendar.DAY_OF_MONTH, d2);
secondTime = Constants.CharsetDecodeToUTF8(df.format(c2.getTime()));
} catch (ParseException e) {
e.printStackTrace();
}


//时间段3
try {
Date date = df.parse(secondTime);
Calendar c3 = Calendar.getInstance();
c3.setTime(date);
int d3 = c3.get(Calendar.DAY_OF_MONTH);
d3 = d3 - 21;
c3.set(Calendar.DAY_OF_MONTH, d3);
thirdTime = Constants.CharsetDecodeToUTF8(df.format(c3.getTime()));
} catch (ParseException e) {
e.printStackTrace();
}


//时间段4
try {
Date date = df.parse(secondTime);
Calendar c4 = Calendar.getInstance();
c4.setTime(date);
int d4 = c4.get(Calendar.DAY_OF_MONTH);
d4 = d4 - 21;
c4.set(Calendar.DAY_OF_MONTH, d4);
fourthTime = Constants.CharsetDecodeToUTF8(df.format(c4.getTime()));
} catch (ParseException e) {
e.printStackTrace();
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值