# Conflicts: # smtweb-framework/bpm/src/main/resources/static/event/bpm/sys/base/dict/dictList.jsmaster
@@ -174,7 +174,17 @@ public class CodeBuildHandler extends AbstractHandler { | |||
private void buildJavaService(long pageId) { | |||
ModelForm form = ModelFormCache.getInstance().get(pageId); | |||
String sName = form.getService(); | |||
if (StringUtils.isEmpty(sName)) throw new BizException("页面设置未定义服务名!" + form.getTitle()); | |||
if (StringUtils.isEmpty(sName)) { | |||
//模块名称 | |||
form = ModelFormCache.getInstance().get(form.getParent()); | |||
if(form == null){ | |||
throw new BizException("页面设置未定义服务名!" + form.getTitle()); | |||
} | |||
sName = form.getService(); | |||
if(StringUtils.isEmpty(sName)){ | |||
throw new BizException("模块设置未定义服务名!" + form.getTitle()); | |||
} | |||
} | |||
sName = CodeGenUtil.toUpperHump(sName); | |||
SwMap model = new SwMap(); | |||
@@ -2,11 +2,11 @@ package cc.smtweb.system.bpm.web.design.form; | |||
import cc.smtweb.framework.core.cache.AbstractCache; | |||
import cc.smtweb.framework.core.common.SwEnum; | |||
import cc.smtweb.framework.core.exception.BizException; | |||
import cc.smtweb.framework.core.common.SwMap; | |||
import cc.smtweb.framework.core.db.cache.ModelTableCache; | |||
import cc.smtweb.framework.core.db.vo.ModelField; | |||
import cc.smtweb.framework.core.db.vo.ModelTable; | |||
import cc.smtweb.framework.core.exception.BizException; | |||
import cc.smtweb.framework.core.mvc.variable.SwVariableFactory; | |||
import cc.smtweb.framework.core.session.UserSession; | |||
import cc.smtweb.framework.core.util.CommUtil; | |||
@@ -252,13 +252,16 @@ public class ModelFormHelper { | |||
public static String buildEngineModel(ModelForm form, SwMap params, UserSession us, boolean incExtra) { | |||
PageDatasets datasets = form.getDatasets(); | |||
if (datasets == null || datasets.list == null) return ""; | |||
String service = form.getService(); | |||
if (StringUtils.isEmpty(service) && form.getParent() > 0) { | |||
service = ModelFormCache.getInstance().get(form.getParent()).getService(); | |||
} | |||
SwMap ret = new SwMap(); | |||
PageModel pageInfo = parsePageInfo(form.getContent()); | |||
if (pageInfo == null) return ""; | |||
ret.put("pageId", form.getEntityId()); | |||
ret.put("label", form.getTitle()); | |||
ret.put("service", form.getService()); | |||
ret.put("service", service); | |||
ret.put("module", ModelProjectCache.getInstance().getModule(form.getPrjId())); | |||
buildOptsEx(form, ret); | |||
//form不用管 | |||
@@ -374,7 +377,8 @@ public class ModelFormHelper { | |||
BpmConfigBean bpmConfigBean = SpringUtil.getBean(BpmConfigBean.class); | |||
Map<String, String> mapIdeaModules = IdeaUtil.getModules(bpmConfigBean.getCodeJavaPath(), bpmConfigBean.getMode()); | |||
if (mapIdeaModules == null || mapIdeaModules.isEmpty()) throw new BizException("没有定义idea项目的路径(smtweb.bpm.codeJavaPath)!"); | |||
if (mapIdeaModules == null || mapIdeaModules.isEmpty()) | |||
throw new BizException("没有定义idea项目的路径(smtweb.bpm.codeJavaPath)!"); | |||
String codeJavaPath = mapIdeaModules.get(moduleName); | |||
if (StringUtils.isEmpty(codeJavaPath)) { | |||
throw new BizException("没有找到对应项目在idea中Module的路径(" + moduleName + ")!"); | |||
@@ -0,0 +1,64 @@ | |||
package cc.smtweb.system.bpm.web.sys.base.dict; | |||
import cc.smtweb.framework.core.annotation.SwTable; | |||
import cc.smtweb.framework.core.common.SwMap; | |||
import cc.smtweb.framework.core.db.impl.DefaultEntity; | |||
/** | |||
* Created by 1 at 2022-07-27 19:59:05 | |||
* 实体【[数据字典](SYS_DICT)】的Entity类 | |||
*/ | |||
@SwTable("SYS_DICT") | |||
public class Dict extends DefaultEntity { | |||
public static final String ENTITY_NAME = "SYS_DICT"; | |||
public Dict() { | |||
super(ENTITY_NAME); | |||
} | |||
/** 主键 */ | |||
public long getId() { | |||
return getLong("sdc_id"); | |||
} | |||
/** 主键 */ | |||
public void setId(long sdc_id) { | |||
put("sdc_id", sdc_id); | |||
} | |||
/** 类别 */ | |||
public long getType() { | |||
return getLong("sdc_type"); | |||
} | |||
/** 类别 */ | |||
public void setType(long sdc_type) { | |||
put("sdc_type", sdc_type); | |||
} | |||
/** 编码 */ | |||
public String getCode() { | |||
return getStr("sdc_code"); | |||
} | |||
/** 编码 */ | |||
public void setCode(String sdc_code) { | |||
put("sdc_code", sdc_code); | |||
} | |||
/** 名称 */ | |||
public String getName() { | |||
return getStr("sdc_name"); | |||
} | |||
/** 名称 */ | |||
public void setName(String sdc_name) { | |||
put("sdc_name", sdc_name); | |||
} | |||
/** 备注 */ | |||
public String getRemark() { | |||
return getStr("sdc_remark"); | |||
} | |||
/** 备注 */ | |||
public void setRemark(String sdc_remark) { | |||
put("sdc_remark", sdc_remark); | |||
} | |||
} |
@@ -0,0 +1,34 @@ | |||
package cc.smtweb.system.bpm.web.sys.base.dict; | |||
import cc.smtweb.framework.core.annotation.SwCache; | |||
import cc.smtweb.framework.core.cache.AbstractEntityCache; | |||
import cc.smtweb.framework.core.cache.CacheManager; | |||
import java.util.ArrayList; | |||
import java.util.Comparator; | |||
import java.util.List; | |||
import java.util.Set; | |||
/** | |||
* Created by 1 at 2022-07-27 19:59:05 | |||
* 实体【[数据字典](SYS_DICT)】的缓存类 | |||
*/ | |||
@SwCache(ident = "SYS_DICT", title = "数据字典") | |||
public class DictCache extends AbstractEntityCache<Dict> { | |||
//缓存key:按编码 | |||
public final static String mk_code = "code"; | |||
public static DictCache getInstance() { | |||
return CacheManager.getIntance().getCache(DictCache.class); | |||
} | |||
public DictCache() { | |||
//缓存key:按编码 | |||
regMap(mk_code, "sdc_type,sdc_code"); | |||
} | |||
//缓存key:按编码 | |||
public final Dict getByCode(String key) { | |||
return getByKey(mk_code, key); | |||
} | |||
} |
@@ -0,0 +1,32 @@ | |||
package cc.smtweb.system.bpm.web.sys.base.dict; | |||
import cc.smtweb.framework.core.db.impl.DefaultEntity; | |||
import cc.smtweb.framework.core.util.NumberUtil; | |||
import cc.smtweb.system.bpm.web.engine.dynPage.DynPageSaveHandler; | |||
/** | |||
* Created by Akmm at 2022/7/1 15:47 | |||
* 区划保存 | |||
*/ | |||
public class DictHandler { | |||
static class DictSaveHandler extends DynPageSaveHandler { | |||
@Override | |||
protected void setNewId(DefaultEntity bean) { | |||
//字典 id用 类型id+编码 | |||
if (bean instanceof Dict) { | |||
Dict dict = (Dict) bean; | |||
dict.setEntityId(NumberUtil.getLongIgnoreErr(dict.getType() + dict.getCode())); | |||
return; | |||
} | |||
//字典类型 id用编码 | |||
if (bean instanceof DictType) { | |||
DictType dictType = (DictType) bean; | |||
dictType.setEntityId(NumberUtil.getLongIgnoreErr(dictType.getCode())); | |||
return; | |||
} | |||
super.setNewId(bean); | |||
} | |||
} | |||
} |
@@ -0,0 +1,24 @@ | |||
package cc.smtweb.system.bpm.web.sys.base.dict; | |||
import cc.smtweb.framework.core.annotation.SwService; | |||
import cc.smtweb.framework.core.mvc.service.AbstractHandler; | |||
import cc.smtweb.system.bpm.web.engine.dynPage.DynPageService; | |||
import cc.smtweb.system.bpm.web.sys.user.area.AreaHandler; | |||
/** | |||
* Created by 1 at 2022-07-28 09:23:54 | |||
* 页面【[数据字典]的服务类 | |||
*/ | |||
@SwService | |||
public class DictService extends DynPageService { | |||
@Override | |||
protected AbstractHandler createHandler(String type) { | |||
switch (type) { | |||
case TYPE_SAVE: | |||
return new DictHandler.DictSaveHandler(); | |||
} | |||
return super.createHandler(type); | |||
} | |||
} |
@@ -0,0 +1,64 @@ | |||
package cc.smtweb.system.bpm.web.sys.base.dict; | |||
import cc.smtweb.framework.core.annotation.SwTable; | |||
import cc.smtweb.framework.core.common.SwMap; | |||
import cc.smtweb.framework.core.db.impl.DefaultEntity; | |||
/** | |||
* Created by 1 at 2022-07-27 19:59:05 | |||
* 实体【[字典类别](SYS_DICT_TYPE)】的Entity类 | |||
*/ | |||
@SwTable("SYS_DICT_TYPE") | |||
public class DictType extends DefaultEntity { | |||
public static final String ENTITY_NAME = "SYS_DICT_TYPE"; | |||
public DictType() { | |||
super(ENTITY_NAME); | |||
} | |||
/** 主键 */ | |||
public long getId() { | |||
return getLong("sdct_id"); | |||
} | |||
/** 主键 */ | |||
public void setId(long sdct_id) { | |||
put("sdct_id", sdct_id); | |||
} | |||
/** 编码 */ | |||
public String getCode() { | |||
return getStr("sdct_code"); | |||
} | |||
/** 编码 */ | |||
public void setCode(String sdct_code) { | |||
put("sdct_code", sdct_code); | |||
} | |||
/** 名称 */ | |||
public String getName() { | |||
return getStr("sdct_name"); | |||
} | |||
/** 名称 */ | |||
public void setName(String sdct_name) { | |||
put("sdct_name", sdct_name); | |||
} | |||
/** 归属项目 */ | |||
public long getPrjId() { | |||
return getLong("sdct_prj_id"); | |||
} | |||
/** 归属项目 */ | |||
public void setPrjId(long sdct_prj_id) { | |||
put("sdct_prj_id", sdct_prj_id); | |||
} | |||
/** 备注 */ | |||
public String getRemark() { | |||
return getStr("sdct_remark"); | |||
} | |||
/** 备注 */ | |||
public void setRemark(String sdct_remark) { | |||
put("sdct_remark", sdct_remark); | |||
} | |||
} |
@@ -0,0 +1,34 @@ | |||
package cc.smtweb.system.bpm.web.sys.base.dict; | |||
import cc.smtweb.framework.core.annotation.SwCache; | |||
import cc.smtweb.framework.core.cache.AbstractEntityCache; | |||
import cc.smtweb.framework.core.cache.CacheManager; | |||
import java.util.ArrayList; | |||
import java.util.Comparator; | |||
import java.util.List; | |||
import java.util.Set; | |||
/** | |||
* Created by 1 at 2022-07-27 19:59:05 | |||
* 实体【[字典类别](SYS_DICT_TYPE)】的缓存类 | |||
*/ | |||
@SwCache(ident = "SYS_DICT_TYPE", title = "字典类别") | |||
public class DictTypeCache extends AbstractEntityCache<DictType> { | |||
//缓存key:按编码 | |||
public final static String mk_code = "code"; | |||
public static DictTypeCache getInstance() { | |||
return CacheManager.getIntance().getCache(DictTypeCache.class); | |||
} | |||
public DictTypeCache() { | |||
//缓存key:按编码 | |||
regMap(mk_code, "sdct_code"); | |||
} | |||
//缓存key:按编码 | |||
public final DictType getByCode(String key) { | |||
return getByKey(mk_code, key); | |||
} | |||
} |
@@ -0,0 +1,36 @@ | |||
/** | |||
* Created by 1 at 2022-07-26 11:11:24 | |||
* 注册事件类【数据字典管理】,参看event.defaultEvent.js | |||
*/ | |||
window.$swEvent.setup("bpm.sys.base.dict.dictList", { | |||
setup(page){ | |||
const { $params,$refs, $widgets, $model, $utils, $tabRouter, $api,$lifecycle } = page || {}; | |||
const { $$message, $$http } = $utils || {}; | |||
// 数据字典类型表格行点击事件 | |||
const dictTypeRowClick = (row) => { | |||
// 缓存变量 | |||
$model.temp?.setFormVal("dict_type_id",row.sdct_id); | |||
// 调用字典加载 | |||
$api.loadOne("dictList"); | |||
}; | |||
// 数据字典新增初始化 | |||
const afterAddDict = () => { | |||
// 设置字典类型 | |||
$model.dictEdit?.setFormVal("sdc_type", $model.temp?.getFormVal("dict_type_id")); | |||
}; | |||
// 删除类型之后 | |||
const afterDelDictType = () => { | |||
$api.loadOne("dictTypeList"); | |||
}; | |||
// 删除字典之后 | |||
const afterDelDict = () => { | |||
$api.loadOne("dictList"); | |||
}; | |||
return { | |||
dictTypeRowClick, | |||
afterAddDict, | |||
afterDelDictType, | |||
afterDelDict, | |||
} | |||
} | |||
}); |
@@ -85,6 +85,11 @@ | |||
<artifactId>commons-lang3</artifactId> | |||
<version>3.12.0</version> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.dom4j</groupId> | |||
<artifactId>dom4j</artifactId> | |||
<version>RELEASE</version> | |||
</dependency> | |||
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-jexl3 --> | |||
<dependency> | |||
<groupId>org.apache.commons</groupId> | |||
@@ -0,0 +1,21 @@ | |||
package cc.smtweb.framework.core.util; | |||
public class Consts { | |||
public static final String BLANK_STRING = " "; | |||
public static final String NULL_STRING = "-"; | |||
public static final String TREE_ROOT_ID = "-1"; | |||
public static final String WORD_LINE_BREAK = "" + (char) 11; | |||
public static final String CLASS_ROOT_ID = "GB000"; | |||
public static final String PERSON_TYPE_ROOT_ID = "GP000000"; | |||
public final static String LEVELCODE_SEPARATOR = "-"; | |||
public static final String DEF_PWD = "abc@123456"; //初始密码 | |||
public static final String fileOperator = System.getProperty("file.separator"); | |||
public static final String TOP_PARTY_ID = "1"; //顶级机构主键 | |||
public static final String TOP_PARTY_DEFAULT_CODE = "10000"; //顶级单位默认机构编码,初始化后可以到系统中进行修改; | |||
public final static String DEF_SCHEMA = "sys";//默认的数据库名 | |||
public final static String FIELD_LAST_TIME = "last_time";//最后更新时间,字段名 | |||
public final static String FIELD_CREATE_PARTY = "create_party_id";//创建单位,字段名 | |||
} |
@@ -1,8 +1,11 @@ | |||
package cc.smtweb.framework.core.util; | |||
import org.apache.commons.lang3.time.DateUtils; | |||
import org.springframework.util.unit.DataUnit; | |||
import java.sql.Timestamp; | |||
import java.text.DateFormat; | |||
import java.text.ParseException; | |||
import java.text.SimpleDateFormat; | |||
import java.util.Calendar; | |||
import java.util.Date; | |||
@@ -23,12 +26,213 @@ public class DateUtil { | |||
private static ThreadLocal<DateFormat> simpleDateFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMdd")); | |||
private static ThreadLocal<DateFormat> simpleDatetimeFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmss")); | |||
private static ThreadLocal<DateFormat> simpleLongDatetimeFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmssSSS")); | |||
private static TimeZone mTimeZone = TimeZone.getTimeZone("Asia/Shanghai"); | |||
private static ThreadLocal<DateFormat> stdTimeFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("HH:mm:ss")); | |||
private static ThreadLocal<DateFormat> stdLongDatetimeFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")); | |||
final static int[] m_seasonBeginTable = new int[]{ | |||
Calendar.JANUARY, Calendar.JANUARY, Calendar.JANUARY, | |||
Calendar.APRIL, Calendar.APRIL, Calendar.APRIL, | |||
Calendar.JULY, Calendar.JULY, Calendar.JULY, | |||
Calendar.OCTOBER, Calendar.OCTOBER, Calendar.OCTOBER | |||
}; | |||
private static TimeZone mTimeZone = TimeZone.getTimeZone("Asia/Shanghai"); | |||
private DateUtil(){} | |||
/** | |||
* 取得系统今天的时间串 | |||
* | |||
* @return current datetime, pattern: "yyyy-MM-dd HH:mm:ss". | |||
*/ | |||
public static String nowStdDateTimeString() { | |||
return stdDatetimeFormat.get().format(nowTimestamp()); | |||
} | |||
/** | |||
* Return a Timestamp for right now | |||
* | |||
* @return Timestamp for right now | |||
*/ | |||
public static Timestamp nowTimestamp() { | |||
return new Timestamp(System.currentTimeMillis()); | |||
} | |||
/** | |||
* 得到如下格式字符串 "YYYY-MM-DD" | |||
* | |||
* @param d 时间戳 如果传入为null,则返回“”; | |||
* @return 日期时间串 | |||
*/ | |||
public static String toStdDateString(Date d) { | |||
if (d == null) return ""; | |||
return stdDateFormat.get().format(d); | |||
} | |||
/** | |||
* 比较两个日期是否相等 | |||
*/ | |||
public static int compareDate(Date a, Date b) { | |||
// java.sql.Date date = new java.sql.Date(System.currentTimeMillis()); | |||
// date中,会保留有时间秒数. | |||
// java.sql.Date a = java.sql.Date.valueOf("2007-01-01"); | |||
// a中,就没有时间秒数 | |||
// 所以,在比较时就统一按字符串[yyyy-MM-dd]进行比较而不直接用a.compareTo(b) | |||
String stra = toStdDateString(a); | |||
String strb = toStdDateString(b); | |||
return stra.compareTo(strb); | |||
} | |||
/** | |||
* 获取当前时间数字串,例如20100909121359 标识2010年09月09日 12时13分59秒 | |||
* | |||
* @return Long | |||
*/ | |||
public static Long nowDateTimeNumber() { | |||
return Long.valueOf(nowDateTimeString()); | |||
} | |||
/** | |||
* 取得系统今天的时间串 | |||
* | |||
* @return current datetime, pattern:"yyyyMMddHHmmss" | |||
*/ | |||
public static String nowDateTimeString() { | |||
return simpleDatetimeFormat.get().format(nowTimestamp()); | |||
} | |||
/** | |||
* 将yyyyMMdd变为yyyy-mm-dd | |||
* | |||
* @param s 原日期串,默认用-进行分割 | |||
* @return yyyy-mm-dd | |||
*/ | |||
public static String toStdDateString(String s) { | |||
if (PubUtil.isEmptyId(s)) return ""; | |||
if (s.contains("-")) return s; | |||
return toStdDateString(s, "-", false); | |||
} | |||
/** | |||
* 将yyyyMMdd日期串用分隔符进行分割 | |||
* | |||
* @param s 原日期串,默认用-进行分割 | |||
* @param split 分隔符; | |||
* @param returnEmptyIfErrDate 如果不是8位的日期串,则控制是反馈“”,还是返回原串; | |||
* @return 用分隔符,如“-”,“.”进行分割后的日期串; | |||
*/ | |||
public static String toStdDateString(String s, String split, boolean returnEmptyIfErrDate) { | |||
if (PubUtil.isEmptyId(s)) return ""; | |||
try { | |||
if (s == null || s.length() < 8) { | |||
return returnEmptyIfErrDate ? "" : s; | |||
} | |||
return s.substring(0, 4) + split + s.substring(4, 6) + split + s.substring(6, 8); | |||
} catch (Exception e) { | |||
return ""; | |||
} | |||
} | |||
/** | |||
* 得到如下字符串 "YYYY-dd | |||
* | |||
* @param d String, 例如20110304131101 | |||
* @return 日期时间串,如:2011-03 | |||
*/ | |||
public static String toStdYd(String d) { | |||
if (d == null || d.length() <6) return ""; | |||
return d.substring(0, 4)+"-"+d.substring(4,6); | |||
} | |||
/** | |||
* @param datetime 时间戳,例如20110304131101; | |||
* @return 毫秒数 | |||
*/ | |||
public static long getTimeInMillis(String datetime) { | |||
if (datetime == null) return 0L; | |||
try { | |||
Calendar calendar = Calendar.getInstance(); | |||
calendar.setTime(stdDatetimeFormat.get().parse(datetime)); | |||
return calendar.getTimeInMillis(); | |||
} catch (Exception e) { | |||
return 0L; | |||
} | |||
} | |||
/** | |||
/** | |||
* a是否小于当前日期 | |||
*/ | |||
public static boolean isLessThanCurrentDate(java.sql.Date a) { | |||
int i = compareDate(a, new java.sql.Date(System.currentTimeMillis())); | |||
return i < 0; | |||
} | |||
/** | |||
* a是否小于b | |||
*/ | |||
public static boolean isLessThanCurrentDate(java.sql.Date a, java.sql.Date b) { | |||
int i = compareDate(a, b); | |||
return i < 0; | |||
} | |||
/** | |||
* 检查beging + days 是否在end前[包括end];(如:招行开始日期与结束日期之差只能在100以内) | |||
* | |||
* @return false 大于了end | |||
*/ | |||
public synchronized static boolean isBeforeDate(Date beging, int days, Date end) { | |||
GregorianCalendar gc = new GregorianCalendar(); | |||
gc.setTimeZone(mTimeZone); | |||
gc.setTime(beging); | |||
gc.add(GregorianCalendar.DATE, days - 1); | |||
Date addDate = gc.getTime(); | |||
return (end.compareTo(addDate) <= 0); | |||
} | |||
/** | |||
* 得到如下字符串 "YYYY-MM-DD HH:MM:SS" | |||
* | |||
* @param d 时间戳,如果传入为null,则返回“”; | |||
* @return 日期时间串 | |||
*/ | |||
public static String toStdDateTimeString(Date d) { | |||
if (d == null) return ""; | |||
return stdDatetimeFormat.get().format(d); | |||
} | |||
/** | |||
* 得到如下字符串 "YYYY-MM-DD HH:MM:SS" | |||
* | |||
* @param d long, 例如20110304131101 | |||
* @return 日期时间串,如:2011-03-04 13:11:01 | |||
*/ | |||
public static String toStdDateTimeString(long d) { | |||
return toStdDateTimeString(String.valueOf(d)); | |||
} | |||
/** | |||
* 得到如下字符串 "YYYY-MM-DD HH:MM:SS" | |||
* | |||
* @param d String, 例如20110304131101 | |||
* @return 日期时间串,如:2011-03-04 13:11:01 | |||
*/ | |||
public static String toStdDateTimeString(String d) { | |||
if (d == null || d.length() != 14) return ""; | |||
return toStdDateString(d.substring(0, 8)) + " " + d.substring(8, 10) + ":" + d.substring(10, 12) + ":" + d.substring(12); | |||
} | |||
/** | |||
* 得到如下字符串 "MM-DD HH:MM" | |||
* | |||
* @param d String, 例如20110304131101 | |||
* @return 日期时间串,如:03-04 13:11 | |||
*/ | |||
public static String toStdDateTime3String(String d) { | |||
if (d == null || d.length() != 14) return ""; | |||
return toStdDateString(d.substring(0, 8)).substring(5) + " " + d.substring(8, 10) + ":" + d.substring(10, 12); | |||
} | |||
/** | |||
* 得到如下字符串 "YYYY-MM-DD HH:MM" | |||
* | |||
* @param d String, 例如20110304131101 | |||
* @return 日期时间串,如:2011-03-04 13:11 | |||
*/ | |||
public static String toStdDateTime2String(String d) { | |||
if (d == null || d.length() != 14) return ""; | |||
return toStdDateString(d.substring(0, 8)) + " " + d.substring(8, 10) + ":" + d.substring(10, 12); | |||
} | |||
/** | |||
* 获取星期几 | |||
* | |||
*/ | |||
public static String getWeekOfDay() { | |||
return simpleWeekFormat.get().format(nowTimestamp()); | |||
} | |||
/** | |||
* 获取当前时间 | |||
* @return | |||
*/ | |||
@@ -436,6 +640,589 @@ public class DateUtil { | |||
} | |||
} | |||
/** | |||
* 取与指定日期间隔年的日期 | |||
* | |||
* @return String java.sql.Date.toString() | |||
*/ | |||
public synchronized static String getAfterYear(Date dt, int years) { | |||
if (dt == null) return ""; | |||
if (years == 0) return dt.toString(); | |||
GregorianCalendar dy = new GregorianCalendar(); | |||
dy.setTime(dt); | |||
dy.add(GregorianCalendar.YEAR, years); | |||
java.sql.Date ret = new java.sql.Date(dy.getTimeInMillis()); | |||
return ret.toString(); | |||
} | |||
/** | |||
* 取与指定日期间隔年的日期 | |||
* | |||
* @param dt (yyyy-mm-dd) | |||
* @param years 间隔年 j | |||
* @return java.sql.Date.toString() | |||
*/ | |||
public static String getAfterYear(String dt, int years) { | |||
return getAfterYear(java.sql.Date.valueOf(dt), years); | |||
} | |||
/** | |||
* get days between fromDate and thruDate | |||
* | |||
* @return the days between fromDate and thruDate | |||
*/ | |||
public static int getDifferDays(Date f, Date t) throws IllegalArgumentException { | |||
return getDifferSeconds(f, t) / (24 * 60 * 60); | |||
} | |||
public static int getDifferMinutes(Date f, Date t) throws IllegalArgumentException { | |||
return getDifferSeconds(f, t) / 60; | |||
} | |||
public static int getDifferMinutes(String f, String t) throws IllegalArgumentException { | |||
return getDifferSeconds(Timestamp.valueOf(f), Timestamp.valueOf(t)) / 60; | |||
} | |||
public static int getDifferMinutes(long f, long t) throws IllegalArgumentException { | |||
return getDifferSeconds(new Date(f), new Date(t)) / 60; | |||
} | |||
public static int getDifferSeconds(Date f, Date t) throws IllegalArgumentException { | |||
if (f == null || t == null) return 0; | |||
if (t.compareTo(f) < 0) { | |||
String msg = "[" + t + "] 比" + "[" + f + "] 早, 应该相反!"; | |||
throw new IllegalArgumentException(msg); | |||
} | |||
return (int) ((t.getTime() - f.getTime()) / 1000); | |||
} | |||
/** | |||
* 判断strDate 是否是日期格式的字符串(支持如 yyyy-mm-dd)。 | |||
* | |||
* @param strDate 字符串,判断其是否为日期格式。 | |||
* @return boolean 如果为日期格式则返回=true; | |||
*/ | |||
public static boolean isDateFormat(String strDate) { | |||
return isDateFormat(strDate, "YYYY-MM-DD"); | |||
} | |||
/** | |||
* 判断strDate 是否是日期格式的字符串。 | |||
* | |||
* @param strDate 字符串,判断其是否为日期格式。 | |||
* @param format 字符串,YYYY-MM,YYYYMM,YYYYMMDD,YYYY-MM-DD,YYYY-MM-DD:HH, 空表示所有上述格式,非上述内容将默认为YYYY-MM-DD; | |||
* @return boolean 如果为日期格式则返回=true; | |||
*/ | |||
public static boolean isDateFormat(String strDate, String format) { | |||
if (PubUtil.isEmpty(strDate)) { | |||
return false; | |||
} | |||
if (PubUtil.isEmpty(format)) { | |||
format = "YYYY-MM-DD"; | |||
} | |||
switch (format) { | |||
case "YYYY-MM": | |||
return strDate.matches("\\d{4}-\\d{2}") && isDateValue(strDate.substring(0, 4), strDate.substring(5), "01"); | |||
case "YYYYMM": | |||
return strDate.matches("\\d{6}") && isDateValue(strDate.substring(0, 4), strDate.substring(4), "01"); | |||
case "YYYYMMDD": | |||
return strDate.matches("\\d{8}") && isDateValue(strDate.substring(0, 4), strDate.substring(4, 6), strDate.substring(6)); | |||
case "YYYY-MM-DD": | |||
return strDate.matches("\\d{4}-\\d{2}-\\d{2}") && isDateValue(strDate.substring(0, 4), strDate.substring(5, 7), strDate.substring(8)); | |||
case "YYYY-MM-DD:HH": | |||
return strDate.matches("\\d{4}-\\d{2}-\\d{2}:[0-5][0-9]") && isDateValue(strDate.substring(0, 4), strDate.substring(5, 7), strDate.substring(8, 10)); | |||
case "HHmmss": | |||
return strDate.matches("[0-5][0-9][0-5][0-9][0-5][0-9]") && isTimeValue(strDate.substring(0, 2), strDate.substring(2, 4), strDate.substring(4)); | |||
default: | |||
return false; | |||
} | |||
} | |||
public static boolean isDateValue(String year, String month, String day) { | |||
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd"); | |||
s.setLenient(false);//这个的功能是不把1996-13-3 转换为1997-1-3 | |||
try { | |||
s.parse(year + "-" + month + "-" + day); | |||
return true; | |||
} catch (ParseException e) { | |||
return false; | |||
} | |||
} | |||
public static boolean isTimeValue(String hour, String min, String sec) { | |||
SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss"); | |||
s.setLenient(false); | |||
try { | |||
s.parse(hour + ":" + min + ":" + sec); | |||
return true; | |||
} catch (ParseException e) { | |||
return false; | |||
} | |||
} | |||
/** | |||
* 将日期变为"yyyyMMdd" | |||
* | |||
* @param date 日期 | |||
* @return 字符串,如果传入参数为null,则返回当前日期; | |||
*/ | |||
public static String toDateString(Date date) { | |||
if (date == null) date = new java.sql.Date(System.currentTimeMillis()); | |||
return simpleDateFormat.get().format(date); | |||
} | |||
/** | |||
* 得到如下字符串 "HHmmss" | |||
* | |||
* @param date 时间戳 如果传入为NULL,则返回当前日期; | |||
* @return 时间串 | |||
*/ | |||
public static String toTimeString(Date date) { | |||
if (date == null) new java.sql.Date(System.currentTimeMillis()); | |||
synchronized (simpleTimeFormat) { | |||
return simpleTimeFormat.get().format(date); | |||
} | |||
} | |||
/** | |||
* 得到长日期时间字符[yyyyMMddHHmmss] | |||
* | |||
* @param date 时间戳,如果传入为null,则返回当前日期 | |||
* @return 日期时间串 | |||
*/ | |||
public static String toDateTimeString(Date date) { | |||
if (date == null) date = new java.sql.Date(System.currentTimeMillis()); | |||
return simpleDatetimeFormat.get().format(date); | |||
} | |||
public static java.sql.Date toDate(Timestamp datetime) { | |||
if (datetime == null) return null; | |||
return new java.sql.Date(datetime.getTime()); | |||
} | |||
//获取当前日期所处月,却掉了前置的0. | |||
public static String nowMonth() { | |||
String month = nowMonth2(); | |||
if (month.charAt(0) == '0') { | |||
return String.valueOf(month.charAt(1)); | |||
} | |||
return month; | |||
} | |||
/** | |||
* 将字符串转换成Java.sql.Date,不能转换,这返回null | |||
* | |||
* @param src 源串 | |||
* @return java.sql.Date | |||
*/ | |||
public static java.sql.Date toDate(String src) { | |||
try { | |||
if (PubUtil.isEmpty(src)) return null; | |||
return java.sql.Date.valueOf(src); | |||
} catch (Exception e) { | |||
return null; | |||
} | |||
} | |||
/** | |||
* 得到长日期时间字符[yyyyMMddHHmmss] | |||
* | |||
* @param date yyyy-mm-dd hh:mm:ss格式 | |||
* @return | |||
*/ | |||
public static String toDateTimeString(String date) { | |||
return toDateTimeString(toDate(date)); | |||
} | |||
public static String toDateTimeString(Timestamp date) { | |||
return toDateTimeString(toDate(date)); | |||
} | |||
/** | |||
* 得到长日期时间字符[yyyyMMddHHmmss] | |||
* | |||
* @param date yyyy-mm-dd hh:mm:ss格式 | |||
* @return | |||
*/ | |||
public static long toDateTimeLong(String date) { | |||
return Long.parseLong(toDateTimeString(date)); | |||
} | |||
public static long toDateTimeLong(Timestamp date) { | |||
return Long.parseLong(toDateTimeString(date)); | |||
} | |||
/** | |||
* 得到长日期时间字符[yyyyMMddHHmmssS] | |||
* | |||
* @param date 时间戳,如果传入为null,则返回当前日期 | |||
* @return 日期时间串,长 | |||
*/ | |||
public static String toLongDateTimeString(Date date) { | |||
if (date == null) date = new java.sql.Date(System.currentTimeMillis()); | |||
synchronized (simpleLongDatetimeFormat) { | |||
return simpleLongDatetimeFormat.get().format(date); | |||
} | |||
} | |||
// /** | |||
// * 将字符串转换成Java.sql.Date,不能转换,这返回null | |||
// * | |||
// * @param src 源串 | |||
// * @return java.sql.Date | |||
// */ | |||
// public static java.sql.Date toDate(String src) { | |||
// try { | |||
// if (PubUtil.isEmpty(src)) return null; | |||
// return java.sql.Date.valueOf(src); | |||
// } catch (Exception e) { | |||
// return null; | |||
// } | |||
// } | |||
/** | |||
* 用中方格式化年月日 | |||
* | |||
* @param yearMonth YYYYMM | |||
*/ | |||
public static String fmtCNYearMonth(String yearMonth) { | |||
if (PubUtil.getIntIgnoreErr(yearMonth) > 190000) | |||
return String.format("%s年%s月", yearMonth.substring(0, 4), yearMonth.substring(4)); | |||
return ""; | |||
} | |||
/** | |||
* 用中方格式化年月日 | |||
* | |||
* @param yearMonth YYYYMMDD | |||
*/ | |||
public static String fmtCNYearMonthDay(String yearMonth) { | |||
if (PubUtil.isEmptyId(yearMonth) || yearMonth.length() != 8) return yearMonth; | |||
return String.format("%s年%s月%s日", yearMonth.substring(0, 4), yearMonth.substring(4, 6), yearMonth.substring(6)); | |||
} | |||
/** | |||
* 用中方格式化年月日 | |||
* | |||
* @param stdYearMonthDay YYYY-MM-DD | |||
*/ | |||
public static String fmtZHCNYearMonthDay(String stdYearMonthDay) { | |||
return String.format("%s年%s月%s日", stdYearMonthDay.substring(0, 4), stdYearMonthDay.substring(5, 7), stdYearMonthDay.substring(8)); | |||
} | |||
public synchronized static Date getSeasonBegin(Calendar now) { | |||
int curYear = now.get(Calendar.YEAR); | |||
int curMonth = now.get(Calendar.MONTH); | |||
int seasonMonth = m_seasonBeginTable[curMonth]; | |||
Calendar calendar = Calendar.getInstance(); | |||
calendar.set(Calendar.YEAR, curYear); | |||
calendar.set(Calendar.MONTH, seasonMonth); | |||
calendar.set(Calendar.DAY_OF_MONTH, 1); | |||
calendar.set(Calendar.HOUR_OF_DAY, 0); | |||
calendar.set(Calendar.MINUTE, 0); | |||
calendar.set(Calendar.SECOND, 0); | |||
calendar.set(Calendar.MILLISECOND, 0); | |||
calendar.set(Calendar.AM_PM, Calendar.AM); | |||
return new Timestamp(calendar.getTime().getTime()); | |||
} | |||
//季度开始 | |||
public synchronized static Date getSeasonBegin() { | |||
Calendar calendar = Calendar.getInstance(); | |||
calendar.setTime(new Date()); | |||
return getSeasonBegin(calendar); | |||
} | |||
public synchronized static java.sql.Date getAfterDate(Date dt, int days) { | |||
if (dt == null) return null; | |||
GregorianCalendar gc = new GregorianCalendar(); | |||
gc.setTime(dt); | |||
gc.add(GregorianCalendar.DATE, days); | |||
return new java.sql.Date(gc.getTimeInMillis()); | |||
} | |||
//获取季度结束年月,YYYYMM格式 | |||
public static String getSeasonEndYm() { | |||
return getDateYm(getAfterDate(getNextSeasonBegin(), -1)); | |||
} | |||
//获取年度开始年月,YYYYMM格式 | |||
public static String getYearBeginYm() { | |||
return nowYear() + "01"; | |||
} | |||
public static String getYearBeginYm(String ymPeriod) { | |||
try { | |||
return ymPeriod.substring(0, 4) + "01"; | |||
} catch (Exception e) { | |||
return nowYear() + "01"; | |||
} | |||
} | |||
/** | |||
* 获取今日日期串,不带分隔符 | |||
* | |||
* @return 今日日期字符串 yyyyMMdd; | |||
*/ | |||
public static String nowDateString() { | |||
return simpleDateFormat.get().format(nowTimestamp()); | |||
} | |||
//获取年度结束年月,YYYYMM格式 | |||
public static String getYearEndYm() { | |||
return nowYear() + "12"; | |||
} | |||
public static String getYearEndYm(String ymPeriod) { | |||
try { | |||
return ymPeriod.substring(0, 4) + "12"; | |||
} catch (Exception e) { | |||
return nowYear() + "12"; | |||
} | |||
} | |||
/** | |||
* 得到本季度的第一天 | |||
* | |||
* @return | |||
*/ | |||
public static String getSeasonFirstDay() { | |||
return toStdDateString(getSeasonBegin()); | |||
} | |||
public static String getYearFirstDay() { | |||
return nowYear() + "-01-01"; | |||
} | |||
public static String getYearLastDay() { | |||
return nowYear() + "-12-31"; | |||
} | |||
/** | |||
* 取得当月最后一天YYYY-MM-DD | |||
* | |||
* @return 当前月最后一天的日期串; | |||
*/ | |||
public static String getMonthEnd() { | |||
return getAfterDays(getAfterMonth(getMonthStart(), 1), -1); | |||
} | |||
/** | |||
* 取得指定日期所在月最后一天YYYY-MM-DD | |||
* | |||
* @return 所在前月最后一天的日期串; | |||
*/ | |||
public static String getMonthEnd(Date d) { | |||
return getAfterDays(getAfterMonth(getMonthStart(d), 1), -1); | |||
} | |||
/** | |||
* 返回当前标准年月 | |||
* | |||
* @return YYYY-MM | |||
*/ | |||
public static String nowStdYearMonth() { | |||
return nowStdDateString().substring(0, 7); | |||
} | |||
/** | |||
* 取得当月第一天日期串YYYY-MM-DD | |||
* | |||
* @return 月第一天的标准日期串 | |||
*/ | |||
public static String getMonthStart() { | |||
return nowStdYearMonth() + "-01"; | |||
} | |||
/** | |||
* 取得指定日期所在月第一天日期串YYYY-MM-DD | |||
* | |||
* @return 月第一天的标准日期串 | |||
*/ | |||
public static String getMonthStart(Date d) { | |||
if (d == null) return ""; | |||
return toStdDateString(d).substring(0, 7) + "-01"; | |||
} | |||
/** | |||
* 取得当月最后一天 | |||
* | |||
* @return String | |||
*/ | |||
public static String getMonthLastDay() { | |||
return getAfterDays(getAfterMonth(getMonthFirstDay(), 1), -1); | |||
} | |||
/** | |||
* 得到本季度最后一天 | |||
* | |||
* @return | |||
*/ | |||
public static String getSeasonLastDay() { | |||
String s = toStdDateString(getNextSeasonBegin()); | |||
return getAfterDays(s, -1); | |||
} | |||
//下个季度开始 | |||
public synchronized static Date getNextSeasonBegin() { | |||
Calendar calendar = Calendar.getInstance(); | |||
calendar.setTime(new Date()); | |||
return getNextSeasonBegin(calendar); | |||
} | |||
public synchronized static Date getNextSeasonBegin(Calendar now) { | |||
int curYear = now.get(Calendar.YEAR); | |||
int curMonth = now.get(Calendar.MONTH); | |||
int seasonMonth = m_seasonBeginTable[curMonth] + 3; | |||
Calendar calendar = Calendar.getInstance(); | |||
calendar.set(Calendar.YEAR, curYear); | |||
calendar.set(Calendar.MONTH, seasonMonth); | |||
calendar.set(Calendar.DAY_OF_MONTH, 1); | |||
calendar.set(Calendar.HOUR_OF_DAY, 0); | |||
calendar.set(Calendar.MINUTE, 0); | |||
calendar.set(Calendar.SECOND, 0); | |||
calendar.set(Calendar.MILLISECOND, 0); | |||
calendar.set(Calendar.AM_PM, Calendar.AM); | |||
return new Timestamp(calendar.getTime().getTime()); | |||
} | |||
//获取季度开始年月,YYYYMM格式 | |||
public static String getSeasonBeginYm() { | |||
return getDateYm(getSeasonBegin()); | |||
} | |||
//获取年月,YYYYMM格式 | |||
public static String getNowYm() { | |||
return getDateYm(nowTimestamp()); | |||
} | |||
public static String getNextYear(String year, int add) { | |||
if (PubUtil.isEmptyId(year)) return year; | |||
int curYear = Integer.parseInt(year) - 1; | |||
return curYear + ""; | |||
} | |||
//获取上一月,YYYYMM格式 | |||
public static String getPrevYear(String year) { | |||
return getNextYear(year, -1); | |||
} | |||
//获取上一月,YYYYMM格式 | |||
public static String getPrevYm(String period_id) { | |||
return getNextYm(period_id, -1); | |||
} | |||
//获取下一月,YYYYMM格式 | |||
public static String getNextYm(String period_id) { | |||
return getNextYm(period_id, 1); | |||
} | |||
//获取年月,YYYYMM格式 | |||
public static String getPrevYm() { | |||
return getPrevYm(getNowYm()); | |||
} | |||
//获取上一年 YYYY格式 | |||
public static String getPrevYear() { | |||
return getPrevYear(nowYear()); | |||
} | |||
//获取年月,YYYYMM格式 | |||
public static String getNextYm() { | |||
return getNextYm(getNowYm()); | |||
} | |||
public static String getNextYm(String period_id, int add) { | |||
if (PubUtil.isEmptyId(period_id)) return period_id; | |||
int curYear = Integer.parseInt(period_id.substring(0, 4)); | |||
int curMonth = Integer.parseInt(period_id.substring(4)) + add; | |||
if (curMonth > 12) { | |||
do { | |||
curYear++; | |||
curMonth -= 12; | |||
} while (curMonth > 12); | |||
} else if (curMonth <= 0) { | |||
do { | |||
curYear--; | |||
curMonth += 12; | |||
} while (curMonth <= 0); | |||
} | |||
return curYear + StringUtil.getRight("00" + curMonth, 2); | |||
} | |||
/** | |||
* 得到当前日期所在周次的星期一 | |||
* | |||
* @return 返回的时间戳 | |||
*/ | |||
public static Timestamp getWeekStart() { | |||
return getWeekStart(new Timestamp(System.currentTimeMillis())); | |||
} | |||
/** | |||
* 得到日期所在周次的星期一 | |||
* | |||
* @param stamp 参数时间戳,如果传入为null,则返回为null | |||
* @return 返回时间戳 | |||
*/ | |||
public synchronized static Timestamp getWeekStart(Timestamp stamp) { | |||
if (stamp == null) return null; | |||
GregorianCalendar tempCal = new GregorianCalendar(mTimeZone); | |||
tempCal.setTime(stamp); | |||
tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), 0, 0, 0); | |||
if (tempCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { | |||
// AKzz fix bug: | |||
// 如果今天是星期天, 天数再减1,找到上周(外国人的星期天是本周). | |||
tempCal.add(Calendar.DATE, -1); // 再将天数减一 | |||
} | |||
tempCal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 设为星期一. | |||
Timestamp retStamp = new Timestamp(tempCal.getTime().getTime()); | |||
retStamp.setNanos(0); | |||
return retStamp; | |||
} | |||
/** | |||
* 当前日期的下周星期一 | |||
*/ | |||
public static Timestamp getNextWeekStart() { | |||
return getNextWeekStart(new Timestamp(System.currentTimeMillis())); | |||
} | |||
/** | |||
* 当前日期的下周星期一,如果传入null,则返回为null | |||
* | |||
* @param t 要计算的时间戳 | |||
* @return 返回的时间戳 | |||
*/ | |||
public synchronized static Timestamp getNextWeekStart(Timestamp t) { | |||
if (t == null) return null; | |||
Calendar tempCal = new GregorianCalendar(mTimeZone); | |||
tempCal.setTime(t); | |||
tempCal.set(tempCal.get(Calendar.YEAR), tempCal.get(Calendar.MONTH), tempCal.get(Calendar.DAY_OF_MONTH), | |||
// 没有时,分,秒 | |||
0, 0, 0); | |||
tempCal.add(Calendar.WEEK_OF_MONTH, 1); // 周数加1 | |||
tempCal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 设为星期一. | |||
Timestamp retStamp = new Timestamp(tempCal.getTime().getTime()); | |||
retStamp.setNanos(0); | |||
return retStamp; | |||
} | |||
public static String getHalfEndYm(String ymPeriod) { | |||
try { | |||
String year = ymPeriod.substring(0, 4); | |||
String month = ymPeriod.substring(4); | |||
if (month.startsWith("0")) { | |||
int m = Integer.parseInt(month.substring(1)); | |||
if (m > 6) { | |||
ymPeriod = year + "12"; | |||
} else { | |||
ymPeriod = year + "06"; | |||
} | |||
} else { | |||
ymPeriod = year + "12"; | |||
} | |||
return ymPeriod; | |||
} catch (Exception e) { | |||
return ymPeriod; | |||
} | |||
} | |||
public static String getHalfEndYm() { | |||
return getHalfEndYm(DateUtil.getNowYm()); | |||
} | |||
//获取年月,YYYYMM格式 | |||
public static String getDateYm(Date dt) { | |||
String s = simpleDateFormat.get().format(dt); | |||
return s.substring(0, 6); | |||
} | |||
//获取年月,YYYY-MM-DD格式 | |||
public static String getDateYm(String dt) { | |||
if (dt == null || dt.length() < 6) return null; | |||
if (dt.contains("-")) { | |||
return dt.substring(0, 4) + dt.substring(5, 7); | |||
} | |||
return dt.substring(0, 6); | |||
} | |||
public static void main(String[] args) { | |||
System.out.println(getTimesmorning()); | |||
} | |||
@@ -0,0 +1,73 @@ | |||
package cc.smtweb.framework.core.util; | |||
/** | |||
* @Author: zhangyulong | |||
* @Date: 2021/10/13 15:16 | |||
*/ | |||
public class EmojiFilter { | |||
/** | |||
* 过滤emoji 或者 其他非文字类型的字符 | |||
* @param source 需要过滤的字符串 | |||
* @return | |||
*/ | |||
public static String filterEmoji(String source) { | |||
if(PubUtil.isEmpty(source)){ | |||
return ""; | |||
} | |||
if (!containsEmoji(source)) { | |||
return source; | |||
} | |||
StringBuilder buf = null; | |||
int len = source.length(); | |||
for (int i = 0; i < len; i++) { | |||
char codePoint = source.charAt(i); | |||
if (notisEmojiCharacter(codePoint)) { | |||
if (buf == null) { | |||
buf = new StringBuilder(source.length()); | |||
} | |||
buf.append(codePoint); | |||
} | |||
} | |||
if (buf == null) { | |||
return ""; | |||
} else { | |||
if (buf.length() == len) { | |||
buf = null; | |||
return source; | |||
} else { | |||
return buf.toString(); | |||
} | |||
} | |||
} | |||
/** | |||
* 检测是否有emoji字符 | |||
* @param source 需要判断的字符串 | |||
* @return 一旦含有就抛出 | |||
*/ | |||
public static boolean containsEmoji(String source) { | |||
int len = source.length(); | |||
for (int i = 0; i < len; i++) { | |||
char codePoint = source.charAt(i); | |||
if (!notisEmojiCharacter(codePoint)) { | |||
return true; | |||
} | |||
} | |||
return false; | |||
} | |||
/** | |||
* 非emoji表情字符判断 | |||
* @param codePoint | |||
* @return | |||
*/ | |||
public static boolean notisEmojiCharacter(char codePoint) { | |||
return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD) | |||
|| ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) | |||
|| ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)); | |||
} | |||
} |
@@ -0,0 +1,780 @@ | |||
package cc.smtweb.framework.core.util; | |||
import cc.smtweb.framework.core.common.AbstractEnum; | |||
import cc.smtweb.framework.core.common.IntEnum; | |||
import cc.smtweb.framework.core.common.StrEnum; | |||
import org.apache.commons.lang3.StringUtils; | |||
import java.util.List; | |||
/** | |||
* 注意:where参数表示是否已经加入了where关键字 | |||
* | |||
* @author : AKzz | |||
* @version : $Revision:$ | |||
*/ | |||
@SuppressWarnings("UnusedDeclaration") | |||
public abstract class ListSQLParaHelper { | |||
/** | |||
* 补充SQL的字符串的==条件 | |||
* | |||
* @param where_is_exist 是否已经加入了where关键字 | |||
*/ | |||
public static boolean addSql_EQUALE_Expr(boolean where_is_exist, StringBuilder sql, List<Object> args, String db_field_name, int condition_value) { | |||
if (!where_is_exist) { | |||
sql.append(" WHERE "); | |||
} else { | |||
sql.append(" AND "); | |||
} | |||
sql.append(db_field_name).append(" = ? \n"); | |||
args.add(condition_value); | |||
return true; | |||
} | |||
/** | |||
* 补充SQL的字符串的==条件 | |||
* | |||
* @param where_is_exist 是否已经加入了where关键字 | |||
*/ | |||
public static boolean addSql_EQUALE_Expr(boolean where_is_exist, StringBuilder sql, List<Object> args, String db_field_name, String condition_value) { | |||
if (PubUtil.isNotEmptyStr(condition_value)) { | |||
if (!where_is_exist) { | |||
sql.append(" WHERE "); | |||
where_is_exist = true; | |||
} else { | |||
sql.append(" AND "); | |||
} | |||
sql.append(db_field_name).append(" = ? \n"); | |||
args.add(condition_value); | |||
} | |||
return where_is_exist; | |||
} | |||
public static boolean addSql_lesser(boolean where_is_exist, StringBuilder sql, List<Object> args, String db_field_name, String condition_value) { | |||
if (PubUtil.isNotEmptyStr(condition_value)) { | |||
if (!where_is_exist) { | |||
sql.append(" WHERE "); | |||
where_is_exist = true; | |||
} else { | |||
sql.append(" AND "); | |||
} | |||
sql.append(db_field_name).append(" <= ? \n"); | |||
args.add(condition_value); | |||
} | |||
return where_is_exist; | |||
} | |||
public static boolean addSql_greater(boolean where_is_exist, StringBuilder sql, List<Object> args, String db_field_name, String condition_value) { | |||
if (PubUtil.isNotEmptyStr(condition_value)) { | |||
if (!where_is_exist) { | |||
sql.append(" WHERE "); | |||
where_is_exist = true; | |||
} else { | |||
sql.append(" AND "); | |||
} | |||
sql.append(db_field_name).append(" >= ? \n"); | |||
args.add(condition_value); | |||
} | |||
return where_is_exist; | |||
} | |||
/** | |||
* 补充SQL的字符串Like条件(仅后匹配) %s | |||
* | |||
* @param where_is_exist 是否已经加入了where关键字 | |||
*/ | |||
public static boolean addSql_LIKE_Pre(boolean where_is_exist, StringBuilder sql, List<Object> args, String db_field_name, String condition_value) { | |||
if (PubUtil.isNotEmptyStr(condition_value)) { | |||
//mysq _ 是一个占位符需要加\进行处理 | |||
condition_value = condition_value.replaceAll("_", "\\\\_"); | |||
if (!where_is_exist) { | |||
sql.append(" WHERE "); | |||
where_is_exist = true; | |||
} else { | |||
sql.append(" AND "); | |||
} | |||
sql.append(db_field_name).append(" LIKE ? \n"); | |||
args.add(String.format("%%%s", condition_value)); | |||
} | |||
return where_is_exist; | |||
} | |||
public static boolean addSql_LIKE_Pre(boolean where_is_exist, StringBuilder sql, List<Object> args, String[] db_field_names, String condition_value) { | |||
if (PubUtil.isNotEmptyStr(condition_value)) { | |||
//mysq _ 是一个占位符需要加\进行处理 | |||
condition_value = condition_value.replaceAll("_", "\\\\_"); | |||
if (!where_is_exist) { | |||
sql.append(" WHERE "); | |||
where_is_exist = true; | |||
} else { | |||
sql.append(" AND "); | |||
} | |||
sql.append("(").append(db_field_names[0]).append(" LIKE ? \n"); | |||
args.add(String.format("%%%s", condition_value)); | |||
for (int i = 1; i < db_field_names.length; ++i) { | |||
sql.append(" or ").append(db_field_names[i]).append(" LIKE ? \n"); | |||
args.add(String.format("%%%s", condition_value)); | |||
} | |||
sql.append(")"); | |||
} | |||
return where_is_exist; | |||
} | |||
public static boolean addSql_LIKE_EX(boolean where_is_exist, StringBuilder sql, List<Object> args, String condition_value, String... db_field_name) { | |||
if (db_field_name == null || db_field_name.length == 0) return where_is_exist; | |||
if (PubUtil.isNotEmptyStr(condition_value)) { | |||
//mysq _ 是一个占位符需要加\进行处理 | |||
condition_value = condition_value.replaceAll("_", "\\\\_"); | |||
if (!where_is_exist) { | |||
sql.append(" WHERE "); | |||
where_is_exist = true; | |||
} else { | |||
sql.append(" AND "); | |||
} | |||
if (db_field_name.length == 1) { | |||
sql.append(db_field_name[0]).append(" LIKE ? \n"); | |||
args.add(String.format("%%%s%%", condition_value)); | |||
return where_is_exist; | |||
} | |||
sql.append(" ("); | |||
for (String db_filed : db_field_name) { | |||
sql.append(" ").append(db_filed).append(" LIKE ? \nOR"); | |||
args.add(String.format("%%%s%%", condition_value)); | |||
} | |||
sql.delete(sql.length() - 2, sql.length()); | |||
sql.append(")\n"); | |||
} | |||
return where_is_exist; | |||
} | |||
/** | |||
* 补充SQL的字符串Like条件(仅后匹配) s% | |||
* | |||
* @param where_is_exist 是否已经加入了where关键字 | |||
*/ | |||
public static boolean addSql_LIKE_Expr(boolean where_is_exist, StringBuilder sql, List<Object> args, String db_field_name, String condition_value) { | |||
if (PubUtil.isNotEmptyStr(condition_value)) { | |||
//mysq _ 是一个占位符需要加\进行处理 | |||
condition_value = condition_value.replaceAll("_", "\\\\_"); | |||
if (!where_is_exist) { | |||
sql.append(" WHERE "); | |||
where_is_exist = true; | |||
} else { | |||
sql.append(" AND "); | |||
} | |||
sql.append(db_field_name).append(" LIKE ? \n"); | |||
args.add(String.format("%s%%", condition_value)); | |||
} | |||
return where_is_exist; | |||
} | |||
public static boolean addSql_LIKE_Expr(boolean where_is_exist, StringBuilder sql, List<Object> args, String[] db_field_names, String condition_value) { | |||
if (PubUtil.isNotEmptyStr(condition_value)) { | |||
//mysq _ 是一个占位符需要加\进行处理 | |||
condition_value = condition_value.replaceAll("_", "\\\\_"); | |||
if (!where_is_exist) { | |||
sql.append(" WHERE "); | |||
where_is_exist = true; | |||
} else { | |||
sql.append(" AND "); | |||
} | |||
sql.append("(").append(db_field_names[0]).append(" LIKE ? \n"); | |||
args.add(String.format("%s%%", condition_value)); | |||
for (int i = 1; i < db_field_names.length; ++i) { | |||
sql.append(" or ").append(db_field_names[i]).append(" LIKE ? \n"); | |||
args.add(String.format("%s%%", condition_value)); | |||
} | |||
sql.append(")"); | |||
} | |||
return where_is_exist; | |||
} | |||
/** | |||
* 补充SQL的字符串Like条件(全模糊) %s% | |||
* | |||
* @param where_is_exist 是否已经加入了where关键字 | |||
*/ | |||
public static boolean addSql_LIKE(boolean where_is_exist, StringBuilder sql, List<Object> args, String db_field_name, String condition_value) { | |||
if (PubUtil.isNotEmptyStr(condition_value)) { | |||
//mysq _ 是一个占位符需要加\进行处理 | |||
condition_value = condition_value.replaceAll("_", "\\\\_"); | |||
if (!where_is_exist) { | |||
sql.append(" WHERE "); | |||
where_is_exist = true; | |||
} else { | |||
sql.append(" AND "); | |||
} | |||
sql.append(db_field_name).append(" LIKE ? \n"); | |||
args.add(String.format("%%%s%%", condition_value)); | |||
} | |||
return where_is_exist; | |||
} | |||
public static boolean addSql_LIKE(boolean where_is_exist, StringBuilder sql, List<Object> args, String[] db_field_names, String condition_value) { | |||
if (PubUtil.isNotEmptyStr(condition_value)) { | |||
//mysq _ 是一个占位符需要加\进行处理 | |||
condition_value = condition_value.replaceAll("_", "\\\\_"); | |||
if (!where_is_exist) { | |||
sql.append(" WHERE "); | |||
where_is_exist = true; | |||
} else { | |||
sql.append(" AND "); | |||
} | |||
sql.append("(").append(db_field_names[0]).append(" LIKE ? "); | |||
args.add(String.format("%%%s%%", condition_value)); | |||
for (int i = 1; i < db_field_names.length; ++i) { | |||
sql.append(" OR ").append(db_field_names[i]).append(" LIKE ? "); | |||
args.add(String.format("%%%s%%", condition_value)); | |||
} | |||
sql.append(")"); | |||
} | |||
return where_is_exist; | |||
} | |||
/** | |||
* 补充SQL字符串的IN条件 | |||
* | |||
* @param where_is_exist 是否已经加入了where关键字 | |||
*/ | |||
public static boolean addSql_IN_Expr(boolean where_is_exist, StringBuilder sql, String db_field_name, Object... expr) { | |||
if (expr != null && expr.length > 0) { | |||
if (!where_is_exist) { | |||
sql.append(" WHERE "); | |||
where_is_exist = true; | |||
} else { | |||
sql.append(" AND "); | |||
} | |||
sql.append(db_field_name).append(" IN (").append(StringUtils.join(expr, ',')).append(")\n"); | |||
} | |||
return where_is_exist; | |||
} | |||
/** | |||
* 补充SQL字符串的not IN条件 | |||
* | |||
* @param where_is_exist 是否已经加入了where关键字 | |||
*/ | |||
public static boolean addSql_NOTIN_Expr(boolean where_is_exist, StringBuilder sql, String db_field_name, Object... expr) { | |||
if (expr != null && expr.length > 0) { | |||
if (!where_is_exist) { | |||
sql.append(" WHERE "); | |||
where_is_exist = true; | |||
} else { | |||
sql.append(" AND "); | |||
} | |||
sql.append(db_field_name).append(" NOT IN (").append(StringUtils.join(expr, ',')).append(")\n"); | |||
} | |||
return where_is_exist; | |||
} | |||
/** | |||
* 增加字符串字段条件,可能多选 | |||
* | |||
* @param field 查询的字段名 | |||
* @param value 值,包含','为多选 | |||
* @param sql sql | |||
* @param paras 参数 | |||
*/ | |||
public static void addStrConMulti(String field, String value, StringBuilder sql, List<Object> paras) { | |||
if (PubUtil.isEmpty(value)) | |||
return; | |||
if (value.contains(",")) {//多选 | |||
sql.append(" and ").append(field).append(" in ('").append(value.replaceAll(",", "','")).append("')"); | |||
} else if (!Consts.NULL_STRING.equals(value)) {//单 | |||
sql.append(" and ").append(field).append("=?"); | |||
paras.add(value); | |||
} | |||
} | |||
/** | |||
* 增加字符串字段条件,不判断多选 | |||
* | |||
* @param field 查询的字段名 | |||
* @param value 值 | |||
* @param paras 参数 | |||
*/ | |||
public static void addStrConSingle(String field, String value, StringBuilder sb, List<Object> paras) { | |||
addStrConSingle(field, "=", value, sb, paras); | |||
} | |||
/** | |||
* 增加字符串字段条件,不判断多选 | |||
* | |||
* @param field 查询的字段名 | |||
* @param opt 逻辑运算符 | |||
* @param value 值 | |||
* @param paras 参数 | |||
*/ | |||
public static void addStrConSingle(String field, String opt, String value, StringBuilder sb, List<Object> paras) { | |||
if (PubUtil.isNotEmpty(value) && !Consts.NULL_STRING.equals(value)) {//单 | |||
sb.append(" and ").append(field).append(" ").append(opt).append(" ? \n"); | |||
paras.add(value); | |||
} | |||
} | |||
/** | |||
* 增加字符串字段条件,包含 | |||
* | |||
* @param field 查询的字段名 | |||
* @param value 值 | |||
* @param paras 参数 | |||
*/ | |||
public static void addStrConSingleLike(String field, String value, StringBuilder sb, List<Object> paras) { | |||
if (PubUtil.isNotEmpty(value) && !Consts.NULL_STRING.equals(value)) {//单 | |||
sb.append(" and ").append(field).append(" like ? \n"); | |||
paras.add("%" + value + "%"); | |||
} | |||
} | |||
/** | |||
* 增加字符串字段条件并且不区分大小写,包含 | |||
* | |||
* @param field 查询的字段名 | |||
* @param value 值 | |||
* @param paras 参数 | |||
*/ | |||
public static void addStrConSingleLikeIgnoreCase(String field, String value, StringBuilder sb, List<Object> paras) { | |||
if (PubUtil.isNotEmpty(value) && !Consts.NULL_STRING.equals(value)) { | |||
String valueLower = value.toLowerCase(); | |||
sb.append(" and ").append("lower(").append(field).append(")").append(" like ? \n"); | |||
paras.add("%" + valueLower + "%"); | |||
} | |||
} | |||
/** | |||
* 增加数字字段条件,可能多选 | |||
* | |||
* @param field 查询的字段名 | |||
* @param value 值,包含','为多选 | |||
* @param paras 参数 | |||
*/ | |||
public static void addNumberConMulti(String field, String value, StringBuilder sb, List<Object> paras) { | |||
if (PubUtil.isEmptyId(value)) | |||
return; | |||
if (value.contains(",")) {//多选 | |||
sb.append(" and ").append(field).append(" in (").append(value).append(") \n"); | |||
} else if (!Consts.NULL_STRING.equals(value)) {//单 | |||
sb.append(" and ").append(field).append("=? \n"); | |||
paras.add(value); | |||
} | |||
} | |||
/** | |||
* 增加数字字段条件,单选 | |||
* | |||
* @param field 查询的字段名 | |||
* @param value 值 | |||
* @param paras 参数 | |||
*/ | |||
public static void addNumConSingle(String field, Number value, StringBuilder sb, List<Object> paras) { | |||
addNumConSingle(field, "=", value, sb, paras); | |||
} | |||
/** | |||
* 增加数字字段条件,单选 | |||
* | |||
* @param field 查询的字段名 | |||
* @param opt 运算符 | |||
* @param value 值 | |||
* @param paras 参数 | |||
*/ | |||
public static void addNumConSingle(String field, String opt, Number value, StringBuilder sb, List<Object> paras) { | |||
sb.append(" and ").append(field).append(opt).append("? \n"); | |||
paras.add(value); | |||
} | |||
/** | |||
* 增加日期字段条件,可能多选 | |||
* | |||
* @param field 查询的字段名 | |||
* @param value 值 | |||
* @param paras 参数 | |||
*/ | |||
@Deprecated | |||
public static void addDateCon(String field, String value, StringBuilder sb, List<Object> paras) { | |||
if (PubUtil.isEmpty(value)) | |||
return; | |||
String[] st = value.split(","); | |||
if (st.length == 0 || Consts.NULL_STRING.equals(st[0])) {//无 | |||
return; | |||
} | |||
String s = PubUtil.getCurDateStr(); | |||
if (st.length > 0) { | |||
switch (st[0]) { | |||
case "D": //当天 考虑日期时间的情况 | |||
sb.append(" and ").append(field).append(">='").append(s).append("' and ").append(field).append("<'").append(DateUtil.getAfterDays(s, 1)).append("' \n"); | |||
break; | |||
case "W": //本周 | |||
s = DateUtil.toStdDateString(DateUtil.getWeekStart()); | |||
sb.append(" and ").append(field).append(">='").append(s).append("' and ").append(field).append("<'").append(DateUtil.getAfterDays(s, 8)).append("' \n"); | |||
break; | |||
case "M": //本月 | |||
sb.append(" and ").append(field).append(">='").append(DateUtil.getMonthStart()).append("' and ").append(field).append("<='").append(DateUtil.getMonthEnd()).append(" 23:59:59' \n"); | |||
break; | |||
case "Q": //本季度 | |||
sb.append(" and ").append(field).append(">='").append(DateUtil.getSeasonFirstDay()).append("' and ").append(field).append("<='").append(DateUtil.getSeasonLastDay()).append(" 23:59:59' \n"); | |||
break; | |||
case "Y": //本年 | |||
sb.append(" and ").append(field).append(">='").append(DateUtil.getYearFirstDay()).append("' and ").append(field).append("<='").append(DateUtil.getYearLastDay()).append(" 23:59:59' \n"); | |||
break; | |||
default: //自定义 | |||
String df, de; | |||
if (st.length > 1) | |||
df = st[1]; | |||
else | |||
df = ""; | |||
if (st.length > 2) | |||
de = st[2]; | |||
else | |||
de = ""; | |||
if (PubUtil.isEmpty(df) && PubUtil.isEmpty(de)) | |||
return; | |||
if (PubUtil.isEmpty(df)) { | |||
sb.append(" and ").append(field).append("<=?"); | |||
paras.add(de); | |||
} else if (PubUtil.isEmpty(de)) { | |||
sb.append(" and ").append(field).append(">=?"); | |||
paras.add(df); | |||
} else { | |||
sb.append(" and ").append(field).append(">=? and ").append(field).append("<=? \n"); | |||
paras.add(df); | |||
paras.add(de); | |||
} | |||
break; | |||
} | |||
} | |||
} | |||
/** | |||
* 增加日期字段条件,可能多选(huangxl:日期格式YYYYMMDD) | |||
* | |||
* @param field 查询的字段名 | |||
* @param value 值 | |||
* @param paras 参数 | |||
*/ | |||
public static void addDateConEx(String field, String value, StringBuilder sb, List<Object> paras) { | |||
if (PubUtil.isEmpty(value)) | |||
return; | |||
String[] st = value.split(","); | |||
if (st.length == 0 || Consts.NULL_STRING.equals(st[0])) {//无 | |||
return; | |||
} | |||
String s = PubUtil.getCurDateStr(); | |||
if (st.length > 0) { | |||
switch (st[0]) { | |||
case "D": //当天 | |||
sb.append(" and ").append(field).append("=").append(PubUtil.getDateLong(s)).append(" \n"); | |||
break; | |||
case "W": //本周 | |||
s = DateUtil.toStdDateString(DateUtil.getWeekStart()); | |||
sb.append(" and ").append(field).append(" between ").append(PubUtil.getDateLong(s)).append(" and ").append(PubUtil.getDateLong(DateUtil.getAfterDays(s, 7))).append(" \n"); | |||
break; | |||
case "M": //本月 | |||
sb.append(" and ").append(field).append(" between ").append(PubUtil.getDateLong(DateUtil.getMonthStart())).append(" and ").append(PubUtil.getDateLong(DateUtil.getMonthEnd())).append(" \n"); | |||
break; | |||
case "Q": //本季度 | |||
sb.append(" and ").append(field).append(" between ").append(PubUtil.getDateLong(DateUtil.getSeasonFirstDay())).append(" and ").append(PubUtil.getDateLong(DateUtil.getSeasonLastDay())).append(" \n"); | |||
break; | |||
case "Y": //本年 | |||
sb.append(" and ").append(field).append(" between ").append(PubUtil.getDateLong(DateUtil.getYearFirstDay())).append(" and ").append(PubUtil.getDateLong(DateUtil.getYearLastDay())).append(" \n"); | |||
break; | |||
default: //自定义 | |||
String df, de; | |||
if (st.length > 1) | |||
df = st[1]; | |||
else | |||
df = ""; | |||
if (st.length > 2) | |||
de = st[2]; | |||
else | |||
de = ""; | |||
if (PubUtil.isEmpty(df) && PubUtil.isEmpty(de)) | |||
return; | |||
if (PubUtil.isEmpty(df)) { | |||
sb.append(" and ").append(field).append("<=?"); | |||
paras.add(PubUtil.getDateLong(de)); | |||
} else if (PubUtil.isEmpty(de)) { | |||
sb.append(" and ").append(field).append(">=?"); | |||
paras.add(PubUtil.getDateLong(df)); | |||
} else { | |||
sb.append(" and ").append(field).append(" between ? and ? \n"); | |||
paras.add(PubUtil.getDateLong(df)); | |||
paras.add(PubUtil.getDateLong(de)); | |||
} | |||
break; | |||
} | |||
} | |||
} | |||
/** | |||
* 增加期间查询日期字段条件,可能多选(huangxl:日期格式YYYYMMDD) | |||
* | |||
* @param field 查询的字段名 | |||
* @param value 值 | |||
* @param paras 参数 | |||
*/ | |||
public static void addPeriodDateConEx(String field, String value, StringBuilder sb, List<Object> paras) { | |||
if (PubUtil.isEmpty(value)) | |||
return; | |||
String[] st = value.split(","); | |||
if (st.length == 0 || Consts.NULL_STRING.equals(st[0])) {//无 | |||
return; | |||
} | |||
String df, de; | |||
if (st.length > 1) | |||
df = st[1]; | |||
else | |||
df = ""; | |||
if (st.length > 2) | |||
de = st[2]; | |||
else | |||
de = ""; | |||
sb.append(" and ").append(field).append(" between ? and ? \n"); | |||
paras.add(df + "01"); | |||
paras.add(de + "31"); | |||
} | |||
/** | |||
* 增加日期时间字段条件 | |||
* | |||
* @param field 查询的字段名 | |||
* @param value 值 | |||
* @param paras 参数 | |||
*/ | |||
public static void addDateTimeConEx(String field, String value, StringBuilder sb, List<Object> paras) { | |||
if (PubUtil.isEmpty(value)) | |||
return; | |||
String[] st = value.split(","); | |||
if (st.length == 0 || Consts.NULL_STRING.equals(st[0])) {//无 | |||
return; | |||
} | |||
String s = PubUtil.getCurDateStr(); | |||
if (st.length > 0) { | |||
switch (st[0]) { | |||
case "D": //当天 | |||
sb.append(" and ").append(field).append(" between ").append(PubUtil.getDateTimeLong(s)).append(" and ").append(PubUtil.getEndDateTimeLong(s)).append(" \n"); | |||
break; | |||
case "W": //本周 | |||
s = DateUtil.toStdDateString(DateUtil.getWeekStart()); | |||
sb.append(" and ").append(field).append(" between ").append(PubUtil.getDateTimeLong(s)).append(" and ").append(PubUtil.getEndDateTimeLong(DateUtil.getAfterDays(s, 6))).append(" \n"); | |||
break; | |||
case "M": //本月 | |||
sb.append(" and ").append(field).append(" between ").append(PubUtil.getDateTimeLong(DateUtil.getMonthStart())).append(" and ").append(PubUtil.getEndDateTimeLong(DateUtil.getMonthEnd())).append(" \n"); | |||
break; | |||
case "Q": //本季度 | |||
sb.append(" and ").append(field).append(" between ").append(PubUtil.getDateTimeLong(DateUtil.getSeasonFirstDay())).append(" and ").append(PubUtil.getEndDateTimeLong(DateUtil.getSeasonLastDay())).append(" \n"); | |||
break; | |||
case "Y": //本年 | |||
sb.append(" and ").append(field).append(" between ").append(PubUtil.getDateTimeLong(DateUtil.getYearFirstDay())).append(" and ").append(PubUtil.getEndDateTimeLong(DateUtil.getYearLastDay())).append(" \n"); | |||
break; | |||
default: //自定义 | |||
String df, de; | |||
if (st.length > 1) | |||
df = st[1]; | |||
else | |||
df = ""; | |||
if (st.length > 2) | |||
de = st[2]; | |||
else | |||
de = ""; | |||
if (PubUtil.isEmpty(df) && PubUtil.isEmpty(de)) | |||
return; | |||
if (PubUtil.isEmpty(df)) { | |||
sb.append(" and ").append(field).append("<=?"); | |||
paras.add(PubUtil.getEndDateTimeLong(de)); | |||
} else if (PubUtil.isEmpty(de)) { | |||
sb.append(" and ").append(field).append(">=?"); | |||
paras.add(PubUtil.getDateTimeLong(df)); | |||
} else { | |||
sb.append(" and ").append(field).append(" between ? and ? \n"); | |||
paras.add(PubUtil.getDateTimeLong(df)); | |||
paras.add(PubUtil.getEndDateTimeLong(de)); | |||
} | |||
break; | |||
} | |||
} | |||
} | |||
/** | |||
* 枚举状态字段,in方式 | |||
* | |||
* @param where_is_exist sql是否已包括where | |||
* @param field sql字段名 | |||
* @param value 前端传回来的查询值,格式:1,1,1,1 | |||
* @param intEnum 枚举类型 | |||
* @param sql 待构建的sql | |||
*/ | |||
public static void addIntEnumCon(boolean where_is_exist, String field, String value, IntEnum intEnum, StringBuilder sql) { | |||
if (PubUtil.isNotEmpty(value) && value.contains("1") && value.contains("0")) { | |||
if (!where_is_exist) { | |||
sql.append(" WHERE "); | |||
where_is_exist = true; | |||
} else { | |||
sql.append(" AND "); | |||
} | |||
String s = ""; | |||
String[] ss = value.split(","); | |||
int i = 0; | |||
for (AbstractEnum.IntEnumBean b : intEnum.values()) { | |||
if (ss.length > i && "1".equals(ss[i])) s += "," + b.value; | |||
i++; | |||
} | |||
sql.append(field + " in (" + s.substring(1) + ")"); | |||
} | |||
} | |||
/** | |||
* 枚举状态字段,in方式 | |||
* | |||
* @param where_is_exist sql是否已包括where | |||
* @param field sql字段名 | |||
* @param value 前端传回来的查询值,格式:1,1,1,1 | |||
* @param intEnum 枚举类型 | |||
* @param sql 待构建的sql | |||
*/ | |||
public static void addStrEnumCon(boolean where_is_exist, String field, String value, StrEnum intEnum, StringBuilder sql) { | |||
if (PubUtil.isNotEmpty(value) && value.contains("1") && value.contains("0")) { | |||
if (!where_is_exist) { | |||
sql.append(" WHERE "); | |||
where_is_exist = true; | |||
} else { | |||
sql.append(" AND "); | |||
} | |||
String s = ""; | |||
String[] ss = value.split(","); | |||
int i = 0; | |||
for (AbstractEnum.StrEnumBean b : intEnum.values()) { | |||
if (ss.length > i && "1".equals(ss[i])) s += ",'" + b.value + "'"; | |||
i++; | |||
} | |||
sql.append(field + " in (" + s.substring(1) + ")"); | |||
} | |||
} | |||
public static void addTreeStateCon(String curPrefix, String value, String tableName, StringBuilder sb, List<Object> paras) { | |||
if (PubUtil.isNotEmpty(value) && !Consts.NULL_STRING.equals(value) && !value.contains(",")) { | |||
int v = PubUtil.getIntIgnoreErr(value); | |||
if (v == 0) {//查启用 自己启用,且父亲未停用 | |||
sb.append(" and ").append(curPrefix).append(".f_status=0 and not exists(select 1 from ").append(tableName).append(" p__ where p__.f_status=1 and ").append(curPrefix).append(".f_level_code like p__.f_level_code || '-%')"); | |||
} else {//查停用 自己停用 或父亲被停用 | |||
sb.append(" and (").append(curPrefix).append(".f_status=1 or exists(select 1 from ").append(tableName).append(" p__ where p__.f_status=1 and ").append(curPrefix).append(".f_level_code like p__.f_level_code || '-%'))"); | |||
} | |||
} | |||
} | |||
/** | |||
* 增加年月字段条件 | |||
* | |||
* @param field 查询的字段名 | |||
* @param value 值 | |||
* @param paras 参数 | |||
*/ | |||
public static void addYmCon(String field, String value, StringBuilder sb, List<Object> paras) { | |||
addYmConEx(field, field, value, sb, paras); | |||
} | |||
public static void addYmConEx(String field_begin, String field_end, String value, StringBuilder sb, List<Object> paras) { | |||
if (PubUtil.isEmpty(value)) | |||
return; | |||
String[] st = value.split(","); | |||
if (st.length == 0 || Consts.NULL_STRING.equals(st[0])) {//无 | |||
return; | |||
} | |||
String s = PubUtil.getCurDateStr(); | |||
if (st.length > 1) { | |||
switch (st[0]) { | |||
case "M": //本月 | |||
String nowYm = DateUtil.getNowYm(); | |||
sb.append(" and ").append(field_end).append(">='").append(DateUtil.getNowYm()).append("' and ").append(field_begin).append("<='").append(nowYm).append("'"); | |||
break; | |||
case "Q": //本季度 | |||
sb.append(" and ").append(field_end).append(">='").append(DateUtil.getSeasonBeginYm()).append("' and ").append(field_begin).append("<='").append(DateUtil.getSeasonEndYm()).append("'"); | |||
break; | |||
case "Y": //本年 | |||
sb.append(" and ").append(field_end).append(">='").append(DateUtil.getYearBeginYm()).append("' and ").append(field_begin).append("<='").append(DateUtil.getYearEndYm()).append("'"); | |||
break; | |||
default: //自定义 | |||
String df, de; | |||
if (st.length > 1) | |||
df = st[1]; | |||
else | |||
df = ""; | |||
if (st.length > 2) | |||
de = st[2]; | |||
else | |||
de = ""; | |||
if (PubUtil.isEmpty(df) && PubUtil.isEmpty(de)) | |||
return; | |||
if (PubUtil.isEmpty(df)) { | |||
sb.append(" and ").append(field_begin).append("<=?"); | |||
paras.add(de); | |||
} else if (PubUtil.isEmpty(de)) { | |||
sb.append(" and ").append(field_end).append(">=?"); | |||
paras.add(df); | |||
} else { | |||
sb.append(" and ").append(field_end).append(">=? and ").append(field_begin).append("<=?"); | |||
paras.add(df); | |||
paras.add(de); | |||
} | |||
break; | |||
} | |||
} else if (st.length == 1) { | |||
sb.append(" and ").append(field_end).append(">='").append(value).append("' and ").append(field_begin).append("<='").append(value).append("'"); | |||
} | |||
} | |||
//构建年月范围的sql | |||
public static String getYmConSql(String field, String value) { | |||
return getYmConSqlEx(field, field, value); | |||
} | |||
public static String getYmConSqlEx(String field_begin, String field_end, String value) { | |||
if (PubUtil.isEmpty(value)) return ""; | |||
String[] st = value.split(","); | |||
if (st.length == 0 || Consts.NULL_STRING.equals(st[0])) return ""; | |||
String s = PubUtil.getCurDateStr(); | |||
StringBuilder sb = new StringBuilder(128); | |||
switch (st[0]) { | |||
case "M": //本月 | |||
String nowYm = DateUtil.getNowYm(); | |||
sb.append(" and ").append(field_end).append(">='").append(DateUtil.getNowYm()).append("' and ").append(field_begin).append("<='").append(nowYm).append("'"); | |||
break; | |||
case "Q": //本季度 | |||
sb.append(" and ").append(field_end).append(">='").append(DateUtil.getSeasonBeginYm()).append("' and ").append(field_begin).append("<='").append(DateUtil.getSeasonEndYm()).append("'"); | |||
break; | |||
case "Y": //本年 | |||
sb.append(" and ").append(field_end).append(">='").append(DateUtil.getYearBeginYm()).append("' and ").append(field_begin).append("<='").append(DateUtil.getYearEndYm()).append("'"); | |||
break; | |||
default: //自定义 | |||
String df, de; | |||
if (st.length > 1) | |||
df = st[1]; | |||
else | |||
df = ""; | |||
if (st.length > 2) | |||
de = st[2]; | |||
else | |||
de = ""; | |||
if (PubUtil.isEmpty(df) && PubUtil.isEmpty(de)) | |||
return ""; | |||
if (PubUtil.isEmpty(df)) { | |||
sb.append(" and ").append(field_begin).append("<='").append(de).append("'"); | |||
} else if (PubUtil.isEmpty(de)) { | |||
sb.append(" and ").append(field_end).append(">='").append(df).append("'"); | |||
} else { | |||
sb.append(" and ").append(field_end).append(">='").append(df).append("' and ").append(field_begin).append("<='").append(de).append("'"); | |||
} | |||
break; | |||
} | |||
return sb.substring(5); | |||
} | |||
} |
@@ -0,0 +1,463 @@ | |||
package cc.smtweb.framework.core.util; | |||
import java.math.BigDecimal; | |||
import java.math.RoundingMode; | |||
import java.text.DecimalFormat; | |||
/** | |||
* Created with IntelliJ IDEA. | |||
* User: AKhh | |||
* Date: 12-12-25 下午1:02 | |||
* To change this template use File | Settings | File Templates. | |||
*/ | |||
public class MathUtil { | |||
//整数 | |||
private static DecimalFormat dfLng = new DecimalFormat("##############0"); | |||
private static DecimalFormat dfLong = new DecimalFormat("###,###,###,###,##0"); | |||
//一位小数 | |||
private static DecimalFormat df1 = new DecimalFormat("##############0.0"); | |||
private static DecimalFormat df1Format = new DecimalFormat("###,###,###,###,##0.0"); | |||
//两位小数 | |||
private static DecimalFormat df2 = new DecimalFormat("##############0.00"); | |||
private static DecimalFormat df2Format = new DecimalFormat("###,###,###,###,##0.00"); | |||
//四位小数 | |||
private static DecimalFormat df4 = new DecimalFormat("###,###,###,###,##0.0000"); | |||
//六位小数 | |||
private static DecimalFormat df6Number = new DecimalFormat("#######################0.000000"); | |||
private static DecimalFormat df6NumberF = new DecimalFormat("#,###,###,###,###,###,##0.000000"); | |||
public final static DecimalFormat stdAmountFormat = new DecimalFormat("###,###,###,###,##0.00"); | |||
public final static DecimalFormat stdNumberFormat = new DecimalFormat("#0.00"); | |||
public final static String DEF_NUM_TEN_THOUSAND = "10000";//万 | |||
public static final double MAX_VALUE = 9999999999999.99D; | |||
public static final double MIN_VALUE = -9999999999999.99D; | |||
private final static BigDecimal ONE_BIG = new BigDecimal(1.00D); | |||
private static final String UNIT = "万仟佰拾亿仟佰拾万仟佰拾元角分"; | |||
private static final String DIGIT = "零壹贰叁肆伍陆柒捌玖"; | |||
/** | |||
* 4舍5入double,2位小数 | |||
*/ | |||
public static double roundDouble(double src) { | |||
return roundDouble(src, 2); | |||
} | |||
/** | |||
* 4舍5入double,N 位小数 | |||
* @param src | |||
* @param scale 小数位数 | |||
* @return | |||
*/ | |||
public static double roundDouble(Object src, int scale) { | |||
if (src == null) return 0.0; | |||
String v = src.toString(); | |||
if (PubUtil.isEmpty(v)) return 0.0; | |||
if (scale < 0) scale = 2; | |||
BigDecimal src_b = new BigDecimal(v); | |||
BigDecimal src_v = src_b.divide(ONE_BIG, scale + 2, BigDecimal.ROUND_HALF_UP);// 4舍5入 | |||
src_v = src_v.divide(ONE_BIG, scale, BigDecimal.ROUND_HALF_UP);// 4舍5入 | |||
return src_v.doubleValue(); | |||
} | |||
/** | |||
* 舍位处理,原生floor有坑,部分浮点数记录为.99999999999形式,导致floor结果错误 | |||
* @param d | |||
* @return | |||
*/ | |||
public static double floor(double d) { | |||
return Math.floor(roundDouble(d, 2)); | |||
} | |||
/** | |||
* 比较两Double是否相等,将会吧他们专程BigDecimal进行比较; | |||
* | |||
* @param src double1 | |||
* @param tag double2 | |||
* @return src > tag 返回1, src < tag 返回-1, 否则返回0 | |||
*/ | |||
public static int compare(double src, double tag) { | |||
BigDecimal src_b = new BigDecimal(src); | |||
BigDecimal src_v = src_b.divide(ONE_BIG, 2, BigDecimal.ROUND_HALF_UP);// 4舍5入 | |||
BigDecimal tag_b = new BigDecimal(tag); | |||
BigDecimal tag_v = tag_b.divide(ONE_BIG, 2, BigDecimal.ROUND_HALF_UP);// 4舍5入 | |||
return src_v.compareTo(tag_v); | |||
} | |||
/** | |||
* 自动过滤金额中的逗号转换为double,如果出错,则返回0 | |||
* | |||
* @param s 源串,可能为带逗号的金额串; | |||
* @return double | |||
*/ | |||
public static Double toDouble(String s) { | |||
return todouble(s); | |||
} | |||
/** | |||
* 自动过滤金额中的逗号转换为double,如果出错,则返回0 | |||
* | |||
* @param s 源串,可能为带逗号的金额串; | |||
* @return double | |||
*/ | |||
public static double todouble(String s) { | |||
try { | |||
return Double.parseDouble(s.replaceAll(",", "")); | |||
} catch (Exception e) { | |||
return 0.00; | |||
} | |||
} | |||
/** | |||
* 获取double,主要过滤d为null的情况; | |||
* | |||
* @param d Double对象; | |||
* @return double | |||
*/ | |||
public static double todouble(Double d) { | |||
if (d == null) return 0.0d; | |||
return d.doubleValue(); | |||
} | |||
/** | |||
* 自动过滤金额中的逗号转换为float,如果出错,则返回0 | |||
* | |||
* @param s 源串,可能为带逗号的金额串; | |||
* @return Float | |||
*/ | |||
public static Float toFloat(String s) { | |||
return tofloat(s); | |||
} | |||
/** | |||
* 自动过滤金额中的逗号转换为float,如果出错,则返回0 | |||
* | |||
* @param s 源串,可能为带逗号的金额串; | |||
* @return Float | |||
*/ | |||
public static float tofloat(String s) { | |||
try { | |||
return Float.parseFloat(s.replaceAll(",", "")); | |||
} catch (Exception e) { | |||
return 0.0f; | |||
} | |||
} | |||
public static long tolong(String src, long defaultvalue) { | |||
try { | |||
return Long.parseLong(src); | |||
} catch (Exception e) { | |||
return defaultvalue; | |||
} | |||
} | |||
public static int toint(String src, int defaultvalue) { | |||
try { | |||
return Integer.parseInt(src); | |||
} catch (Exception e) { | |||
return defaultvalue; | |||
} | |||
} | |||
/** | |||
* 考虑使用中的精度,判断一个Value是否>0,实际是>0.00001 | |||
* | |||
* @param value double类型 | |||
* @return boolean | |||
*/ | |||
public static boolean isBigThanZero(double value) { | |||
return (value > 0.00001); | |||
} | |||
/** | |||
* 考虑使用中的精度,判断一个Value是否>0,实际是>0.00001 | |||
* | |||
* @param value String类型 | |||
* @return boolean | |||
*/ | |||
public static boolean isBigThanZero(String value) { | |||
return !PubUtil.isEmpty(value) && isBigThanZero(toDouble(value)); | |||
} | |||
/** | |||
* 考虑使用中的精度,判断一个Value是否=0,实际是给出一个值范围。 | |||
* | |||
* @param value double类型 | |||
* @return boolean | |||
*/ | |||
public static boolean isEqualsZero(double value) { | |||
return (-0.00001 < value && value < 0.00001); | |||
} | |||
/** | |||
* 考虑使用中的精度,判断一个Value是否=0,实际是给出一个值范围。 | |||
* | |||
* @param value String类型 | |||
* @return boolean | |||
*/ | |||
public static boolean isEqualsZero(String value) { | |||
return PubUtil.isEmpty(value) || isEqualsZero(toDouble(value)); | |||
} | |||
/** | |||
* 是否是负数 | |||
* | |||
* @param db_val 要判断的double | |||
* @return 负数则返回true; | |||
*/ | |||
public static boolean isNegative(double db_val) { | |||
return (compare(db_val, 0.00D) == -1); | |||
} | |||
/** | |||
* 是否是正数 | |||
* | |||
* @param db_val 要判断的double | |||
* @return 正数则返回true; | |||
*/ | |||
public static boolean isPlus(double db_val) { | |||
return (compare(db_val, 0.00D) == 1); | |||
} | |||
/** | |||
* 得到金额字符串,保持小数点2位 | |||
* | |||
* @param db 将double转换为金额字符串; | |||
* @return 金额字符串#0.00; | |||
*/ | |||
public static String toStdNumberString(double db) { | |||
try { | |||
return stdNumberFormat.format(db); | |||
} catch (Exception e) { | |||
return "0.00"; | |||
} | |||
} | |||
public static String toStdNumberStringEx(double db) { | |||
try { | |||
if (compare(db, -1d) == 0) return "-"; | |||
return stdNumberFormat.format(db); | |||
} catch (Exception e) { | |||
return "0.00"; | |||
} | |||
} | |||
/** | |||
* 将金额格式字符串,如23,333,093.01 去掉逗号 | |||
* | |||
* @param s 金额串 | |||
* @return String 去掉逗号后的串,如果amount为空,则返回0.00 | |||
*/ | |||
public static String toStdNumberString(String s) { | |||
if (PubUtil.isEmpty(s)) | |||
return "0.00"; | |||
return stdNumberFormat.format(todouble(s)); | |||
} | |||
/** | |||
* 将数据转换为两位小数的数字格式; | |||
* | |||
* @param d 数据 | |||
* @param isZeroToEmpty 如果未0,是否返回“”; | |||
* @return 两位小数的字符串; | |||
*/ | |||
public static String toStdNumberString(double d, boolean isZeroToEmpty) { | |||
if (isEqualsZero(d)) { | |||
return isZeroToEmpty ? "": "0.00"; | |||
} | |||
return stdNumberFormat.format(d); | |||
} | |||
public static String toStdNumberString(String s, boolean isZeroToEmpty) { | |||
return toStdNumberString(todouble(s), isZeroToEmpty); | |||
} | |||
public static String toStdNumberString(double d, int scale) { | |||
DecimalFormat dfn = null; | |||
if (scale == 1) dfn = df1Format; | |||
if (scale == 2) dfn = df2Format; | |||
else if (scale == 4) dfn = df4; | |||
else if (scale == 6) dfn = df6NumberF; | |||
else if (scale <= 0) dfn = dfLong; | |||
else { | |||
StringBuilder sb = new StringBuilder("###,###,###,###,##0."); | |||
for (int i = 0; i < scale; i++) sb.append("0"); | |||
dfn = new DecimalFormat(sb.toString()); | |||
} | |||
return dfn.format(d); | |||
} | |||
/** | |||
* 将数字乘100,保留小数点后两位, 然后后面添加% | |||
* | |||
* @param d 值 | |||
* @param isZeroToEmpty,如果值为0,是否返回空; | |||
* @return 字符串; | |||
*/ | |||
public static String toStdPercentNumberStr(double d, boolean isZeroToEmpty) { | |||
if (d > -0.00000000001 && d < 0.00000000001) { | |||
return isZeroToEmpty ? "": "0.00%"; | |||
} | |||
return toStdNumberString(d * 100) + "%"; | |||
} | |||
public static String toStdAmountString(double d) { | |||
return toStdAmountString(d, false); | |||
} | |||
/** | |||
* 将数据转换为两位小数的金额格式,带逗号; | |||
* | |||
* @param d 数据 | |||
* @param isZeroToEmpty 如果未0,是否返回“”; | |||
* @return 金额格式的字符串; | |||
*/ | |||
public static String toStdAmountString(double d, boolean isZeroToEmpty) { | |||
if (isEqualsZero(d)) { | |||
return isZeroToEmpty ? "": "0.00"; | |||
} | |||
return stdAmountFormat.format(d); | |||
} | |||
public static String toStdAmountString(String s) { | |||
return toStdAmountString(todouble(s), false); | |||
} | |||
public static String toStdAmountString(String s, boolean isZeroToEmpty) { | |||
return toStdAmountString(todouble(s), isZeroToEmpty); | |||
} | |||
/** | |||
* 将小写金额转换为人民币大写金额 | |||
* | |||
* @param s 金额格式的串 | |||
* @return String 转换结果 | |||
*/ | |||
public static String toCapsAmountString(String s) { | |||
if (PubUtil.isEmpty(s)) return ""; | |||
return toCapsAmountString(todouble(s)); | |||
} | |||
/** | |||
* 将小写金额转换为人民币大写金额 | |||
* | |||
* @param v double | |||
* @return String 转换结果 | |||
*/ | |||
public static String toCapsAmountString(double v) { | |||
if (v < MIN_VALUE || v > MAX_VALUE) return "参数非法!"; | |||
boolean negative = isNegative(v); | |||
if (negative) v = Math.abs(v); | |||
long l = Math.round(v * 100); | |||
if (l == 0) return "零元整"; | |||
String strValue = String.valueOf(l); | |||
// i用来控制数 | |||
int i = 0; | |||
// j用来控制单位 | |||
int j = UNIT.length() - strValue.length(); | |||
StringBuilder rs = new StringBuilder(32); | |||
boolean isZero = false; | |||
for (; i < strValue.length(); i++, j++) { | |||
char ch = strValue.charAt(i); | |||
if (ch == '0') { | |||
isZero = true; | |||
if (UNIT.charAt(j) == '亿' || UNIT.charAt(j) == '万' || UNIT.charAt(j) == '元') { | |||
rs.append(UNIT.charAt(j)); | |||
isZero = false; | |||
} | |||
} else { | |||
if (isZero) { | |||
rs.append('零'); | |||
isZero = false; | |||
} | |||
rs.append(DIGIT.charAt(ch - '0')).append(UNIT.charAt(j)); | |||
} | |||
} | |||
if (rs.charAt(rs.length() - 1) != '分') | |||
rs.append('整'); | |||
i = rs.indexOf("亿万"); | |||
if (i > 0) rs.delete(i + 1, i + 2); // i+1 ->万 | |||
if (negative) | |||
return rs.insert(0, '负').toString(); | |||
else | |||
return rs.toString(); | |||
} | |||
/** | |||
* 返回0 到 maxvalue的随机数 | |||
* | |||
* @param maxvalue 随机数的最大值 | |||
* @return int | |||
*/ | |||
public static int rnd(int maxvalue) { | |||
return (int) (Math.random() * (maxvalue + 1)); | |||
} | |||
public static double chkDbNull(Double v) { | |||
return v == null ? 0: v; | |||
} | |||
public static double max(double d1, double d2) { | |||
return compare(d1, d2) < 0 ? d2 : d1; | |||
} | |||
public static double min(double d1, double d2) { | |||
return compare(d1, d2) < 0 ? d1 : d2; | |||
} | |||
public static void main(String[] args) { | |||
double aa=123456789.345678900005; | |||
System.out.println("args0 = " + upDouble(aa,0)); | |||
System.out.println("args1 = " + upDouble(aa,1)); | |||
System.out.println("args2 = " + upDouble(aa,2)); | |||
System.out.println("args3 = " + upDouble(aa,3)); | |||
System.out.println("args4 = " + upDouble(aa,4)); | |||
System.out.println("args5 = " + upDouble(aa,5)); | |||
System.out.println("args5 = " + upDouble(aa,6)); | |||
System.out.println("args5 = " + upDouble(aa,7)); | |||
System.out.println("args5 = " + upDouble(aa,8)); | |||
} | |||
/** | |||
* double 去尾法 | |||
* @param src 待处理数据 | |||
* @param scale 保留小数位数 | |||
* @return | |||
*/ | |||
public static double cutDouble(double src, int scale){ | |||
String v = toStdNumberString(src, 6);//先到6位小数,再去计算,否则容易出错,如8.3成8.29999999999,舍位就成了8.29了 | |||
DecimalFormat formater = new DecimalFormat(); | |||
formater.setMaximumFractionDigits(scale); | |||
formater.setGroupingSize(0); | |||
formater.setRoundingMode(RoundingMode.FLOOR); | |||
return new BigDecimal(formater.format(toDouble(v))).doubleValue(); | |||
} | |||
/** | |||
* double 进位法 | |||
* @param src 待处理数据 | |||
* @param scale 保留小数位数 | |||
* @return | |||
*/ | |||
public static double upDouble(double src, int scale){ | |||
String v = toStdNumberString(src, 6);//先到6位小数,再去计算 | |||
DecimalFormat formater = new DecimalFormat(); | |||
formater.setMaximumFractionDigits(scale); | |||
formater.setGroupingSize(0); | |||
formater.setRoundingMode(RoundingMode.UP); | |||
return new BigDecimal(formater.format(toDouble(v))).doubleValue(); | |||
} | |||
} |
@@ -0,0 +1,377 @@ | |||
package cc.smtweb.framework.core.util; | |||
import java.util.HashMap; | |||
import java.util.Map; | |||
import java.util.regex.Pattern; | |||
/** | |||
* 个人身份证帮助类 | |||
* | |||
* @author : AKzz | |||
* @version : $Revision:$ | |||
*/ | |||
public final class PersonUIDUtil { | |||
private final static String uid_chars = "0123456789XZ"; | |||
private static Map<String, String> map_uid_chars; | |||
static { | |||
map_uid_chars = new HashMap<>(); | |||
map_uid_chars.put("0", "0"); | |||
map_uid_chars.put("1", "1"); | |||
map_uid_chars.put("2", "2"); | |||
map_uid_chars.put("3", "3"); | |||
map_uid_chars.put("4", "4"); | |||
map_uid_chars.put("5", "5"); | |||
map_uid_chars.put("6", "6"); | |||
map_uid_chars.put("7", "7"); | |||
map_uid_chars.put("8", "8"); | |||
map_uid_chars.put("9", "9"); | |||
map_uid_chars.put("X", "X"); | |||
map_uid_chars.put("x", "X"); | |||
map_uid_chars.put("Z", "Z"); | |||
map_uid_chars.put("z", "Z"); | |||
} | |||
/* | |||
大陆的身份证为18位,老的身份证是15位。 | |||
地址码(身份证前六位)第一、第二数字表示公民所在地的省份(或自治区、直辖市); | |||
第三、第四位数字表示公民 所在地的市(或州); | |||
第五、第六位数字表示公民所在地的县(或县级市); | |||
生日期码(身份证第七位到第十四位)接下来的8位数字表示公民的出生年、月、日; | |||
顺序码(身份证第十五位到十七位)为同一地址码所标识的区域范围内,对同年、月、日出生的人员编定的顺序号 | |||
第17位:奇数为男性,偶数为女性) | |||
第十八位数字的计算方法为: | |||
1.将前面的身份证号码17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 | |||
2.将这17位数字和系数相乘的结果相加。 | |||
3.用加出来和除以11,看余数是多少? | |||
4.余数只可能有0 1 2 3 4 5 6 7 8 9 10这11个数字。其分别对应的最后一位身份证的号码为'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' | |||
*/ | |||
public final static int NEW_CARD_NUMBER_LENGTH = 18; | |||
public final static int OLD_CARD_NUMBER_LENGTH = 15; | |||
// 18位身份证中,各个数字的生成校验码时的权值 | |||
private final static int[] VERIFY_CODE_WEIGHT = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; | |||
// 18位身份证中最后一位校验码 | |||
private final static char[] VERIFY_CODE = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'}; | |||
public static boolean checkPersonUID(String uid) { | |||
return checkPersonUIDA(uid, false); | |||
} | |||
/** | |||
* 检查身份证是否合法. | |||
*/ | |||
public static boolean checkPersonUIDA(String uid, boolean include15) { | |||
String uid_15_ex = "^\\d{6}\\d{2}(0[1-9]|10|11|12)(0[1-9]|1[0-9]|2[0-9]|3[0-1])\\d{3}$"; | |||
String uid_18_ex = "^\\d{6}(19|20)\\d{2}(0[1-9]|10|11|12)(0[1-9]|1[0-9]|2[0-9]|3[0-1])\\d{3}[0-9X]{1}$"; | |||
String regx, bdate; | |||
if (include15 && uid.length() == 15) { | |||
regx = uid_15_ex; // 15位 地址码 + (年+月+日) + 顺序号 | |||
bdate = "19" + uid.substring(6, 12); | |||
} else if (uid.length() == NEW_CARD_NUMBER_LENGTH) { | |||
regx = uid_18_ex; // 18位 身份证 | |||
bdate = uid.substring(6, 14); | |||
} else { | |||
return false; | |||
} | |||
if (!DateUtil.isDateFormat(bdate, "YYYYMMDD")) return false; | |||
Pattern pattern = Pattern.compile(regx); | |||
boolean ret = pattern.matcher(uid).matches(); | |||
if (ret && uid.length() == NEW_CARD_NUMBER_LENGTH) { | |||
return calculateVerifyCode(uid) == uid.charAt(NEW_CARD_NUMBER_LENGTH - 1); | |||
} | |||
return ret; | |||
} | |||
/** | |||
* 检查身份证是否合法 | |||
* | |||
* @param uid 要检查的身份证号; | |||
* @return 对于可以转换的自动验证转换为正确的身份证号,如18位51012719731204001X,则能自动转换为正确的,15位自动转换为18位,返回的message中。 | |||
*/ | |||
public static boolean checkPersonUIDEx(String uid) { | |||
return checkPersonUIDExA(uid, false); | |||
} | |||
public static boolean checkPersonUIDExA(String uid, boolean include15) { | |||
String uid_15_ex = "^\\d{6}\\d{2}(0[1-9]|10|11|12)(0[1-9]|1[0-9]|2[0-9]|3[0-1])\\d{3}$"; | |||
String uid_18_ex = "^\\d{6}(19|20)\\d{2}(0[1-9]|10|11|12)(0[1-9]|1[0-9]|2[0-9]|3[0-1])\\d{3}[0-9A-Z]{1}$"; | |||
String regx, bdate; //11011720031031501Z | |||
if (include15 && uid.length() == 15) { | |||
regx = uid_15_ex; // 15位 地址码 + (年+月+日) + 顺序号 | |||
bdate = "19" + uid.substring(6, 12); | |||
} else if (uid.length() == NEW_CARD_NUMBER_LENGTH) { | |||
regx = uid_18_ex; // 18位 身份证 | |||
bdate = uid.substring(6, 14); | |||
} else { | |||
return false; | |||
} | |||
if (!DateUtil.isDateFormat(bdate, "YYYYMMDD")) { | |||
return false; | |||
} | |||
Pattern pattern = Pattern.compile(regx); | |||
boolean ret = pattern.matcher(uid).matches(); | |||
if (!ret) { | |||
return false; | |||
} | |||
if (uid.length() == NEW_CARD_NUMBER_LENGTH) {//18位 | |||
char c = calculateVerifyCode(uid); | |||
if (c == uid.charAt(NEW_CARD_NUMBER_LENGTH - 1)) | |||
return true;//正确 | |||
return false;//错误 | |||
} else {//15位则换算成18位 | |||
return true; | |||
} | |||
} | |||
/** | |||
* 校验码(第十八位数): | |||
* 十七位数字本体码加权求和公式 S = Sum(Ai * Wi), i = 0...16 ,先对前17位数字的权求和; | |||
* Ai:表示第i位置上的身份证号码数字值 Wi:表示第i位置上的加权因子 Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 | |||
* 2; | |||
* 计算模 Y = mod(S, 11)< | |||
* 通过模得到对应的校验码 Y: 0 1 2 3 4 5 6 7 8 9 10 校验码: 1 0 X 9 8 7 6 5 4 3 2 | |||
*/ | |||
public static char calculateVerifyCode(CharSequence uid) { | |||
int sum = 0; | |||
for (int i = 0; i < NEW_CARD_NUMBER_LENGTH - 1; i++) { | |||
char ch = uid.charAt(i); | |||
sum += (ch - '0') * VERIFY_CODE_WEIGHT[i]; | |||
} | |||
return VERIFY_CODE[sum % 11]; | |||
} | |||
/** | |||
* 获取身份证的第17位,奇数为男性,偶数为女性 | |||
* | |||
* @param uid 身份证号; | |||
* @return 0: 女; 1.男; | |||
*/ | |||
public static int getPersonSex(CharSequence uid) { | |||
char genderCode; | |||
if (uid.length() == NEW_CARD_NUMBER_LENGTH) | |||
genderCode = uid.charAt(NEW_CARD_NUMBER_LENGTH - 2); | |||
/*else if (uid.length() == OLD_CARD_NUMBER_LENGTH) | |||
genderCode = uid.charAt(OLD_CARD_NUMBER_LENGTH - 1);*/ | |||
else | |||
return -1;//throw new IllegalArgumentException("非法的居民身份证号[" + uid + "]!"); | |||
return (genderCode - '0' & 0x1) == 0x01 ? 1 : 0; | |||
} | |||
/** | |||
* 从身份证中取得出身日期 | |||
*/ | |||
public static String getBirthday(CharSequence uid) { | |||
StringBuilder birthday = new StringBuilder(10); | |||
/*if (uid.length() == OLD_CARD_NUMBER_LENGTH) { | |||
birthday.append("19").append(uid.subSequence(6, 8)).append('-'); | |||
birthday.append(uid.subSequence(8, 10)).append('-'); | |||
birthday.append(uid.subSequence(10, 12)); | |||
} else */ | |||
if (uid.length() == NEW_CARD_NUMBER_LENGTH) { | |||
birthday.append(uid.subSequence(6, 10)).append('-'); | |||
birthday.append(uid.subSequence(10, 12)).append('-'); | |||
birthday.append(uid.subSequence(12, 14)); | |||
} else { | |||
throw new IllegalArgumentException("非法的居民身份证号[" + uid + "]!"); | |||
} | |||
return birthday.toString(); | |||
} | |||
/** | |||
* 把15位身份证号码转换到18位身份证号码<br> | |||
* 15位身份证号码与18位身份证号码的区别为:<br> | |||
* 1、15位身份证号码中,"出生年份"字段是2位,转换时需要补入"19",表示20世纪<br> | |||
* 2、15位身份证无最后一位校验码。18位身份证中,校验码根据根据前17位生成 | |||
*/ | |||
public static String contertToNewCardNumber(String oldUID) { | |||
if (oldUID.length() != 15) { | |||
char c = calculateVerifyCode(oldUID); | |||
return oldUID.substring(0, NEW_CARD_NUMBER_LENGTH - 1) + c; | |||
} | |||
StringBuilder buf = new StringBuilder(NEW_CARD_NUMBER_LENGTH); | |||
buf.append(oldUID.substring(0, 6)); | |||
buf.append("19"); | |||
buf.append(oldUID.substring(6)); | |||
buf.append(calculateVerifyCode(buf)); | |||
return buf.toString().toUpperCase(); | |||
} | |||
/** | |||
* 根据身份证和传入的年月,计算人员年龄; | |||
* | |||
* @param person_uid 身份证号 | |||
* @param curYear 年度 | |||
* @param curMonth 月份 | |||
*/ | |||
public static int getPersonAge(String person_uid, int curYear, int curMonth) { | |||
String birth_date = getBirthday(person_uid); | |||
int year = Integer.parseInt(birth_date.substring(0, 4)); | |||
int month = Integer.parseInt(birth_date.substring(5, 7)); | |||
int age = curYear - year; | |||
if (curMonth < month) | |||
age--; | |||
return age; | |||
} | |||
public static int getPersonAge(String person_uid, String period) { | |||
int y = Integer.parseInt(period.substring(0, 4)); | |||
int m = Integer.parseInt(period.substring(4)); | |||
return getPersonAge(person_uid, y, m); | |||
} | |||
/** | |||
* 根据身份证和传入的年月,计算人员年龄; | |||
* | |||
* @param person_uid 身份证号 | |||
* @param nowDate 比较日期 2017-01-01 | |||
*/ | |||
public static int getPersonAgeEx(String person_uid, String nowDate, int ageType) { | |||
int y = Integer.parseInt(nowDate.substring(0, 4)); | |||
int m = Integer.parseInt(nowDate.substring(5, 7)); | |||
int d = Integer.parseInt(nowDate.substring(8, 10)); | |||
String birth_date = getBirthday(person_uid); | |||
int year = Integer.parseInt(birth_date.substring(0, 4)); | |||
int month = Integer.parseInt(birth_date.substring(5, 7)); | |||
int day = Integer.parseInt(birth_date.substring(8, 10)); | |||
int age = y - year; | |||
if (ageType == 0) { | |||
if (m < month) age--; | |||
} else if (ageType == 4) { | |||
if (m < month || (m == month && d < day)) age--; | |||
else age++; | |||
} else if (ageType == 2) { | |||
age++; | |||
} else if (ageType == 3) { | |||
age--; | |||
} | |||
return age; | |||
} | |||
public static int getPersonAge(String person_uid, String period, int ageType) { | |||
int y = Integer.parseInt(period.substring(0, 4)); | |||
int m = Integer.parseInt(period.substring(4)); | |||
String birth_date = getBirthday(person_uid); | |||
int year = Integer.parseInt(birth_date.substring(0, 4)); | |||
int month = Integer.parseInt(birth_date.substring(5, 7)); | |||
int age = y - year; | |||
if (ageType == 0) { | |||
if (m < month) age--; | |||
} else if (ageType == 2) { | |||
age++; | |||
} else if (ageType == 3) { | |||
age--; | |||
} | |||
return age; | |||
} | |||
public static int getPersonAge(String person_uid, int year, int month, int ageType) { | |||
String birth_date = getBirthday(person_uid); | |||
int y = Integer.parseInt(birth_date.substring(0, 4)); | |||
int m = Integer.parseInt(birth_date.substring(5, 7)); | |||
int age = year - y; | |||
if (ageType == 0) { | |||
if (month < m) age--; | |||
} else if (ageType == 2) { | |||
age++; | |||
} else if (ageType == 3) { | |||
age--; | |||
} | |||
return age; | |||
} | |||
//计算年龄,按年算 | |||
public static int getPersonAgeYear(String person_uid, int curYear) { | |||
String birth_date = getBirthday(person_uid); | |||
int year = Integer.parseInt(birth_date.substring(0, 4)); | |||
return curYear - year; | |||
} | |||
public static int getPersonAgeYear(String person_uid, String period) { | |||
int y = Integer.parseInt(period.substring(0, 4)); | |||
return getPersonAgeYear(person_uid, y); | |||
} | |||
/** | |||
* 根据身份证,根据当前年月,计算人员年龄; | |||
* | |||
* @param person_uid 身份证号 | |||
*/ | |||
public static int getPersonAge(String person_uid) { | |||
String birth_date = getBirthday(person_uid); | |||
int year = Integer.parseInt(birth_date.substring(0, 4)); | |||
int month = Integer.parseInt(birth_date.substring(5, 7)); | |||
int age = Integer.parseInt(DateUtil.nowYear()) - year; | |||
if (Integer.parseInt(DateUtil.nowMonth()) < month) | |||
age--; | |||
return age; | |||
} | |||
//去掉非法字符 | |||
public static String cleanPersonUid(String personUid) { | |||
if (personUid == null) return ""; | |||
personUid = personUid.trim().toUpperCase(); | |||
StringBuilder sb = new StringBuilder(personUid.length()); | |||
for (int len = personUid.length(), i = 0; i < len; i++) { | |||
char c = personUid.charAt(i); | |||
if (c == 'X' || (c >= '0' && c <= '9')) sb.append(c); | |||
} | |||
return sb.toString(); | |||
} | |||
//去掉非法字符,含z | |||
public static String cleanPersonUidEx(String personUid) { | |||
if (personUid == null) return ""; | |||
personUid = personUid.trim().toUpperCase(); | |||
StringBuilder sb = new StringBuilder(18); | |||
String s; | |||
for (int len = personUid.length(), i = 0; i < len; i++) { | |||
char c = personUid.charAt(i); | |||
if (uid_chars.indexOf(c) >= 0) sb.append(c); | |||
else { | |||
s = String.valueOf(c); | |||
if (map_uid_chars.containsKey(s)) sb.append(map_uid_chars.get(s)); | |||
} | |||
} | |||
return sb.toString(); | |||
} | |||
/** | |||
* 显示uid的时候保密,用一些**符号 | |||
* @param person_uid | |||
* @return | |||
*/ | |||
public static String secretPersonUID(String person_uid){ | |||
return secretPersonUID(person_uid,8); | |||
} | |||
public static String secretPersonUID(String person_uid,int secretLength){ | |||
if(person_uid.length()>14){ | |||
String head = person_uid.substring(0,6); | |||
String tail = person_uid.substring(14,person_uid.length()); | |||
StringBuilder sb = new StringBuilder(); | |||
sb.append(head); | |||
int i=0; | |||
while (i<secretLength){ | |||
sb.append("*"); | |||
i++; | |||
} | |||
sb.append(tail); | |||
return sb.toString(); | |||
}else return person_uid; | |||
} | |||
public static void main(String[] args) { | |||
} | |||
} |
@@ -0,0 +1,779 @@ | |||
package cc.smtweb.framework.core.util; | |||
import lombok.extern.slf4j.Slf4j; | |||
import org.apache.commons.lang3.ArrayUtils; | |||
import org.apache.commons.lang3.StringUtils; | |||
import java.io.UnsupportedEncodingException; | |||
import java.sql.Date; | |||
import java.sql.Timestamp; | |||
import java.text.Collator; | |||
import java.text.DecimalFormat; | |||
import java.text.SimpleDateFormat; | |||
import java.util.*; | |||
import java.util.function.Function; | |||
import java.util.regex.Matcher; | |||
import java.util.regex.Pattern; | |||
/** | |||
* Created with IntelliJ IDEA. | |||
* User: AKhh | |||
* Date: 12-12-26 下午11:55 | |||
* 基础工具类; | |||
*/ | |||
@SuppressWarnings("UnusedDeclaration") | |||
@Slf4j | |||
public class PubUtil { | |||
private static SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd"); | |||
private static SimpleDateFormat tFormat = new SimpleDateFormat("HH:mm:ss"); | |||
private static SimpleDateFormat dtFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | |||
private static SimpleDateFormat dLFormat = new SimpleDateFormat("yyyyMMddHHmmss"); | |||
private static SimpleDateFormat dLFormat2 = new SimpleDateFormat("yyyyMMdd"); | |||
private static DecimalFormat df2Format = new DecimalFormat("###,###,###,###,##0.00"); | |||
private static DecimalFormat df2 = new DecimalFormat("##############0.00"); | |||
private static DecimalFormat df = new DecimalFormat("#.##"); | |||
public static boolean isEmpty(String str) { | |||
return str == null || str.trim().length() == 0; | |||
} | |||
//判断 是否支持GBK | |||
public static boolean supportGBK(String str){ | |||
if(str == null){ | |||
return true; | |||
} | |||
try { | |||
return str.equals ( new String (str.getBytes ("gbk"),"gbk") ); | |||
}catch (Exception e){ | |||
log.error ( "判断【"+str+"】是否支持GBK时出现异常:"+e.getMessage () ); | |||
e.printStackTrace (); | |||
return false; | |||
} | |||
} | |||
public static boolean isEmpty(Collection collection) { | |||
return collection == null || collection.size() == 0; | |||
} | |||
public static boolean isEmpty(Object[] o) { | |||
return o == null || o.length == 0; | |||
} | |||
public static boolean isEmptyStr(String str) { | |||
return str == null || str.trim().length() == 0 || "-".equals(str) || "-1".equals(str); | |||
} | |||
//判断Id是否为空id,有可能是"-","-1" | |||
public static boolean isEmptyId(String id) { | |||
return id == null || id.trim().length() == 0 || "-".equals(id) || "-1".equals(id) || "null".equals(id) || "(无)".equals(id); | |||
} | |||
public static boolean isNotEmpty(String str) { | |||
return !isEmpty(str); | |||
} | |||
public static boolean isNotEmptyId(String str) { | |||
return !isEmptyId(str); | |||
} | |||
public static boolean isNotEmptyStr(String str) { | |||
return !isEmptyStr(str); | |||
} | |||
public static boolean isNotEmpty(Collection collection) { | |||
return !isEmpty(collection); | |||
} | |||
public static boolean isNotEmpty(Object[] o) { | |||
return !isEmpty(o); | |||
} | |||
/** | |||
* 获取浮点数(有错误默认为0),可以识别金额中的逗号格式 | |||
* | |||
* @param str 带转换的字符串 | |||
* @return 浮点数 | |||
*/ | |||
public static double getDoubleIgnoreErr(String str) { | |||
if (str == null) | |||
return 0.0; | |||
str = str.trim(); | |||
if (str.equals("")) | |||
return 0.0; | |||
str = str.replaceAll(",", "").replaceAll(",", ""); | |||
try { | |||
return Double.valueOf(str); | |||
} catch (Exception e) { | |||
return 0.0; | |||
} | |||
} | |||
/** | |||
* 得到int 获取转换的int值,有错返回0 | |||
* | |||
* @param str 带转换的字符串 | |||
* @return int | |||
*/ | |||
public static int getIntIgnoreErr(String str) { | |||
return getIntIgnoreErr(str, 0); | |||
} | |||
public static int getIntIgnoreErr(String str, int defValue) { | |||
if (str == null) | |||
return defValue; | |||
str = str.trim(); | |||
if (str.equals("")) | |||
return defValue; | |||
str = str.replaceAll(",", "").replaceAll(",", ""); | |||
if (str.contains(".")) | |||
str = str.substring(0, str.indexOf('.')); | |||
try { | |||
return Integer.valueOf(str); | |||
} catch (Exception e) { | |||
return defValue; | |||
} | |||
} | |||
/** | |||
* 得到Long 获取转换的Long值,有错返回0 | |||
* | |||
* @param str 带转换的字符串 | |||
* @return long | |||
*/ | |||
public static long getLongIgnoreErr(String str) { | |||
return getLongIgnoreErr(str, 0L); | |||
} | |||
public static long getLongIgnoreErr(String str, long defValue) { | |||
if (str == null) | |||
return defValue; | |||
str = str.trim(); | |||
if (str.equals("")) | |||
return defValue; | |||
str = str.replaceAll(",", "").replaceAll(",", ""); | |||
try { | |||
return Long.valueOf(str); | |||
} catch (Exception e) { | |||
return defValue; | |||
} | |||
} | |||
public static String getDateStrIgnoreNull(Timestamp obj) { | |||
if (obj == null) | |||
return ""; | |||
return dFormat.format(obj); | |||
} | |||
public static String getTimeStrIgnoreNull(Timestamp obj) { | |||
if (obj == null) | |||
return ""; | |||
return tFormat.format(obj); | |||
} | |||
public static Date getCurDate() { | |||
return new Date(System.currentTimeMillis()); | |||
} | |||
public static String getCurDateStr() { | |||
return dFormat.format(new Date(System.currentTimeMillis())); | |||
} | |||
public static Timestamp getCurDatetime() { | |||
return new Timestamp(System.currentTimeMillis()); | |||
} | |||
public static String getCurDatetimeStr() { | |||
return dtFormat.format(new Timestamp(System.currentTimeMillis())); | |||
} | |||
public static String getCurDatetimeShortStr() { | |||
return dLFormat.format(new Timestamp(System.currentTimeMillis())); | |||
} | |||
/** | |||
* Func:为空,返回"" | |||
* | |||
* @param str 带检查的字符串 | |||
* @return "" | |||
*/ | |||
public static String checkNull(String str) { | |||
if (isEmpty(str)) | |||
return ""; | |||
return str.trim(); | |||
} | |||
/** | |||
* 功能:null过滤,返回默认值 | |||
* | |||
* @param strValue 带检查的字符串 | |||
* @param defaultValue 为空返回的字符串 | |||
* @return str | |||
*/ | |||
public static String checkNull(String strValue, String defaultValue) { | |||
return strValue == null ? defaultValue : strValue; | |||
} | |||
public static String checkNull(Integer n) { | |||
return checkNull(n, true); | |||
} | |||
public static String checkNull(Integer n, boolean zeroToEmpty) { | |||
int v; | |||
if (n == null || n == 0) | |||
v = 0; | |||
else | |||
v = n; | |||
return v != 0 ? String.valueOf(v) : zeroToEmpty ? "" : "0"; | |||
} | |||
public static String checkNull(Long n) { | |||
return checkNull(n, 0L); | |||
} | |||
public static String checkNull(Long n, long defValue) { | |||
return n != null ? String.valueOf(n) : String.valueOf(defValue); | |||
} | |||
public static String checkNull(Double n) { | |||
return checkNull(n, 0d); | |||
} | |||
public static String checkNull(Double n, boolean zeroToEmpty) { | |||
double v; | |||
if (n == null || n == 0) | |||
v = 0; | |||
else | |||
v = n; | |||
return v != 0 ? String.valueOf(v) : zeroToEmpty ? "" : "0"; | |||
} | |||
public static String checkNull(Double n, double defValue) { | |||
double d = n != null ? n : defValue; | |||
return getDoubleStr(d); | |||
} | |||
//字符串清洗 | |||
public static String clearStr(String str) { | |||
if (str == null) return ""; | |||
else | |||
str = str.replaceAll("\u3000", "").replaceAll("\n", "").replaceAll("\b", "").replaceAll("\r", "").replaceAll("\t", "").replaceAll("\\s", "").replaceAll(" ", "").trim(); | |||
return str; | |||
} | |||
/** | |||
* Func:取当前年 | |||
* | |||
* @return "" | |||
*/ | |||
public static String getThisYear() { | |||
Calendar tlpDate = new GregorianCalendar(); | |||
tlpDate.setTime(getCurDate()); | |||
return String.valueOf(tlpDate.get(Calendar.YEAR)); | |||
} | |||
/** | |||
* Func:取当前月 | |||
* | |||
* @return "" | |||
*/ | |||
public static String getThisMonth() { | |||
Calendar tlpDate = new GregorianCalendar(); | |||
tlpDate.setTime(getCurDate()); | |||
return String.valueOf(tlpDate.get(Calendar.MONTH)); | |||
} | |||
/** | |||
* 功能:空值过滤,返回默认值 | |||
* | |||
* @param strValue 待检查的字符串 | |||
* @param defaultValue 为空返回的字符串 | |||
* @return str | |||
*/ | |||
public static String checkEmpty(String strValue, String defaultValue) { | |||
return isEmpty(strValue) ? defaultValue : strValue; | |||
} | |||
/** | |||
* 功能:空值过滤,返回默认值 | |||
* | |||
* @param strValue 待检查的字符串 | |||
* @return str | |||
*/ | |||
public static String checkEmptyId(String strValue, String defaultValue) { | |||
return isEmptyId(strValue) ? defaultValue : strValue; | |||
} | |||
public static String checkEmptyId(String strValue) { | |||
return isEmptyId(strValue) ? "" : strValue; | |||
} | |||
public static String getOrigMsg(Throwable e) { | |||
String s = e.getMessage(); | |||
Throwable t = e.getCause(); | |||
while (t != null) { | |||
s = t.getMessage(); | |||
t = t.getCause(); | |||
} | |||
return s; | |||
} | |||
//计算一个字符串source的长度(英文和数字占一个长度,其他字符(中文和特殊符号等)占2个长度) | |||
public static int strLength(String source) { | |||
int totalLength = source.length();// 总长度 | |||
String otherStr = source.replaceAll("\\d|\\w", "");// 去掉字符串中的数字和英文字符 | |||
int otherLength = otherStr.length();// 占2个长度的字符 | |||
return (totalLength - otherLength) + otherLength * 2; | |||
} | |||
/** | |||
* 格式化最后修改时间 | |||
* | |||
* @param strTime YYYYMMDDhhmmss | |||
* @return YYYY-MM-DD hh:mm:ss | |||
*/ | |||
public static String checkLastTime(String strTime) { | |||
if (isEmpty(strTime)) | |||
return ""; | |||
return checkLastTime(getLongIgnoreErr(strTime)); | |||
} | |||
public static String checkLastTime(long lngTime) { | |||
if (0 >= lngTime) | |||
return ""; | |||
String s1 = String.valueOf(lngTime + "").trim(); | |||
if (s1.length() != 14) | |||
return ""; | |||
return s1.substring(0, 4).concat("-").concat(s1.substring(4, 6)).concat("-").concat(s1.substring(6, 8)).concat(" ").concat(s1.substring(8, 10)).concat(":").concat(s1.substring(10, 12)).concat(":").concat(s1.substring(12)); | |||
} | |||
/** | |||
* 格式化最后修改时间 | |||
* | |||
* @param strTime YYYYMMDDhhmmss | |||
* @return YYYY-MM-DD hh:mm:ss | |||
*/ | |||
public static String getTimeFormatValue(String strTime) { | |||
if (isEmpty(strTime)) | |||
return ""; | |||
return getTimeFormatValue(getTimeDbValue(strTime)); | |||
} | |||
/** | |||
* 日期格式化 YYYYMMDDHHMMSS 转 YYYY-MM-DD- HH:MM:SS | |||
* | |||
* @param lngTime | |||
* @return | |||
*/ | |||
public static String getTimeFormatValue(long lngTime) { | |||
if (lngTime <= 0L) return ""; | |||
String s1 = String.valueOf(lngTime + "").trim(); | |||
if (s1.length() == 8) {//日期 | |||
return s1.substring(0, 4).concat("-").concat(s1.substring(4, 6)).concat("-").concat(s1.substring(6, 8)); | |||
} | |||
if (s1.length() == 6) {//时间 | |||
return s1.substring(0, 2).concat(":").concat(s1.substring(2, 4)).concat(":").concat(s1.substring(4, 6)); | |||
} | |||
if (s1.length() == 14) {//日期 | |||
return s1.substring(0, 4).concat("-").concat(s1.substring(4, 6)).concat("-").concat(s1.substring(6, 8)).concat(" ").concat(s1.substring(8, 10)).concat(":").concat(s1.substring(10, 12)).concat(":").concat(s1.substring(12)); | |||
} | |||
return ""; | |||
} | |||
/** | |||
* 日期格式化 YYYY-MM-DD- HH:MM:SS 转 YYYYMMDDHHMMSS | |||
* | |||
* @param strTime | |||
* @return | |||
*/ | |||
public static long getTimeDbValue(String strTime) { | |||
if (isEmptyId(strTime)) return -1L; | |||
return getLongIgnoreErr(strTime.replaceAll("-", "").replaceAll(":", "").replaceAll(" ", "")); | |||
} | |||
/** | |||
* 取最后修改时间 | |||
* | |||
* @return Long | |||
*/ | |||
public static Long getLastTime() { | |||
return DateUtil.nowDateTimeNumber(); | |||
} | |||
/** | |||
* 取最后修改日期 | |||
* | |||
* @return | |||
*/ | |||
public static Long getLastDate() { | |||
return Long.valueOf(dLFormat2.format(new Timestamp(System.currentTimeMillis()))); | |||
} | |||
public static String getAmountStr(double amount) { | |||
return df2Format.format(amount); | |||
} | |||
public static String getAmountStrEx(double amount) { | |||
return df.format(amount); | |||
} | |||
public static String getSingleAmountStr(double amount) { | |||
return df2.format(amount); | |||
} | |||
public static String getIntStr(int d) { | |||
if (d == 0) | |||
return ""; | |||
else | |||
return String.valueOf(d); | |||
} | |||
/** | |||
* 截取右边的0串 | |||
* | |||
* @param codeStr | |||
* @return | |||
*/ | |||
public static String cutZero(String codeStr) { | |||
int j = 0; | |||
int len = codeStr.length() - 1; | |||
for (int i = len; i > -1; i--) { | |||
if ('0' == codeStr.charAt(i)) { | |||
j++; | |||
} else { | |||
break; | |||
} | |||
} | |||
return codeStr.substring(0, len - j + 1); | |||
} | |||
/** | |||
* 右边补0串 | |||
* | |||
* @param codeLen | |||
* @param cutedCode | |||
* @return | |||
*/ | |||
public static String fillZero(int codeLen, String cutedCode) { | |||
StringBuilder codeStr = new StringBuilder(20).append(cutedCode); | |||
for (int len = cutedCode.length(), i = 0; i < codeLen - len; i++) { | |||
codeStr.append("0"); | |||
} | |||
return codeStr.toString(); | |||
} | |||
/** | |||
* 转换为boolean | |||
*/ | |||
public static boolean toBoolean(String v, boolean b) { | |||
if (v == null) return b; | |||
return "1".equals(v) || "true".equalsIgnoreCase(v) || "Y".equalsIgnoreCase(v) || "yes".equalsIgnoreCase(v); | |||
} | |||
public static String getDateStr(Object _dateTime) { //yyyymmddhhMMss | |||
if (_dateTime == null) return ""; | |||
String dateTime = String.valueOf(_dateTime); | |||
if (PubUtil.isEmpty(dateTime) || dateTime.length() < 8) { | |||
return ""; | |||
} else { | |||
String s1 = String.valueOf(dateTime); | |||
return s1.substring(0, 4) + "-" + s1.substring(4, 6) + "-" + s1.substring(6, 8); | |||
} | |||
} | |||
/** | |||
* 日期转long | |||
* | |||
* @param dateStr YYYY-MM-DD | |||
* @return YYYYmmDD | |||
*/ | |||
public static long getDateLong(String dateStr) { | |||
if (PubUtil.isEmpty(dateStr) || dateStr.length() != 10) { | |||
return -1L; | |||
} | |||
String _dateTime = dateStr.replaceAll("-", ""); | |||
return getLongIgnoreErr(_dateTime); | |||
} | |||
/** | |||
* 转日期时间long | |||
* | |||
* @param dateStr | |||
* @return | |||
*/ | |||
public static long getDateTimeLong(String dateStr) { | |||
if (PubUtil.isEmpty(dateStr)) { | |||
return -1L; | |||
} | |||
String _dateTime = dateStr.replaceAll("-", "").replaceAll(" ", "").replaceAll(":", ""); | |||
_dateTime = StringUtil.getLeft(_dateTime + "00000000000000", 14); | |||
return getLongIgnoreErr(_dateTime); | |||
} | |||
/** | |||
* 转日期时间long | |||
* | |||
* @param dateStr | |||
* @return | |||
*/ | |||
public static long getEndDateTimeLong(String dateStr) { | |||
if (PubUtil.isEmpty(dateStr)) { | |||
return -1L; | |||
} | |||
String _dateTime = dateStr.replaceAll("-", "").replaceAll(" ", "").replaceAll(":", ""); | |||
_dateTime = StringUtil.getLeft(_dateTime + "00000000", 8) + "235959"; | |||
return getLongIgnoreErr(_dateTime); | |||
} | |||
public static String getDateTimeStr(Object _dateTime) { //yyyymmddhhMMss | |||
String dateTime = String.valueOf(_dateTime); | |||
if (PubUtil.isEmpty(dateTime) || dateTime.length() < 14) { | |||
return ""; | |||
} else { | |||
String s1 = String.valueOf(dateTime); | |||
return s1.substring(0, 4) + "-" + s1.substring(4, 6) + "-" + s1.substring(6, 8);// + " " + s1.substr(8, 10) + ":" + s1.substr(10, 12) + ":" + s1.substr(12); | |||
} | |||
} | |||
//删除除 {"0","1","2","3","4","5","6","7","8","9",".","/","-"}以外的字符 | |||
public static String remove(String str) { | |||
if (isEmpty(str)) return ""; | |||
char[] charArray = str.toCharArray(); | |||
char[] strArray = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '/', '-'}; | |||
for (char c : charArray) { | |||
if (!ArrayUtils.contains(strArray, c)) { | |||
str = StringUtils.remove(str, c); | |||
} | |||
} | |||
return str; | |||
} | |||
//数字转字符串,如无小数,则去掉.00 | |||
public static String getDoubleStr(double d) { | |||
return getAmountStr(d).replaceAll(",", "").replaceAll("\\.00", ""); | |||
} | |||
//将List转为sql语句in后面的常量,'XXX','XXX'格式 | |||
public static String getSqlInstr(List<String> list) { | |||
if (PubUtil.isEmpty(list)) return ""; | |||
StringBuilder sb = new StringBuilder(256); | |||
for (String s : list) { | |||
if (PubUtil.isEmptyId(s)) continue; | |||
sb.append(",'").append(s).append("'"); | |||
} | |||
if (sb.length() == 0) return ""; | |||
return sb.substring(1); | |||
} | |||
/** | |||
* 将数组ID转为sql语句in后面的常量 | |||
* | |||
* @param ids ID数组 | |||
* @return 返回格式:110,1002 | |||
*/ | |||
public static String getSqlInIds(String[] ids) { | |||
if (null == ids || ids.length < 1) return ""; | |||
StringBuilder s = new StringBuilder(128); | |||
for (String id : ids) { | |||
if (PubUtil.isEmptyId(id)) continue; | |||
s.append(id.trim()).append(","); | |||
} | |||
if (s.length() == 0) return ""; | |||
return s.substring(0, s.length() - 1); | |||
} | |||
public static String getSqlInIds(Collection<String> ids) { | |||
if (null == ids || ids.size() < 1) return ""; | |||
StringBuilder s = new StringBuilder(128); | |||
for (String id : ids) { | |||
if (PubUtil.isEmptyId(id)) continue; | |||
s.append(id).append(","); | |||
} | |||
if (s.length() == 0) return ""; | |||
return s.substring(0, s.length() - 1); | |||
} | |||
public static String getSqlInStrIds(Collection<String> ids) { | |||
if (null == ids || ids.size() < 1) return ""; | |||
StringBuilder s = new StringBuilder(128); | |||
for (String id : ids) { | |||
if (PubUtil.isEmptyId(id)) continue; | |||
s.append("'" + id + "'").append(","); | |||
} | |||
if (s.length() == 0) return ""; | |||
return s.substring(0, s.length() - 1); | |||
} | |||
public static List<String> buildSqlIds(Collection<String> ids, int size) { | |||
List<String> l = new ArrayList<>(); | |||
StringBuilder sb = new StringBuilder(); | |||
int count = 0; | |||
for (String id : ids) { | |||
if (count >= size) { | |||
l.add(sb.substring(1)); | |||
sb.setLength(0); | |||
count = 0; | |||
} | |||
sb.append(",").append(id); | |||
count++; | |||
} | |||
if (sb.length() > 0) l.add(sb.substring(1)); | |||
return l; | |||
} | |||
/** | |||
* 判断字符串是否只包含unicode数字。 | |||
* <p/> | |||
* <p> | |||
* <code>null</code>将返回<code>false</code>,空字符串<code>""</code>将返回 | |||
* <code>true</code>。 | |||
* </p> | |||
* <p/> | |||
* <p> | |||
* <pre> | |||
* StringUtil.isNumeric(null) = false | |||
* StringUtil.isNumeric("") = true | |||
* StringUtil.isNumeric(" ") = false | |||
* StringUtil.isNumeric("123") = true | |||
* StringUtil.isNumeric("12 3") = false | |||
* StringUtil.isNumeric("ab2c") = false | |||
* StringUtil.isNumeric("12-3") = false | |||
* StringUtil.isNumeric("12.3") = false | |||
* </pre> | |||
* | |||
* @param str 要检查的字符串 | |||
* @return 如果字符串非<code>null</code>并且全由unicode数字组成,则返回<code>true</code> | |||
*/ | |||
public static boolean isNumeric(String str) { | |||
if (PubUtil.isEmpty(str)) { | |||
return false; | |||
} | |||
int length = str.length(); | |||
for (int i = 0; i < length; i++) { | |||
if (!Character.isDigit(str.charAt(i))) { | |||
return false; | |||
} | |||
} | |||
return true; | |||
} | |||
public static boolean isDouble(String str) { | |||
if (null == str || "".equals(str)) { | |||
return false; | |||
} | |||
Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$"); | |||
return pattern.matcher(str).matches(); | |||
} | |||
/** | |||
* 获得某个字符在 字符串中 出现第n次时的 下标 | |||
* | |||
* @param res | |||
* @param _c | |||
* @param index | |||
* @return | |||
*/ | |||
public static int getIndexChar(String res, String _c, int index) { | |||
Matcher mh = Pattern.compile(_c).matcher(res); | |||
int i = 0; | |||
while (mh.find()) { | |||
i++; | |||
if (i == index) { | |||
break; | |||
} | |||
} | |||
return mh.start(); | |||
} | |||
/** | |||
* 将字符串转为指定字符集 | |||
* @param str | |||
* @param charSet | |||
* @return | |||
*/ | |||
public static String changeStrCharSet(String str, String charSet) { | |||
if (isEmptyId(str)) return str; | |||
String newStr = str; | |||
try { | |||
newStr = new String(str.getBytes(charSet), charSet); | |||
} catch (UnsupportedEncodingException e) { | |||
return newStr; | |||
} | |||
return newStr; | |||
} | |||
/** | |||
* 转布尔 | |||
* | |||
* @param o | |||
* @return | |||
*/ | |||
public static boolean getBool(Object o) { | |||
if (o == null) return false; | |||
String v = o.toString(); | |||
return "1".equals(v) || "t".equalsIgnoreCase(v) || "true".equalsIgnoreCase(v); | |||
} | |||
/** | |||
* 返回当恰能操作系统是否为Linux | |||
* | |||
* @return boolean | |||
*/ | |||
public static boolean osIsLinux() { | |||
String s = System.getProperty("os.name"); | |||
return s != null && s.equalsIgnoreCase("linux"); | |||
} | |||
public static boolean strAllEqual(String str) { | |||
if (PubUtil.isEmptyId(str)) return true; | |||
for (int i = 1; i < str.length(); i++) | |||
if (str.charAt(i) != str.charAt(0)) return false; | |||
return true; | |||
} | |||
/** | |||
* 对中文字段 list排序 | |||
* | |||
* @param list | |||
* @param fun 获取排序的值 | |||
* @param desc 降序 | |||
* @throws Exception | |||
*/ | |||
public static <T, R> void sortChineseStrList(List<T> list, Function<T, R> fun, boolean desc) { | |||
list.sort((T o1, T o2) -> { | |||
int rt = Collator.getInstance(Locale.CHINESE).compare(fun.apply(o1), fun.apply(o2)); | |||
if (rt < 0) { | |||
if (desc) { | |||
return 1; | |||
} | |||
return -1; | |||
} | |||
if (rt > 0) { | |||
if (desc) { | |||
return -1; | |||
} | |||
return 1; | |||
} | |||
return 0; | |||
}); | |||
} | |||
//遍历map | |||
public static <K, V> void doForMap(Map<K, V> map, IMapEntryReader<K, V> reader) throws Exception { | |||
if (map == null) return; | |||
for (Map.Entry<K, V> entry : map.entrySet()) { | |||
reader.readEntry(entry.getKey(), entry.getValue()); | |||
} | |||
} | |||
public interface IMapEntryReader<K, V> { | |||
void readEntry(K key, V value) throws Exception; | |||
} | |||
} |
@@ -0,0 +1,129 @@ | |||
package cc.smtweb.framework.core.util; | |||
import org.apache.commons.codec.binary.Base64; | |||
import javax.crypto.Cipher; | |||
import java.security.*; | |||
import java.security.interfaces.RSAPrivateKey; | |||
import java.security.interfaces.RSAPublicKey; | |||
import java.security.spec.PKCS8EncodedKeySpec; | |||
import java.security.spec.X509EncodedKeySpec; | |||
/** | |||
* Created by Akmm at 2020/4/23 15:23 | |||
*/ | |||
public class RSAUtil { | |||
private static final KeyPair keyPair = initKey(); | |||
private static KeyPair initKey() { | |||
try { | |||
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA"); | |||
keyPairGen.initialize(1024, new SecureRandom()); | |||
// 生成一个密钥对,保存在keyPair中 | |||
return keyPairGen.generateKeyPair(); | |||
} catch (Exception e) { | |||
throw new RuntimeException(e); | |||
} | |||
} | |||
public static String generateBase64PublicKey() { | |||
PublicKey publicKey = keyPair.getPublic(); | |||
return new String(Base64.encodeBase64(publicKey.getEncoded())); | |||
} | |||
public static String generateBase64PrvKey() { | |||
PrivateKey privkey = keyPair.getPrivate(); | |||
return new String(Base64.encodeBase64(privkey.getEncoded())); | |||
} | |||
public static String decryptBase64(String string) throws Exception { | |||
byte[] byteArray = Base64.decodeBase64(string.getBytes()); | |||
Cipher cipher = Cipher.getInstance("RSA"); | |||
PrivateKey privateKey = keyPair.getPrivate(); | |||
cipher.init(Cipher.DECRYPT_MODE, privateKey); | |||
byte[] plainText = cipher.doFinal(byteArray); | |||
return new String(plainText); | |||
} | |||
/** | |||
* 利用我们提供的密钥加密 | |||
* | |||
* @param str | |||
* @return | |||
* @throws Exception | |||
*/ | |||
public static String encryptBase64(String str) throws Exception { | |||
PublicKey pubKey = keyPair.getPublic(); | |||
//RSA加密 | |||
Cipher cipher = Cipher.getInstance("RSA"); | |||
cipher.init(Cipher.ENCRYPT_MODE, pubKey); | |||
return Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8"))); | |||
} | |||
/** | |||
* 指定RSA公钥加密 | |||
* | |||
* @param str 加密字符串 | |||
* @param publicKey 公钥 | |||
* @return 密文 | |||
* @throws Exception 加密过程中的异常信息 | |||
*/ | |||
public static String encryptBase64(String str, String publicKey) throws Exception { | |||
//base64编码的公钥 | |||
byte[] decoded = Base64.decodeBase64(publicKey); | |||
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded)); | |||
//RSA加密 | |||
Cipher cipher = Cipher.getInstance("RSA"); | |||
cipher.init(Cipher.ENCRYPT_MODE, pubKey); | |||
return Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8"))); | |||
} | |||
/** | |||
* 指定RSA私钥解密 | |||
* | |||
* @param str 加密字符串 | |||
* @param privateKey 私钥 | |||
* @return 铭文 | |||
* @throws Exception 解密过程中的异常信息 | |||
*/ | |||
public static String decryptBase64(String str, String privateKey) throws Exception { | |||
//64位解码加密后的字符串 | |||
byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8")); | |||
//base64编码的私钥 | |||
byte[] decoded = Base64.decodeBase64(privateKey); | |||
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded)); | |||
//RSA解密 | |||
Cipher cipher = Cipher.getInstance("RSA"); | |||
cipher.init(Cipher.DECRYPT_MODE, priKey); | |||
String outStr = new String(cipher.doFinal(inputByte)); | |||
return outStr; | |||
} | |||
public static void main(String[] args) throws Exception { | |||
String ss = "我的123"; | |||
/*String ss = "UR/ykQMQiLePbUFIlRYkUIaDcxDTYVOK9uTwqkbwlo6OcFzs0sQL+qzRMYBYCOC+ufLSSbQgwgbb+gxWf5GP5QyH5ygp4XHOa6IWbILHDB1nqze4S+NKhR72UOMqY5J4/C2ZZyiTQKTY5KJzn1gtjEbrzBHEFQEBTIJbg5k2zu4="; | |||
String pk = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIBwwtBhDftNsYLUMQme/Av+RQKg32wmhkd7m2hm/CZu+FnNsWCIGt7pmCVfIdnJCgm15HjmW/alfLO5XDDacRVmL" + | |||
"I5pvmv6jCki+BoquUMQFV3pJcgSMg/MocdACPkXSvB/m7IhDu2P3eXIx55aieLiPDciujiOD9Do5AmicM07AgMBAAECgYAha7X1fNujgYLoFQixhTH2ePJCDdQ1Ew0ruokksG96HLP4wi" + | |||
"IeiSvcxxG0370yKzK2diL+JorVchgxt9C6UYzM8o+b4NflFCuKqfUCk63fTrBeXI0OaUFJATxxXfqzNgDaWgiqItRE/h1rBtQG5FQ0V1zFE+pLPSK+bnD6uWpFEQJBAM1x8pFOdnP8+iX" + | |||
"brt+dHeFttOLFt8OS/TVD2ZD+NblxPYYRha89a9mLb0TkErEnQEegoA1Dq+4c+4Ayx3+nnykCQQCgC+Rr+pilMIUuJSnBbY4+Mn2cgRNhQX+w2cDpXNxtFi1eld2MjHgVbp+VmivGSDBe" + | |||
"c/Oq7qSnEITWLxYsvSnDAkB1AZmh8ir6YjDhkN1m06fp9L5ZjTvcsgFt7aeisXVNmVXt3lrh9DxRWN1VQgP3SyCL6spc45au9WizmbgPVxsJAkAqUPqkUi3p/1o3STwxGHZzm4X6y2YEY" + | |||
"P0SFYEcMqFVdneoepeJgdp+UDGBrLHGS2/+iUnz+AUw5v04ZFrImXthAkAfoPdHP00CFsvIK4fK/iBboDPmY014/DR+W8jfJHHu5Lv0XuYvdNsrvM9GL+UNgjNTCF0OT3UWFfq5Hrta89vZ"; | |||
System.out.println(decryptBase64(ss, pk));*/ | |||
String pubkey = generateBase64PublicKey(); | |||
System.out.println("pubkey::::::::::::::" + pubkey); | |||
PrivateKey pKey = keyPair.getPrivate(); | |||
String prvkey = new String(Base64.encodeBase64(pKey.getEncoded())); | |||
System.out.println("prvkey::::::" + prvkey); | |||
System.out.println("encrypt1::::::" + encryptBase64(ss)); | |||
String en = encryptBase64(ss, pubkey); | |||
System.out.println("encrypt2::::::" + en); | |||
System.out.println("decrypt1::::::" + decryptBase64(en)); | |||
System.out.println("decrypt2::::::" + decryptBase64(encryptBase64(ss), prvkey)); | |||
} | |||
} |
@@ -0,0 +1,502 @@ | |||
package cc.smtweb.framework.core.util; | |||
import java.text.Collator; | |||
import java.util.*; | |||
/** | |||
* Created with IntelliJ IDEA. | |||
* User: AKhh | |||
* Date: 12-12-24 下午5:09 | |||
* To change this template use File | Settings | File Templates. | |||
*/ | |||
@SuppressWarnings("UnusedDeclaration") | |||
public class StringUtil { | |||
private static Collator chineseCollator = Collator.getInstance(Locale.CHINA); | |||
/** | |||
* strSrc中寻找第一个strSe并且返回以其分隔的Left部分,汉字长度也为1 | |||
* | |||
* @param strSrc 源字符串 | |||
* @param strSe 分割字符 | |||
* @return String 返回 | |||
*/ | |||
public static String getLeft(String strSrc, String strSe) { | |||
if (PubUtil.isEmpty(strSrc)) | |||
return ""; | |||
if (PubUtil.isEmpty(strSe)) | |||
strSe = " "; | |||
String result = ""; | |||
int pos = strSrc.indexOf(strSe); | |||
if (pos >= 0) | |||
result = strSrc.substring(0, pos); | |||
return result; | |||
} | |||
/** | |||
* 返回字符串的左边部分,汉字长度也为1 | |||
* | |||
* @param strSrc 源串,如果为空,则返回“”; | |||
* @param count 要获取的右边字符串长度,负数将返回“”,如果count>字符串长度,则返回整个字符串; | |||
* @return String return | |||
*/ | |||
public static String getLeft(String strSrc, int count) { | |||
if (PubUtil.isEmpty(strSrc) || count <= 0) { | |||
return ""; | |||
} | |||
if (strSrc.length() < count) { | |||
return strSrc; | |||
} else { | |||
return strSrc.substring(0, count); | |||
} | |||
} | |||
/** | |||
* strSrc中寻找第一个strSe并且返回以其分隔的Right部分,汉字长度也为1 | |||
* | |||
* @param strSrc 源串 | |||
* @param strSe 分隔符,一个字符 | |||
* @return String right部分 | |||
*/ | |||
public static String getRight(String strSrc, String strSe) { | |||
if (PubUtil.isEmpty(strSrc)) | |||
return ""; | |||
if (PubUtil.isEmpty(strSe)) | |||
strSe = " "; | |||
String result = strSrc; | |||
int pos = strSrc.indexOf(strSe); | |||
if (pos >= 0) | |||
result = strSrc.substring(pos + strSe.length()); | |||
return result; | |||
} | |||
/** | |||
* 返回字符串的右边部分,汉字长度也为1 | |||
* | |||
* @param strSrc 源串 | |||
* @param count 要获取的右边字符串长度,负数将返回“”,如果count>字符串长度,则返回整个字符串; | |||
* @return String return | |||
*/ | |||
public static String getRight(String strSrc, int count) { | |||
if (PubUtil.isEmpty(strSrc) || count <= 0) { | |||
return ""; | |||
} | |||
int l = strSrc.length(); | |||
if (l <= count) { | |||
return strSrc; | |||
} | |||
return strSrc.substring(l - count); | |||
} | |||
/** | |||
* 左边补齐字符 | |||
* | |||
* @param src 源串 | |||
* @param pad 补齐字符 | |||
* @param length 最终长度 | |||
* @return 补齐后的字符串 | |||
*/ | |||
public static String padLeft(String src, String pad, int length) { | |||
StringBuilder sb = new StringBuilder(repeatString(pad, length)); | |||
sb.append(src); | |||
return sb.substring(sb.length() - length); | |||
} | |||
public static String padLeft(long src, String pad, int length) { | |||
StringBuilder sb = new StringBuilder(repeatString(pad, length)); | |||
sb.append(src); | |||
return sb.substring(sb.length() - length); | |||
} | |||
public static String padRight(String src, String pad, int length) { | |||
StringBuilder sb = new StringBuilder(length * pad.length() + src.length()); | |||
sb.append(src).append(repeatString(pad, length)); | |||
return sb.substring(0, length); | |||
} | |||
public static String padRight(long src, String pad, int length) { | |||
StringBuilder sb = new StringBuilder(length * pad.length()); | |||
sb.append(src).append(repeatString(pad, length)); | |||
return sb.substring(0, length); | |||
} | |||
/** | |||
* 由于jdk1.3提供的replace函数不能满足替换要求,自己写一个 | |||
* | |||
* @param src 源串 | |||
* @param oldS 将... | |||
* @param newS 替换成... | |||
* @return 替换后的字符串 | |||
*/ | |||
public static String replaceString(String src, String oldS, String newS) { | |||
StringBuilder ret = new StringBuilder(64); | |||
int pos = src.indexOf(oldS); | |||
while (pos >= 0) { | |||
ret.append(src.substring(0, pos)).append(newS); | |||
src = src.substring(pos + oldS.length()); | |||
pos = src.indexOf(oldS); | |||
} | |||
ret.append(src); | |||
return ret.toString(); | |||
} | |||
/** | |||
* 取得指定字符串左边的有效数字,首先去掉两边空格 | |||
* | |||
* @param s 源串 | |||
* @return 串左边的有效数字 | |||
*/ | |||
public static String getStringLeftNumber(String s) { | |||
String ret = ""; | |||
int dotCount = 0; | |||
s = s.trim(); | |||
char[] carr = s.toCharArray(); | |||
for (char aCarr : carr) { | |||
if (Character.isDigit(aCarr)) { | |||
ret += aCarr; | |||
} else if (aCarr == '.' && dotCount == 0) { | |||
ret += aCarr; | |||
dotCount++; | |||
} else { | |||
break; | |||
} | |||
} | |||
if (ret.endsWith(".")) { | |||
ret = ret.substring(0, ret.length() - 1); | |||
} | |||
return ret; | |||
} | |||
/** | |||
* 取得重复字串 | |||
* | |||
* @param repeatString 重复字串 | |||
* @param count 重复次数 | |||
* @return String | |||
*/ | |||
public static String repeatString(String repeatString, int count) { | |||
if (count <= 0) return ""; | |||
StringBuilder ret = new StringBuilder(repeatString.length() * count); | |||
for (int i = 1; i <= count; i++) { | |||
ret.append(repeatString); | |||
} | |||
return ret.toString(); | |||
} | |||
/** | |||
* 去除字符串左边的指定字符串 | |||
* | |||
* @param src 源字符串 | |||
* @param cut 要去掉的字符串; | |||
* @return 处理结果 | |||
*/ | |||
public static String cutStringLeft(String src, String cut) { | |||
if (PubUtil.isEmpty(src) || PubUtil.isEmpty(cut)) { | |||
return ""; | |||
} | |||
if (src.startsWith(cut)) { | |||
return cutStringLeft(src.substring(cut.length()), cut); | |||
} else { | |||
return src; | |||
} | |||
} | |||
public static String cutStringRight(String src, String cut) { | |||
if (PubUtil.isEmpty(src) || PubUtil.isEmpty(cut)) { | |||
return ""; | |||
} | |||
while (src.endsWith(cut)) | |||
src = src.substring(0, src.length() - cut.length()); | |||
return src; | |||
} | |||
/** | |||
* Removes all spaces from a string | |||
* 可以替换大部分空白字符, 不限于空格,\s 可以匹配空格、制表符、换页符等空白字符的其中任意一个 | |||
*/ | |||
public static String removeSpaces(String str) { | |||
return str.replaceAll("\\s*", ""); | |||
} | |||
/** | |||
* Creates a single string from a List of strings seperated by a delimiter. | |||
* | |||
* @param list a list of strings to join | |||
* @param delim the delimiter character(s) to use. (null value will join with no delimiter) | |||
* @return a String of all values in the list seperated by the delimiter | |||
*/ | |||
public static String join(List<String> list, String delim) { | |||
if (list == null || list.size() < 1) | |||
return null; | |||
StringBuffer buf = new StringBuffer(); | |||
Iterator i = list.iterator(); | |||
while (i.hasNext()) { | |||
buf.append((String) i.next()); | |||
if (i.hasNext()) | |||
buf.append(delim); | |||
} | |||
return buf.toString(); | |||
} | |||
/** | |||
* Splits a String on a delimiter into a List of Strings. | |||
* | |||
* @param str the String to split | |||
* @param delim the delimiter character(s) to join on (null will split on whitespace) | |||
* @return a list of Strings | |||
*/ | |||
public static List<String> split(String str, String delim) { | |||
List<String> splitList = new ArrayList<>(); | |||
StringTokenizer st; | |||
if (str == null) | |||
return splitList; | |||
if (delim != null) | |||
st = new StringTokenizer(str, delim); | |||
else | |||
st = new StringTokenizer(str); | |||
while (st.hasMoreTokens()) { | |||
splitList.add(st.nextToken()); | |||
} | |||
return splitList; | |||
} | |||
//是否为true,(1,y,true,yes) | |||
public static boolean toBoolean(String v) { | |||
return "1".equals(v) || "y".equalsIgnoreCase(v) || "true".equalsIgnoreCase(v) || "yes".equalsIgnoreCase(v); | |||
} | |||
public static int chineseCompare(String s1, String s2) { | |||
return chineseCollator.compare(s1, s2); | |||
} | |||
/** | |||
* 按照编码级次,得到类型的真实编码,主要用于like 'parentCode%' | |||
* getSplitTypeCode('GF82000',2, 2, 1) == GF82 | |||
* getSplitTypeCode('GF82100',3, 2, 1) == GF821 | |||
* getSplitTypeCode('82100' ,3, 0, 1) == 821 | |||
* | |||
* @param curLevel 当前编码的所在层次 | |||
* @param startIndex 数字编码的开始选项 | |||
* @param perSize 每层的数字编码长度 | |||
*/ | |||
public static String getRealCode(String code, int curLevel, int startIndex, int perSize) { | |||
StringBuilder sb = new StringBuilder(code.length()); | |||
if (startIndex > 0) sb.append(code.substring(0, startIndex)); | |||
for (int i = startIndex, l = 0; i < code.length(); i += perSize) { | |||
if (l < curLevel) { | |||
sb.append(code.substring(i, i + perSize)); | |||
++l; | |||
} else { | |||
break; | |||
} | |||
} | |||
return sb.toString(); | |||
} | |||
//函数功能: 正整数 | |||
public static boolean isPureNumber(String inputString) { | |||
return inputString.matches("^[1-9]\\d*$"); | |||
} | |||
//函数功能: 整数 | |||
public static boolean isNumber(String inputString) { | |||
return inputString.matches("^[-+][0-9]\\d*$"); | |||
} | |||
//函数功能: 浮点数 | |||
public static boolean isAmount(String inputString) { | |||
return inputString.matches("^[-+]?[\\d,]+(\\.\\d+)?$"); | |||
} | |||
//函数功能: 带千分号的整数 | |||
public static boolean isFormatNumber(String inputString) { | |||
return inputString.matches("^[-+]?[\\d,]+(\\d+)?$"); | |||
} | |||
//首字母大写 | |||
public static String upFirst(String s) { | |||
return s.substring(0, 1).toUpperCase() + s.substring(1); | |||
} | |||
public static String padRightBytes(String src, String pad, int length) { | |||
length -= src.replaceAll("[^\\x00-\\xff]", "**").length(); | |||
return src + repeatString(pad, length); | |||
} | |||
//按字节数取子串,begin不是按字节的 | |||
public static String substrByte(String src, int begin, int len) { | |||
StringBuilder sb = new StringBuilder(32); | |||
char c; | |||
int tl = src.length(); | |||
for (int i = begin; i < len + begin && i < tl; i++) { | |||
c = src.charAt(i); | |||
sb.append(c); | |||
if (String.valueOf(c).replaceAll("[^\\x00-\\xff]", "**").length() > 1) { | |||
// 遇到中文汉字,截取字节总数减1 | |||
--len; | |||
} | |||
} | |||
return sb.toString(); | |||
} | |||
/** | |||
* 通配符算法。 可以匹配"*"和"?" | |||
* 如a*b?d可以匹配aAAAbcd | |||
* | |||
* @param pattern 匹配表达式 | |||
* @param str 匹配的字符串 | |||
* @return | |||
*/ | |||
public static boolean match(String pattern, String str) { | |||
if (pattern == null || str == null) | |||
return false; | |||
boolean result = false; | |||
char c; // 当前要匹配的字符串 | |||
boolean beforeStar = false; // 是否遇到通配符* | |||
int back_i = 0;// 回溯,当遇到通配符时,匹配不成功则回溯 | |||
int back_j = 0; | |||
int i, j; | |||
for (i = 0, j = 0; i < str.length(); ) { | |||
if (pattern.length() <= j) { | |||
if (back_i != 0) {// 有通配符,但是匹配未成功,回溯 | |||
beforeStar = true; | |||
i = back_i; | |||
j = back_j; | |||
back_i = 0; | |||
back_j = 0; | |||
continue; | |||
} | |||
break; | |||
} | |||
if ((c = pattern.charAt(j)) == '*') { | |||
if (j == pattern.length() - 1) {// 通配符已经在末尾,返回true | |||
result = true; | |||
break; | |||
} | |||
beforeStar = true; | |||
j++; | |||
continue; | |||
} | |||
if (beforeStar) { | |||
if (str.charAt(i) == c) { | |||
beforeStar = false; | |||
back_i = i + 1; | |||
back_j = j; | |||
j++; | |||
} | |||
} else { | |||
if (c != '?' && c != str.charAt(i)) { | |||
result = false; | |||
if (back_i != 0) {// 有通配符,但是匹配未成功,回溯 | |||
beforeStar = true; | |||
i = back_i; | |||
j = back_j; | |||
back_i = 0; | |||
back_j = 0; | |||
continue; | |||
} | |||
break; | |||
} | |||
j++; | |||
} | |||
i++; | |||
} | |||
if (i == str.length() && j == pattern.length())// 全部遍历完毕 | |||
result = true; | |||
return result; | |||
} | |||
//填充变量 | |||
public static String myReplaceStrEx(String express, String b, String e, IStrHanlder hanlder) { | |||
if (null == express) return express; | |||
int keyBegin = 0, keyEnd = 0; | |||
int lb = b.length(), le = e.length(); | |||
String fn; | |||
while (true) { | |||
keyBegin = express.indexOf(b, keyBegin); | |||
if (keyBegin < 0) break; | |||
keyEnd = express.indexOf(e, keyBegin); | |||
if (keyEnd <= keyBegin) break; | |||
keyBegin++; | |||
fn = express.substring(keyBegin + lb - 1, keyEnd); | |||
fn = hanlder.work(fn); | |||
if (fn != null) { | |||
express = express.substring(0, keyBegin - 1) + fn + express.substring(keyEnd + le); | |||
} | |||
} | |||
return express; | |||
} | |||
//填充变量 | |||
public static String myReplaceStrEx(String express, String b, String e, final Map<String, String> mapVals) { | |||
return myReplaceStrEx(express, b, e, new IStrHanlder() { | |||
@Override | |||
public String work(String src) { | |||
return PubUtil.checkNull(mapVals.get(src)); | |||
} | |||
}); | |||
} | |||
/*Blob转String*/ | |||
// public static String blob2Str(Blob blob) { | |||
// if (blob == null) return ""; | |||
// ByteArrayOutputStream outStream = null; | |||
// try { | |||
// long len = blob.length(); | |||
// if (len == 0L) return ""; | |||
// byte[] bytes; | |||
// long i = 1L; | |||
// outStream = new ByteArrayOutputStream(); | |||
// while (i < len) { | |||
// bytes = blob.getBytes(i, 1024); | |||
// i += 1024L; | |||
// outStream.write(bytes); | |||
// } | |||
// | |||
// return UtilEncode.base64EncodeB(outStream.toByteArray()); | |||
// } catch (Exception e) { | |||
// e.printStackTrace(); | |||
// return ""; | |||
// } finally { | |||
// IOUtils.closeQuietly(outStream); | |||
// } | |||
// } | |||
public interface IStrHanlder { | |||
String work(String src); | |||
} | |||
public static int[] splitStr(String src, String ch) { | |||
String[] ss = src.split(ch); | |||
int[] ret = new int[ss.length]; | |||
for (int i = 0, len = ss.length; i < len; i++) { | |||
ret[i] = PubUtil.getIntIgnoreErr(ss[i]); | |||
} | |||
return ret; | |||
} | |||
public static void main(String[] args) { | |||
String s = "a[#[#123#]bcde[aaa]bcd"; | |||
s = myReplaceStrEx(s, "[#", "#]", new IStrHanlder() { | |||
@Override | |||
public String work(String src) { | |||
if (src.equals("123")) return "1"; | |||
return null; | |||
} | |||
}); | |||
System.out.println(s); | |||
} | |||
} |
@@ -0,0 +1,258 @@ | |||
package cc.smtweb.framework.core.util; | |||
import org.dom4j.*; | |||
import org.dom4j.io.OutputFormat; | |||
import org.dom4j.io.SAXReader; | |||
import org.dom4j.io.XMLWriter; | |||
import java.io.File; | |||
import java.io.FileOutputStream; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import java.net.MalformedURLException; | |||
import java.net.URL; | |||
import java.util.HashMap; | |||
import java.util.Iterator; | |||
import java.util.Map; | |||
/** | |||
* Created by Akmm at 2011-1-19 10:16:01 | |||
*/ | |||
public class XmlUtil { | |||
/** | |||
* 按缩减格式写xml文件 | |||
* | |||
* @param document | |||
* @param fileName | |||
* @throws IOException | |||
*/ | |||
public static void saveToFileCompact(Document document, String fileName) throws IOException { | |||
saveToFile(document, fileName, OutputFormat.createCompactFormat()); | |||
} | |||
/** | |||
* 按美化格式写xml | |||
* | |||
* @param document | |||
* @param fileName | |||
* @throws IOException | |||
*/ | |||
public static void saveToFilePretty(Document document, String fileName) throws IOException { | |||
// 指定文件 美化格式 | |||
saveToFile(document, fileName, OutputFormat.createPrettyPrint()); | |||
} | |||
/** | |||
* 直接写xml,无格式 | |||
* | |||
* @param document | |||
* @param fileName | |||
* @throws IOException | |||
*/ | |||
public static void saveToFile(Document document, String fileName, OutputFormat format) throws IOException { | |||
XMLWriter xmlWriter; | |||
if (format == null) xmlWriter = new XMLWriter(new FileOutputStream(fileName)); | |||
else xmlWriter = new XMLWriter(new FileOutputStream(fileName), format); | |||
xmlWriter.write(document); | |||
xmlWriter.close(); | |||
} | |||
/** | |||
* 直接写xml,无格式 | |||
* | |||
* @param document | |||
* @param fileName | |||
* @throws IOException | |||
*/ | |||
public static void saveToFile(Document document, String fileName) throws IOException { | |||
saveToFile(document, fileName, null); | |||
} | |||
/** | |||
* 创建一个Document | |||
* | |||
* @return | |||
*/ | |||
public static Document createDocument() { | |||
return DocumentHelper.createDocument(); | |||
} | |||
/** | |||
* 解析string为一个Document | |||
* | |||
* @return | |||
*/ | |||
public static Document parseXml(String xml) throws DocumentException { | |||
return DocumentHelper.parseText(xml); | |||
} | |||
public static Document readXmlDocument(String fileName) throws MalformedURLException, DocumentException { | |||
SAXReader reader = new SAXReader(); | |||
return reader.read(new File(fileName)); | |||
} | |||
public static Document readXmlDocument(InputStream file) throws MalformedURLException, DocumentException { | |||
SAXReader reader = new SAXReader(); | |||
return reader.read(file); | |||
} | |||
public static Document readXmlDocument(URL url) throws MalformedURLException, DocumentException { | |||
SAXReader reader = new SAXReader(); | |||
return reader.read(url); | |||
} | |||
public static Document readXmlDocument(File file) throws MalformedURLException, DocumentException { | |||
SAXReader reader = new SAXReader(); | |||
return reader.read(file); | |||
} | |||
//获取节点属性 | |||
public static String getChildValue(Element e, String chileName) { | |||
Element ec = e.element(chileName); | |||
if (ec != null) return ec.getTextTrim(); | |||
return null; | |||
} | |||
//获取节点属性 | |||
public static String getAttribute(Element e, String attrName) { | |||
Attribute a = e.attribute(attrName); | |||
if (a != null) return a.getValue(); | |||
return null; | |||
} | |||
//获取节点属性,转为字符串数组 | |||
public static String[] getAttrStrArray(Element e, String attrName) { | |||
String s = getAttribute(e, attrName); | |||
if (PubUtil.isNotEmptyStr(s)) return StringUtil.split(s, ",").toArray(new String[]{}); | |||
return new String[0]; | |||
} | |||
//获取节点属性,转为int数组 | |||
public static int[] getAttrIntArray(Element e, String attrName) { | |||
String[] ss = getAttrStrArray(e, attrName); | |||
int[] ret = new int[ss.length]; | |||
for (int i = 0, len = ss.length; i < len; i++) { | |||
ret[i] = PubUtil.getIntIgnoreErr(ss[i]); | |||
} | |||
return ret; | |||
} | |||
public static int getAttributeInt(Element e, String attrName) { | |||
return PubUtil.getIntIgnoreErr(getAttribute(e, attrName)); | |||
} | |||
public static double getAttributeDbl(Element e, String attrName) { | |||
return PubUtil.getDoubleIgnoreErr(getAttribute(e, attrName)); | |||
} | |||
public static boolean getAttributeBool(Element e, String attrName) { | |||
String s = getAttribute(e, attrName); | |||
return "1".equals(s) || "Y".equalsIgnoreCase(s) || "true".equalsIgnoreCase(s) || "yes".equalsIgnoreCase(s); | |||
} | |||
//获取节点属性 | |||
public static String getAttributeIgnoreNull(Element e, String attrName) { | |||
return PubUtil.checkNull(getAttribute(e, attrName)); | |||
} | |||
//读取节点所有属性,返回map | |||
public static Map<String, String> getAttributeMap(Element e) { | |||
Map<String, String> map = new HashMap<>(); | |||
for (Iterator as = e.attributeIterator(); as.hasNext(); ) { | |||
Attribute a = (Attribute) as.next(); | |||
map.put(a.getName(), a.getValue()); | |||
} | |||
return map; | |||
} | |||
public static void removeAttribute(Element e, String attrName) { | |||
Attribute a = e.attribute(attrName); | |||
if (a != null) e.remove(a); | |||
} | |||
//设置节点属性 | |||
public static void setAttribute(Element e, String attrName, String attrValue) { | |||
if (PubUtil.isNotEmpty(attrValue)) e.addAttribute(attrName, attrValue); | |||
else removeAttribute(e, attrName); | |||
} | |||
//设置节点属性 | |||
public static void setAttribute(Element e, String attrName, int attrValue) { | |||
if (attrValue != 0) e.addAttribute(attrName, String.valueOf(attrValue)); | |||
else removeAttribute(e, attrName); | |||
} | |||
//设置节点属性 | |||
public static void setAttribute(Element e, String attrName, double attrValue) { | |||
if (!MathUtil.isEqualsZero(attrValue)) e.addAttribute(attrName, String.valueOf(attrValue)); | |||
else removeAttribute(e, attrName); | |||
} | |||
//设置节点属性 | |||
public static void setAttribute(Element e, String attrName, boolean attrValue) { | |||
if (attrValue) e.addAttribute(attrName, "1"); | |||
else removeAttribute(e, attrName); | |||
} | |||
//设置节点属性 | |||
public static void removeElement(Element parent, String name) { | |||
Element e = parent.element(name); | |||
if (e != null) parent.remove(e); | |||
} | |||
//解析节点到Map | |||
public static void decodeForMap(Element pnode, Map<String, String> map) { | |||
for (Iterator it = pnode.elementIterator(); it.hasNext(); ) { | |||
Element el = (Element) it.next(); | |||
String nodename = el.getName(); | |||
map.put(nodename, el.getTextTrim()); | |||
} | |||
} | |||
//解析节点到Map | |||
public static void writeMap(Element pnode, Map<String, Object> map) { | |||
for (String s: map.keySet()) { | |||
Element e = pnode.addElement(s); | |||
final Object o = map.get(s); | |||
e.setText(o != null ? o.toString() : ""); | |||
} | |||
} | |||
//添加节点 | |||
public static void addNode(Element pnode, String name, Object value){ | |||
Element e = pnode.addElement(name); | |||
e.setText(value != null ? value.toString() : ""); | |||
} | |||
public static void main(String[] args) { | |||
try { | |||
Document doc = readXmlDocument("f:\\temp\\plugin-developer.xml"); | |||
Element e = doc.getRootElement(); | |||
System.out.println("id:::::::" + getAttribute(e, "id")); | |||
System.out.println("name:::::::" + getAttribute(e, "name")); | |||
System.out.println("vvvv:::::::" + getAttribute(e, "呵呵")); | |||
// e.accept(new VisitorSupport() { | |||
// public void visit(Element element) { | |||
// System.out.println(element.getName() + ":::eee:::" + element.getText()); | |||
// } | |||
// | |||
// public void visit(Attribute a) { | |||
// System.out.println(a.getName() + ":::aaa:::" + a.getValue()); | |||
// } | |||
// }); | |||
// 枚举所有子节点 | |||
Element e1 = e.element("fields"); | |||
e1 = e1.element("field"); | |||
System.out.println(getAttributeIgnoreNull(e1, "id")); | |||
setAttribute(e, "vvvv", "tttttttt"); | |||
//e.addCDATA("<a>asdasd</a>"); | |||
saveToFileCompact(doc, "f:\\temp\\plugin-developer1.xml"); | |||
saveToFilePretty(doc, "f:\\temp\\plugin-developer2.xml"); | |||
saveToFile(doc, "f:\\temp\\plugin-developer3.xml"); | |||
} catch (Exception e1) { | |||
e1.printStackTrace(); | |||
} | |||
} | |||
} |