📄 utility.java
字号:
package com.rainbow.mas.plugin.dbplugin.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.StringTokenizer;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import com.rainbow.mas.SessionFactory;
import com.rainbow.mas.web.dto.MsgRoute;
public class Utility
{
private static final Logger logger = Logger.getLogger(Utility.class);
private static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final SimpleDateFormat sdff = new SimpleDateFormat("yyyyMMddHHmmssSSS");
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
private static final SimpleDateFormat mdf = new SimpleDateFormat("yyyy-MM");
private static final SimpleDateFormat tf = new SimpleDateFormat("HH:mm:ss");
private static final Calendar cal = Calendar.getInstance();
/**
* 是否为有效的手机号 ,支持多个手机号以 ','分隔
* HX 修改 支持15新号段
* @param mobileST
* @return
*/
public static boolean isValidMobile(String mobileST)
{
boolean isOK = true;
if (mobileST == null || mobileST.length() == 0)
return false;
StringTokenizer st = new StringTokenizer(mobileST, ",");
while (st.hasMoreTokens())
{
try
{
String mobile = st.nextToken();
if (!(mobile != null &&
((mobile.length() == 11 && (mobile.startsWith("13") || mobile.startsWith("15"))) ||
(mobile.length() == 13 && (mobile.startsWith("8613") || mobile.startsWith("8615"))
))))
{
return false;
}
for (int i = mobile.length() - 1; i >= 0; i--)
{
if (!Character.isDigit(mobile.charAt(i)))
{
return false;
}
}
}
catch (Exception e)
{
logger.error(e.getMessage(), e);
isOK = false;
break;
}
}
return isOK;
}
/**
* 去掉手机号码前的86,不用再查检号码有效性
* @param mobile
* @return
*/
public static String remove86Header(String mobile)
{
if(mobile.startsWith("86"))
return mobile.substring(2);
return mobile;
}
public static String formatDate(Date dt)
{
if (dt == null)
return "0000-00-00 00:00:00";
return df.format(dt);
}
/**
* 用于设置彩信附件的文件名
* @return
*/
public static String formatFileDate()
{
return sdff.format(new Date());
}
public static String getNowTime()
{
return formatDate(new Date());
}
public static String formatShortDate(Date dt)
{
if (dt == null)
return "0000-00-00";
return sdf.format(dt);
}
public static String formatYearMonthDate(Date dt)
{
if (dt == null)
return "0000-00";
//向前一个月
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
if (month > 0)
month--;
else
{
year--;
month = 11;
}
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
return mdf.format(cal.getTime());
}
public static String formatUserID(int id)
{
if (id <= 0)
return "00";
String idStr = "00";
if (id < 10)
idStr = "0" + String.valueOf(id);
else if (id < 100)
idStr = String.valueOf(id);
return idStr;
}
public static Date parseDate(
String yearStr,
String monthStr,
String dayStr,
String hourStr,
String minuteStr,
String secondStr)
{
if (yearStr == null
|| monthStr == null
|| dayStr == null
|| hourStr == null
|| minuteStr == null
|| secondStr == null)
return null;
Calendar cal = Calendar.getInstance();
int year = Integer.parseInt(yearStr);
int month = Integer.parseInt(monthStr);
int day = Integer.parseInt(dayStr);
int hour = Integer.parseInt(hourStr);
int minute = Integer.parseInt(minuteStr);
int second = Integer.parseInt(secondStr);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, second);
return cal.getTime();
}
public static String replaceSingleQuotes(String str)
{
if (str == null)
return null;
StringBuffer temp = new StringBuffer();
int length = str.length();
for (int i = 0; i < length; i ++)
{
if (str.charAt(i) == '\'')
{
temp.append("'");
temp.append("'");
}
else
temp.append(str.charAt(i));
}
return temp.toString();
}
public static Date toEndDate(
String yearStr,
String monthStr,
String dayStr,
String hourStr,
String minuteStr,
String secondStr)
{
if (yearStr == null
|| monthStr == null
|| dayStr == null
|| hourStr == null
|| minuteStr == null
|| secondStr == null)
return null;
int year = Integer.parseInt(yearStr);
int month = Integer.parseInt(monthStr);
int day = Integer.parseInt(dayStr);
int hour = Integer.parseInt(hourStr);
int minute = Integer.parseInt(minuteStr);
int second = Integer.parseInt(secondStr);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, second);
return cal.getTime();
}
public static Date toStartDate(
String yearStr,
String monthStr,
String dayStr,
String hourStr,
String minuteStr,
String secondStr)
{
if (yearStr == null
|| yearStr.length() == 0
|| monthStr == null
|| monthStr.length() == 0
|| dayStr == null
|| dayStr.length() == 0
|| hourStr == null
|| minuteStr == null
|| secondStr == null)
return null;
int year = Integer.parseInt(yearStr);
int month = Integer.parseInt(monthStr);
int day = Integer.parseInt(dayStr);
int hour = Integer.parseInt(hourStr);
int minute = Integer.parseInt(minuteStr);
int second = Integer.parseInt(secondStr);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DAY_OF_MONTH, day);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, second);
return cal.getTime();
}
/**
* 根据应用ID 和 扩展码 得到对应的服务代码,业务代码(如果要添)
* 查询应用表 MsgRoute
* @param ApID
* @param extendCode
* @return
*/
public static SendHeadInfo getSendHeadInfo(String apID,String extendCode)
{
if(apID == null || extendCode == null)
return null;
SendHeadInfo sendHeadInfo = null;
Session session = null;
try
{
session = SessionFactory.currentSession();
Iterator<MsgRoute> itmsgRoute = session.createQuery("from MsgRoute where applicationId = '" + apID + "'").iterate();
MsgRoute msgRoute = null;
//这二项是由是我自己来的 封闭成一个函数
//IN:ApID,extendCode
//OUT:服务代码,业务代码,bizConfig
String serverCode = null; //服务代码
String buinessCode = null; //业务代码
//
if(itmsgRoute.hasNext())
{
sendHeadInfo = new SendHeadInfo();
msgRoute = itmsgRoute.next();
//生成对应的服务代码
if(msgRoute.getUseSveCodeFlag() == 1) //使用新服务号码
serverCode = msgRoute.getNewServiceCode();
else
serverCode = msgRoute.getOldServiceCode();
if(msgRoute.getIsExtSvcCode() == 1) //要扩展服务号码为长号码
serverCode += extendCode;
if(msgRoute.getIsOpenBusCode() == 1) //也要使用业务代码
buinessCode = msgRoute.getBusinessCode();
//
sendHeadInfo.setServerCode(serverCode);
sendHeadInfo.setBuinessCode(buinessCode); //可能为空 代表不使用此参数
sendHeadInfo.setBizConfig(msgRoute.getBiz());
}
}
catch (Exception e)
{
logger.error(e.getMessage(),e);
}
/* 由调用的父函数负责关闭!!!!!!!!!!!
finally
{
SessionFactory.closeSession();
}
*/
return sendHeadInfo;
}
/**
* 根据应用ID 得到对应的服务代码
* 查询应用表 MsgRoute
* @param ApID
* @param extendCode
* @return
*/
public static String getServerCode(String apID)
{
if(apID == null)
return null;
String serverCode = null; //服务代码
Session session = null;
try
{
session = SessionFactory.currentSession();
Iterator<MsgRoute> itmsgRoute = session.createQuery("from MsgRoute where applicationId = '" + apID + "'").iterate();
MsgRoute msgRoute = null;
//这二项是由是我自己来的 封闭成一个函数
//IN:ApID,extendCode
//OUT:服务代码,业务代码,bizConfig
String buinessCode = null; //业务代码
//
if(itmsgRoute.hasNext())
{
msgRoute = itmsgRoute.next();
//生成对应的服务代码
if(msgRoute.getUseSveCodeFlag() == 1) //使用新服务号码
serverCode = msgRoute.getNewServiceCode();
else
serverCode = msgRoute.getOldServiceCode();
}
}
catch (Exception e)
{
logger.error(e.getMessage(),e);
}
finally
{
SessionFactory.closeSession();
}
return serverCode;
}
/**
* 以下二个函数 看是不是转换下编码
* @param s
* @return
*/
// 将 s 进行 BASE64 编码
public static String encodeBASE64(String s)
{
if (s == null)
return null;
return (new BASE64Encoder()).encode(s.getBytes());
}
// 将 BASE64 编码的字符串 s 进行解码
public static String decodeBASE64(String s)
{
if (s == null)
return null;
BASE64Decoder decoder = new BASE64Decoder();
byte[] b = null;
try
{
b = decoder.decodeBuffer(s);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
return new String(b);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -