java工具类总结

将例如201101的数据转换成11年1季度
public static String getcategory(String banchNo) {
String category = "";
if (Integer.parseInt(banchNo.substring(5, 6)) == 3) {
category = banchNo.substring(2, 4) + "年1季度";
} else if (Integer.parseInt(banchNo.substring(5, 6)) == 6) {
category = banchNo.substring(2, 4) + "年2季度";
} else if (Integer.parseInt(banchNo.substring(5, 6)) == 9) {
category = banchNo.substring(2, 4) + "年3季度";
} else if (Integer.parseInt(banchNo.substring(4, 6)) == 12) {
category = banchNo.substring(2, 4) + "年4季度";
}
return category;

}


用spilt方法删除小数点后面全是的零的情况:
public static String delete(Double d) {
if (d == Integer.parseInt(d.toString().split("\\.")[0])) {
return d.toString().split("\\.")[0];
} else {
return d.toString();
}

}


格式话double使其不会显示科学计数法:
public static Double doubleFormat(Double db, int num) {
if (num<0) {num=0;}//过滤异常数据
StringBuffer sb = new StringBuffer(".");
//格式化的字符串
for (int i = 0; i < num; i++) {
sb.append("#");
}
return Double.parseDouble(new DecimalFormat(sb.toString()).format(db));
}
转换季度的另外一种方法:
public static String getQuarter(String batchNo){
if(batchNo==null||batchNo=="") {
  return "";
  }
String newBatchNo="";
//获取月份数据
Integer  no = Integer.parseInt(batchNo.substring(batchNo.length()-2));
//获取年份数据
Integer strYear =Integer.parseInt( batchNo.substring(0,4));
switch(no){
case 1:
case 2:
strYear--;
no=12;
break;
case 4:
case 5:
no=3;
break;
case 7:
case 8:
no=6;
break;
case 10:
case 11:
no=9;
break;
}
if(no!=12){
newBatchNo=strYear+"0"+no;
}else{
newBatchNo=strYear+""+no;
}
return newBatchNo;

}


根据距离当前日期最近的数据期次往前递推6个月份或4个季度
public static String[] getBatchNoListByFlag(String flag,String batchNo) {
  //如果数据期次为空,返回null
  if(batchNo==null||batchNo=="") {
  return null;
  }
  String[] reverse;
//存放数据期次的后两位--月份
Integer  no = Integer.parseInt(batchNo.substring(batchNo.length()-2));
//存放数据期次的前四位--年份
String strYear = batchNo.substring(0,4);
//存放月度的查询条件--月份查询前六个月
String[] strMonth = new String[6];
//存放季度的查询条件--季度查询前四个季度
String[] strQusrter = new String[4];
//将每个数组的首位先赋值,如果月份小于10,要在前面加上0
if(no<10){
strMonth[0] = strYear+"0"+no;
strQusrter[0] = strYear+"0"+no;
}else{
strMonth[0] = strYear+no;
strQusrter[0] = strYear+no;
}
//判断是否是3、6、9、12月份的,属于这些月份有两种情况
if(no!=3&&no!=6&&no!=9&&no!=12){
for(int n=1;n<6;n++){
if(no>10){
strMonth[n]=strYear+new Integer(no-1);
}else if(no>1){
strMonth[n]=strYear+"0"+new Integer(no-1);
}else{
strMonth[n]=new Integer(Integer.parseInt(strYear)-1).toString()+12;
strYear = new Integer(Integer.parseInt(strYear)-1).toString();
no=13;
}
no--;
}
}else{
//根据粒度分别设置查询条件
if("0".equals(flag)){
for(int n=1;n<4;n++){
if(no>3){
strQusrter[n]=strYear+"0"+new Integer(no-3);
}else{
strQusrter[n]=new Integer(Integer.parseInt(strYear)-1).toString()+"12";
strYear = new Integer(Integer.parseInt(strYear)-1).toString();
no=15;
}
no=no-3;
}
//对数据期次数组反向排序
reverse=new String[strQusrter.length];
for(int i=0;i<strQusrter.length;i++) {
reverse[i]=strQusrter[strQusrter.length-i-1];
}
return reverse;
}else{
for(int n=1;n<6;n++){
if(no>10){
strMonth[n]=strYear+new Integer(no-1);
}else if(no>1){
strMonth[n]=strYear+"0"+new Integer(no-1);
}else{
strMonth[n]=new Integer(Integer.parseInt(strYear)-1).toString()+12;
strYear = new Integer(Integer.parseInt(strYear)-1).toString();
no=13;
}
no--;
}

         }
       }
//对数据期次数组反向排序
reverse=new String[strMonth.length];
for(int i=0;i<strMonth.length;i++) {
reverse[i]=strMonth[strMonth.length-i-1];
}
return reverse;

    }


将对象装换成字符串的形式:
public static String objToStr(Object obj) {
JsonConfig config = new JsonConfig();
config.setJsonPropertyFilter(new IgnoreFieldProcessorImpl());
JSONObject jsonObj = JSONObject.fromObject(obj, config);
return jsonObj.toString();
}


保留小数:
 v 需要保留小数的double类型数据
scale 保留几位小数
public static double round4(double v,int scale)
{
if(scale<0){
throw new IllegalArgumentException("The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值