您的位置:首页 > 编程语言 > Java开发

java工具类总结

2013-09-03 15:03 141 查看
将例如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
=strYear+new Integer(no-1);

}else if(no>1){

strMonth
=strYear+"0"+new Integer(no-1);

}else{

strMonth
=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
=strYear+"0"+new Integer(no-3);

}else{

strQusrter
=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
=strYear+new Integer(no-1);

}else if(no>1){

strMonth
=strYear+"0"+new Integer(no-1);

}else{

strMonth
=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();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: