笔记

/**
 * 表情包工具类 
 */
public class EmojiUtils {
private static EmojiUtils emojiUtils = new EmojiUtils();
private EmojiUtils() {
}


public static EmojiUtils getEmojiUtils() {
return emojiUtils;
}

/**
* 判断字符串中是否包含表情符号
* 
* @return
*/
public static boolean containsEmoji(String source) {
int len = source.length();
boolean isEmoji = false;
for (int i = 0; i < len; i++) {
char hs = source.charAt(i);
if (0xd800 <= hs && hs <= 0xdbff) {
if (source.length() > 1) {
char ls = source.charAt(i + 1);
int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
if (0x1d000 <= uc && uc <= 0x1f77f) {
return true;
}
}
} else {
// non surrogate
if (0x2100 <= hs && hs <= 0x27ff && hs != 0x263b) {
return true;
} else if (0x2B05 <= hs && hs <= 0x2b07) {
return true;
} else if (0x2934 <= hs && hs <= 0x2935) {
return true;
} else if (0x3297 <= hs && hs <= 0x3299) {
return true;
} else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c
|| hs == 0x2b1b || hs == 0x2b50 || hs == 0x231a) {
return true;
}
if (!isEmoji && source.length() > 1 && i < source.length() - 1) {
char ls = source.charAt(i + 1);
if (ls == 0x20e3) {
return true;
}
}
}
}
return isEmoji;
}

}





    /**
     * 新建时间并格式转换 年月日 时分秒
     */
       public String newDate() {
    Date date = new Date();
    SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return time.format(date);
        }



      /**
* String转Date
* 
* @return
*/
public static Date strToDateYMD(String time) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
return format.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return null;

}





     import java.io.InputStream;
     import java.net.HttpURLConnection;
     import java.net.URL;

      /***

         get请求 无参

* @param url
* @return
*/
public String HTTPGet(String url) {
String value = null;
try {
URL urlGet = new URL(url);
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
http.setRequestMethod("GET"); // 必须是get方式请求
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
http.connect();
InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
value = new String(jsonBytes, "UTF-8");
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return value;

}





     

      import java.security.MessageDigest;

      /**

* MD5加码 生成32位md5码
*/
public static String string2MD5(String inStr) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
return "";
}
char[] charArray = inStr.toCharArray();
byte[] byteArray = new byte[charArray.length];


for (int i = 0; i < charArray.length; i++)
byteArray[i] = (byte) charArray[i];
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}


/**
* 加密解密算法 执行一次加密,两次解密
*/
public static String convertMD5(String inStr) {
char[] a = inStr.toCharArray();
for (int i = 0; i < a.length; i++) {
a[i] = (char) (a[i] ^ 't');
}
String s = new String(a);
return s;
}

       

       /**
* 两个Int 算概率 保存小数点后两位

*/

       import java.text.DecimalFormat;

public String algorithm(int SurplusNumber, int number) {
Float nowprize = (float) SurplusNumber / (float) number * 100;
DecimalFormat df = new DecimalFormat("##0.00");// 格式化小数
String probability = df.format(nowprize);// 返回的是String类型
return probability;

}



import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.jfinal.kit.PropKit;
import net.sf.jxls.exception.ParsePropertyException;
import net.sf.jxls.transformer.XLSTransformer;


/**
 * @author ChenXb
 *
 *         2017年9月5日
 */
public class Tools {
/**
* 
* map转换json. <br>
* 详细说明
* 
* @param map
*            集合
* @return
* @return String json字符串
* @throws @author
*             slj
*/
public static String mapToJson(Map<String, Object> map) {
Set<String> keys = map.keySet();
String key = "";
Object value;
StringBuffer jsonBuffer = new StringBuffer();
jsonBuffer.append("{");
for (Iterator<String> it = keys.iterator(); it.hasNext();) {
key = (String) it.next();
value = map.get(key);
if (value instanceof java.util.List) {
jsonBuffer.append("\"" + key + "\":" + value);
} else {
jsonBuffer.append("\"" + key + "\":" + "\"" + value + "\"");
}


if (it.hasNext()) {
jsonBuffer.append(",");
}
}
jsonBuffer.append("}");
return jsonBuffer.toString();
}


/**
* 计算总页数
*/
public static int getSize(int size) {
String pageCount = PropKit.use("system.properties").get("pageCount");
int page = Integer.parseInt(pageCount);
if (size % page == 0) {
return size / page;
} else {
return size / page + 1;
}
}

/**
* 输出文件流
* @param filename
* @param wb
* @param response
*/
public static void writeStream(String filename, Workbook wb, HttpServletResponse response) {
try {
filename += ".xlsx";
filename.replaceAll("/", "-");
filename = URLEncoder.encode(filename, "UTF-8");
response.reset();
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + filename);
response.setContentType("application/octet-stream;charset=UTF-8");

OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());

wb.write(outputStream);

outputStream.flush();
outputStream.close();


} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 导出公用的方法
*/
public static void comExportExcel(@SuppressWarnings("rawtypes") List list,String title,HttpServletResponse response,String templateFilePath){
Map<String, Object> datamap = new HashMap<String, Object>();
datamap.put("list", list); // 导出excel的数据
datamap.put("title", title); // 导出excel的标题
InputStream in = null;
try {
in = new FileInputStream(templateFilePath); // 将模板文件转换为文件流
XLSTransformer transformer = new XLSTransformer(); // jxls生成excel
//HSSFWorkbook  XSSFWorkbook
//HSSF是POI工程对Excel 97(-2007)文件操作的纯Java实现 
//XSSF是POI工程对Excel 2007 OOXML (.xlsx)文件操作的纯Java实现 
XSSFWorkbook wb = (XSSFWorkbook) transformer.transformXLS(in, datamap); // 将excel流转换为Workbook 
// Sheet sheet = wb.getSheetAt(0); // 取第一个sheet
// sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7)); // 四个参数分别是:起始行,结束行,起始列,结束列
writeStream(title, wb, response); // 返回excel
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (ParsePropertyException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
} finally{
try {
if(in != null) in.close();
} catch (IOException e) {
}
}
}
}





public class LocalDateTimeConverter implements Converter<LocalDateTime> {

 @Override
 public Class<LocalDateTime> supportJavaTypeKey() {
  return LocalDateTime.class;
 }

 @Override
 public CellDataTypeEnum supportExcelTypeKey() {
  return CellDataTypeEnum.STRING;
 }

 @Override
 public LocalDateTime convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
                                        GlobalConfiguration globalConfiguration) {
  return LocalDateTime.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
 }

 @Override
 public CellData<String> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty,
                                            GlobalConfiguration globalConfiguration) {
  return new CellData<>(value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
 }

}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值