Java使用Calendar月操作(时间段内的月份、前三个月、月份加1)

示例:

使用Calendar代替Data,对月份进行操作。

  • 获取当前时间和前三个月时间
  • 获取时间段内所有的年月集合
  • 月份加1

1、获取当前时间和前三个月时间

代码:

 

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");//格式化为2017-10
Calendar calendar = Calendar.getInstance();//得到Calendar实例
calendar.add(Calendar.MONTH, -3);//把月份减三个月
Date starDate = calendar.getTime();//得到时间赋给Data
String stardtr = formatter.format(starDate);//使用格式化Data
tv_start_time.setText(stardtr);//显示

如果想得到当前时间,把calendar.add(Calendar.MONTH, -3);去掉就可以了

 

结果:

当前时间:2017-10

减三个月:2017-07

2、获取时间段内所有的年月集合

代码:

 

/**
     * 获取时间段内所有的年月集合
     *
     * @param minDate 最小时间  2017-01
     * @param maxDate 最大时间 2017-10
     * @return 日期集合 格式为 年-月
     * @throws Exception
     */
    public static List<String> getMonthBetween(String minDate, String maxDate) throws Exception {
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        min.setTime(sdf.parse(minDate));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        max.setTime(sdf.parse(maxDate));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        Calendar curr = min;
        while (curr.before(max)) {
            result.add(sdf.format(curr.getTime()));
            curr.add(Calendar.MONTH, 1);
        }

        return result;
    }

结果:

 

[2017-01,2017-02,2017-03,2017-04,2017-05,2017-06,2017-07,2017-08,2017-09,2017-10]

3、月份加1

代码:

 

   /**
     * 月份加一
     * @param date
     * @return
     */
    public static String monthAddFrist(String date) {

        DateFormat df = new SimpleDateFormat("yyyy-MM");
        try {
            Calendar ct = Calendar.getInstance();
            ct.setTime(df.parse(date));
            ct.add(Calendar.MONTH, +1);
            return df.format(ct.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return "";
    }
结果:

 

2017-01 返回 2017-02

2017-12 返回 2018-01


 

ฅ՞•ﻌ•՞ฅ ~ 厚着脸皮要个赞赏哈 ~ ฅ՞•ﻌ•՞ฅ

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Beluga_白鲸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值