Java获取当前时间的前后指定N个月时间(近N个月时间校验)

获取近N个月以及每月的第一天及最后一天

public boolean timeCMonth(String date) {
    String lastDay = "";
    String firstDay = "";
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
    //获取前月的第一天
    for (int i = 0; i > -6; i--) {
        Calendar cal_1 = Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, i);
        cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        if (i == -5) {
            firstDay = format.format(cal_1.getTime());
        }
        //获取前月的最后一天
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, i + 1);//设置为1号,当前日期既为本月第一天
        cale.set(Calendar.DAY_OF_MONTH, 0);//设置为1号,当前日期既为本月第一天
        if (i == 0) {
            lastDay = format.format(cale.getTime());
        }
    }
    if (date.compareTo(firstDay) >= 0 && date.compareTo(lastDay) <= 0) {
        return true;
    }
    return false;
}

注意

  1. 动态修改format格式化参数实现精确匹配个人需求。
  2. 传入的方法参数作用为判断该参数是否在指定时间段内。

获取当前时间的前后N个月时间并校验任意时间是否在该时间段内

public boolean dayTime(String sTime,String eTime) {
    GregorianCalendar now = new GregorianCalendar();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String n = sdf.format(now.getTime());
    now.add(GregorianCalendar.MONTH,-6);   //可以是天数或月数  数字自定 -6前6个月
    String l=sdf.format(now.getTime());
    if (eTime.compareTo(sTime) >= 0 && sTime.compareTo(l) >= 0 && sTime.compareTo(n) <= 0
    && eTime.compareTo(l) >= 0 && eTime.compareTo(n) <= 0) {
        return true;
    }
    return false;
}

注意

  1. 方法参数为通用开始时间和结束时间的前后校验,以及两个参数是否满足近6个月内,即双参数校验。
  2. 默认-6代表当前时间之前6个月,可对等切换正负号实现后六个月时间校验。

将费用金额等业务数据进行三位逗号隔开格式化

Map<String,Object> map= new HashMap();
String totalDiscountedFee="1523.88";
BigDecimal b1=new BigDecimal(totalDiscountedFee);
b1=b1.divide(new BigDecimal(1000));
DecimalFormat df = new DecimalFormat("#,###.00");
String totalFee = df.format(b1);
map.put("totalDiscountedFee",totalFee);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值