获取近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;
}
注意
- 动态修改format格式化参数实现精确匹配个人需求。
- 传入的方法参数作用为判断该参数是否在指定时间段内。
获取当前时间的前后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;
}
注意
- 方法参数为通用开始时间和结束时间的前后校验,以及两个参数是否满足近6个月内,即双参数校验。
- 默认-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);