公共类方法

package com.ways.app.sales.utils;

import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ways.auth.model.AuthConstants;
import com.ways.auth.model.MyUserDetails;
import com.ways.framework.utils.LocaleMessageUtil;
import com.ways.waysdata.utils.Convert;

import flexjson.JSONSerializer;

/**
 * 公共工具类
 * @author yinlue
 *
 */
public class AppFrameworkUtil {

 /**
  *  四舍五入。保留num个小数点后的数字
  * @param bh
  * @param num
  * @return
  */
 public static String getNum(double bh, int num) {
  String numstr = "";
  numstr = new BigDecimal(bh).setScale(num, BigDecimal.ROUND_HALF_UP)+"";
  return numstr;
 }
 
 /**
  *  四舍五入。保留num个小数点后的数字
  * @param bh
  * @param num
  * @return
  */
 public static String getNum(String bh, int num) {
  
  if(AppFrameworkUtil.isEmpty(bh) || "-".equals(bh)) return "-";
  if("0.0".equals(bh) || "0".equals(bh)) return bh;
  String numstr = "";
  numstr = new BigDecimal(Double.parseDouble(bh)).setScale(num, BigDecimal.ROUND_HALF_UP)+"";
  return numstr;
 }
 
 /**
  * 字符串空判断
  * @param str
  * @return
  */
 public static boolean isEmpty(String str)
 {
  boolean flag = true;
  if(null != str && 0 != str.trim().length()) flag = false;
  return flag;
 }
 
 /**
  * 添加符号
  * @param str
  */
 public static String addSign(String str)
 {
  if(!AppFrameworkUtil.isEmpty(str) && !str.contains("-"))
  {
   str = "+" + str;
  }
  return str;
 }
 
 /**
  * 添加千分位
  * @param str
  * @return
  */
 public static String format(String str)
 {
  if(!isEmpty(str) && !"-".equals(str))
  {
   DecimalFormat df = new DecimalFormat("###,###");
   try {
     str = df.format(Long.parseLong(str));
   } catch (Exception e) {
    return str;
   }
  }
  return str;
 }
 
 /**
  * 序列化集合成JSON字符
  * @param list
  * @return
  */
 public static String serializableJSONData(List<?> list)
 {
  JSONSerializer serializer = new JSONSerializer();
  String json="";
  json = serializer.exclude("*.class").deepSerialize(list).replaceAll(":\\s*null\\s*", ":\"\"");
  return json;
 }
 
 public static String serializableJSONData(Object obj)
 {
  JSONSerializer serializer = new JSONSerializer();
  String json="";
  json = serializer.exclude("*.class").deepSerialize(obj).replaceAll(":\\s*null\\s*", ":\"\"");
  return json;
 }
 
 public static String serializableJSONData(Map<String, ?> map)
 {
  JSONSerializer serializer = new JSONSerializer();
  String json="";
  json = serializer.exclude("*.class").deepSerialize(map).replaceAll(":\\s*null\\s*", ":\"\"");
  return json;
 }

 
 
 /**
  * response JSON数据
  * @param response
  * @param context
  */
 public static void renderJSON(HttpServletResponse response,String context){
  PrintWriter out = null;
  try {
   response.setCharacterEncoding("UTF-8");
   response.setContentType("application/json");
   out =response.getWriter();
   out.write(context);
   out.flush();
  } catch (Exception e) {
   e.printStackTrace();
  }finally{
   out.close();
  }
 }
 
 /**
  * 获取不足一季的补充内容
  * @param year
  * @param month
  * @return String
  */
 public static String getQuartersLable(String year,int month){
  String monthI18n = "月";
  if("en".equals(LocaleMessageUtil.getCurrentLanguage())) monthI18n = "";
  int quarter = Convert.getQuarterNumbers(month);
  for (int i = 0; i < 4; i++) {
//   String label = LocaleMessageUtil.getLocaleMessage(getQuarterKeysMap().get((i+1)+""));
   String label = "";
   if((i+1) == quarter){
    if(i==3 && month % 3 != 0){
     //label += "{br}("+year+"’{br}"+(month%3==1 ? month:month-1+"-"+month)+""+monthI18n+")";
     label += "("+(month%3==1 ? month:month-1+"-"+month)+""+monthI18n+")";
    }else if(month % 3 != 0){
     //label += "("+year+"’"+(month%3==1 ? month:month-1+"-"+month)+""+monthI18n+")";
     label += "("+(month%3==1 ? month:month-1+"-"+month)+""+monthI18n+")";
    }
    return label;
   }
  }
  return "";
 }
 
 /**
  * 判断  * @param month
  * @return int
  */
 public static int getQuarterNumbers(int month){
  int nums = 0;
  if(month <= 3){
   nums = 1;
  }else if(month > 3 && month <= 6){
   nums = 2;
  }else if(month > 6 && month <= 9){
   nums = 3;
  }else if(month > 9 && month <= 12){
   nums = 4;
  }
  return nums;
 }
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值