月份 日期, 年份, 加减一个

注意:

  ca.set(2011, 12, 2);   2012-01-02

  ca.set(2011, 0, 2);    2011-01-02

日期 

ca.set(2011, 2, 1);      2011-03-01

 ca.set(2011, 2, 31);  2011-03-31

 ca.set(2011, 2, 32);  2011-04-01

 ca.set(2011, 2, 0);  2011-02-28

 ca.set(2011, 2, -1);  2011-02-27

0.常用函数:

获取本月最后一天

  ca.set(Calendar.DAY_OF_MONTH,ca.getActualMaximum(Calendar.DAY_OF_MONTH));

获取本月第一天 

  cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 

  cale.set(Calendar.DAY_OF_MONTH,0);//  获去前月最后一天

 

1. 减一个月'

       这里7月 不要超过  32天

       ca.set(2011, 6, 31);// 月份是从0开始的,所以11表示12月     
       dateToFormatChar(ca.getTime());
       
       int i=-1;
       ca.add(Calendar.MONTH, i);// 月份减1 

显示: 

2011-07-31
2011-06-30
2011-05-30
2011-04-30
2011-03-30
2011-02-28
2011-01-28

2.加一个月 

       ca.set(2011, 6, 31);// 月份是从0开始的,所以11表示12月  
       dateToFormatChar(ca.getTime());
       
       int i=1;
       ca.add(Calendar.MONTH, i);// 月份减1 
       dateToFormatChar(ca.getTime());
       ca.add(Calendar.MONTH, i);// 月份减1 
       dateToFormatChar(ca.getTime());
       ca.add(Calendar.MONTH, i);// 月份减1 
       dateToFormatChar(ca.getTime());
       ca.add(Calendar.MONTH, i);// 月份减1 

2011-07-31
2011-08-31
2011-09-30
2011-10-30
2011-11-30
2011-12-30
2012-01-30
2012-02-29
2012-03-29

 

3.日期   加一天    

  ca.set(2011, 6, 31);// 月份是从0开始的,所以11表示12月  
       dateToFormatChar(ca.getTime());
       
       int i=1;
       ca.add(Calendar.DATE, i);// 月份减1 
       dateToFormatChar(ca.getTime());

2011-07-31
2011-08-01
2011-08-02
2011-08-03
2011-08-04

 3.日期   减一天

       ca.set(2011, 6, 1);// 月份是从0开始的,所以11表示12月  
       dateToFormatChar(ca.getTime());
       int i=-1;
       ca.add(Calendar.DATE, i);// 月份减1 
       dateToFormatChar(ca.getTime());
       ca.add(Calendar.DATE, i);// 月份减1 

2011-07-01
2011-06-30
2011-06-29
2011-06-28
2011-06-27

     //ca.setTime(new Date()); // 设置时间为当前时间  
       ca.set(2011, 6, 0);// 月份是从0开始的,所以11表示12月  
       dateToFormatChar(ca.getTime());
       int i=-1;
       ca.add(Calendar.DATE, i);// 月份减1 
       dateToFormatChar(ca.getTime());

2011-06-30
2011-06-29

转载于:https://my.oschina.net/u/1760858/blog/855388

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值