【Java】指定两个日期相隔的天数日期及月份日期

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Test {
    public static void main(String[] args) throws ParseException {
        Test t = new Test();
        t.month();
    }

    public void day() throws ParseException {
        DateFormat d = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(d.parse(d.format(new Date())));
        c.set(Calendar.DATE, c.get(Calendar.DATE)-30);//当前30天前的日期
        String t = d.format(c.getTime());

        System.out.println(t);
        System.out.println(d.format(new Date()));

        String starDay = t;
        String endDay = d.format(new Date());
        Date date1 = d.parse(endDay);
        Date date2 = d.parse(starDay);
        //计算两个日期间有多少天
        int a = (int) ((date1.getTime() - date2.getTime()) / (1000*3600*24));
        System.out.println(a);
        //遍历出两个日期中间的日期
        for (int i = 0; i <= a; i++) {
            c.setTime(d.parse(starDay));
            c.set(Calendar.DATE, c.get(Calendar.DATE)+i);
            String day = d.format(c.getTime());
            System.out.println(i+" 当前:"+day);
        }
    }

    public void month() throws ParseException {

        DateFormat d = new SimpleDateFormat("yyyy-MM");
        Calendar c = Calendar.getInstance();
        c.setTime(d.parse(d.format(new Date())));
        c.set(Calendar.MONTH, c.get(Calendar.MONTH)-12);//当前月份前12个月的日期
        String t = d.format(c.getTime());

        System.out.println(t);
        System.out.println(d.format(new Date()));

        String starDay = t;
        String endDay = d.format(new Date());
        Date date1 = d.parse(endDay);
        Date date2 = d.parse(starDay);

        Calendar end  = Calendar.getInstance();
        Calendar start = Calendar.getInstance();

        end.setTime(date1);
        start.setTime(date2);

        if (!start.after(end)) {
            //计算两个日期间相差的月份
            int subMonthCount = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR) == 0) ? end.get(Calendar.MONTH) - start.get(Calendar.MONTH)  //同一年
                    : ((end.get(Calendar.YEAR) - start.get(Calendar.YEAR) >= 2) //年数差超过2年
                    ? (end.get(Calendar.YEAR) - start.get(Calendar.YEAR) - 1) * 12 + start.getActualMaximum(Calendar.MONTH) - start.get(Calendar.MONTH) + end.get(Calendar.MONTH) + 1
                    : start.getActualMaximum(Calendar.MONTH) - start.get(Calendar.MONTH)+ end.get(Calendar.MONTH) + 1);  //年数差为1,Calendar.get(MONTH) 第一月是0,所以+1

            System.out.println(subMonthCount);
            //遍历出两个日期间的月份日期
            for (int i = 0; i <= subMonthCount; i++) {
                c.setTime(d.parse(starDay));
                c.set(Calendar.MONTH, c.get(Calendar.MONTH)+i);
                String day = d.format(c.getTime());
                System.out.println("当前:"+day);
            }
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值