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();
}
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();
}
相关文章推荐
- 黑马程序员_Java第17天知识总结_集合框架的工具类_Collections_Arrays_将数组变成list集合_集合变数组_高级for循环_可变参数
- java常用工具类组件 总结
- Java基础学习总结(49)——Excel导入导出工具类
- Java复习笔记+经验总结-04 java异常 常用工具类
- Java同步工具类总结
- java线程同步工具类总结
- Java—遍历集合的N种方式总结&Collections工具类
- Java基础学习总结(49)——Excel导入导出工具类
- 基于孤傲苍狼的上传下载总结出的java工具类
- Java基础学习总结(49)——Excel导入导出工具类
- 【Java学习笔记之十二】Java8增强的工具类:Arrays的用法整理总结
- java的office转化为pdf,pdf转化为swf的工具类 在线预览之总结
- java同步工具类总结
- 黑马程序员:Java基础总结-----Arrays 数组的工具类
- 黑马程序员 JAVA基础 数组、数组工具类、文档注释总结
- Java数组总结之自制简单数组工具类
- JAVA 多线程同步工具类总结
- java基础总结19-java常用API(StringBuffer和Arrays工具类)
- 黑马程序员_Java基础 数组、数组工具类、文档注释总结
- JAVA 多线程同步工具类总结