redis 的基本用法
/** * 〈一句话功能简述〉 redis工具类 * 〈功能详细描述〉 * * @author 17040683 * @see [相关类/方法](可选) * @since [产品/模块版本] (可选) * @Date 2017年5月23日 下午4:56:36 */ public class RedisTool { protected ShardedJedisClientImpl shardedClient; public RedisTool() { shardedClient = new ShardedJedisClientImpl("redis.conf"); } /** * * get操作 */ public String get(final String key) { String value = shardedClient.execute(new ShardedJedisAction() { @Override public String doAction(ShardedJedis shardedJedis) { return shardedJedis.get(key); } }); return value; } /** * * set操作 */ public String set(final String key, final String val) { return shardedClient.execute(new ShardedJedisAction () { @Override public String doAction(ShardedJedis shardedJedis) { return shardedJedis.set(key, val); } }); } /** * * expire操作 */ public Long expire(final String key, final int seconds) { return shardedClient.execute(new ShardedJedisAction () { @Override public Long doAction(ShardedJedis shardedJedis) { return shardedJedis.expire(key, seconds); } }); } /** * * setex操作 */ public String setex(final String key, final int seconds, final String value) { return shardedClient.execute(new ShardedJedisAction () { @Override public String doAction(ShardedJedis shardedJedis) { return shardedJedis.setex(key, seconds, value); } }); } /** * * del操作 */ public Long delKey(final String key) { return shardedClient.execute(new ShardedJedisAction () { @Override public Long doAction(ShardedJedis shardedJedis) { return shardedJedis.del(key); } }); } /** * * 获取管道操作 */ public ShardedJedisPipeline pipeline() { return shardedClient.execute(new ShardedJedisAction () { @Override public ShardedJedisPipeline doAction(ShardedJedis shardedJedis) { return shardedJedis.pipelined(); } }); } /** * * hset操作 * */ public Long hset(final String key, final String field, final String value) { return shardedClient.execute(new ShardedJedisAction () { @Override public Long doAction(ShardedJedis shardedJedis) { return shardedJedis.hset(key, field, value); } }); } /** * * hget操作 * */ public String hget(final String key, final String field) { return shardedClient.execute(new ShardedJedisAction () { @Override public String doAction(ShardedJedis shardedJedis) { return shardedJedis.hget(key, field); } }); } /** * * hdel操作 * */ public Long hdel(final String key, final String field) { return shardedClient.execute(new ShardedJedisAction () { @Override public Long doAction(ShardedJedis shardedJedis) { return shardedJedis.hdel(key, field); } }); } /** * * hkeys操作 * */ public Set hkeys(final String key) { return shardedClient.execute(new ShardedJedisAction >() { @Override public Set doAction(ShardedJedis shardedJedis) { return shardedJedis.hkeys(key); } }); } /** * * ltrim操作 * * @param key * @param field */ public String ltrim(final String key, final long start, final long end) { return shardedClient.execute(new ShardedJedisAction () { @Override public String doAction(ShardedJedis shardedJedis) { return shardedJedis.ltrim(key, start, end); } }); } /** * * lpush操作 * */ public Long lpush(final String key, final String... strings) { return shardedClient.execute(new ShardedJedisAction () { @Override public Long doAction(ShardedJedis shardedJedis) { return shardedJedis.lpush(key, strings); } }); } /** * * rpush操作 * */ public Long rpush(final String key, final String... strings) { return shardedClient.execute(new ShardedJedisAction () { @Override public Long doAction(ShardedJedis shardedJedis) { return shardedJedis.rpush(key, strings); } }); } /** * * lrem操作 :根据参数 count 的值,移除列表中与参数 value 相等的元素,返回被移除元素的数量 * */ public Long lrem(final String key, final long count, final String value) { return shardedClient.execute(new ShardedJedisAction () { @Override public Long doAction(ShardedJedis shardedJedis) { return shardedJedis.lrem(key, count, value); } }); } /** * * lrange操作 * */ public List lrange(final String key, final long start, final long end) { return shardedClient.execute(new ShardedJedisAction >() { @Override public List
doAction(ShardedJedis shardedJedis) { return shardedJedis.lrange(key, start, end); } }); } /** * * llen操作 * */ public Long llen(final String key) { return shardedClient.execute(new ShardedJedisAction () { @Override public Long doAction(ShardedJedis shardedJedis) { return shardedJedis.llen(key); } }); } /** * * type操作,获取key是哪一种数据存储类型 * * @param key */ public String type(final String key) { return shardedClient.execute(new ShardedJedisAction () { @Override public String doAction(ShardedJedis shardedJedis) { return shardedJedis.type(key); } }); } /** * * smembers操作 * * @param key */ public Set smembers(final String key) { return shardedClient.execute(new ShardedJedisAction >() { @Override public Set doAction(ShardedJedis shardedJedis) { return shardedJedis.smembers(key); } }); } /** * * lrange操作 */ public List lrange(final String key) { return shardedClient.execute(new ShardedJedisAction >() { @Override public List
doAction(ShardedJedis shardedJedis) { return shardedJedis.lrange(key, 0L, shardedJedis.llen(key) - 1L); } }); } /** * * ttl操作 * */ public Long ttl(final String key) { return shardedClient.execute(new ShardedJedisAction () { @Override public Long doAction(ShardedJedis shardedJedis) { return shardedJedis.ttl(key); } }); } /** * * 应用在分布式锁场景中 * * @param key 需要setnx的key * @param value key对应的值 */ public long setnx(final String key, final String value) { return shardedClient.execute(new ShardedJedisAction () { @Override public Long doAction(ShardedJedis shardedJedis) { long tmpVal = shardedJedis.setnx(key, value); return tmpVal; } }); } /** * * 应用在分布式锁场景中 * * @param key getSet * @param value key对应的值 */ public String getSet(final String key, final String value) { String retValue = shardedClient.execute(new ShardedJedisAction () { @Override public String doAction(ShardedJedis shardedJedis) { String tmpVal = shardedJedis.getSet(key, value); return tmpVal; } }); return retValue; } /** * * 获取所有的key * * @param key * @return */ public Set keys(final String key) { return shardedClient.execute(new ShardedJedisAction >() { @Override public Set doAction(ShardedJedis shardedJedis) { Set keySet = new HashSet (); Collection jedisList = shardedJedis.getAllShards(); for (Jedis jedis : jedisList) { Set keys = jedis.keys(key); keySet.addAll(keys); } return keySet; } }); } }
bigDecimal的用法:
/** * SUNING APPLIANCE CHAINS. * Copyright (c) 2014-2014 All Rights Reserved. */ package com.suning.sdipospc.util; import org.apache.commons.lang3.StringUtils; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; /** * 〈一句话功能简述〉 BigDecimal类型数据处理的常用工具类 * 〈功能详细描述〉 * * @author 17040683 * @see [相关类/方法](可选) * @since [产品/模块版本] (可选) * @Date 2017年6月3日 上午9:09:10 */ public class BigDecimalUtils { /** * BigDecimal精确一个小数 */ public static final int ONE_DECIMAL_PLACES = 1; /** * BigDecimal精确两个小数 */ public static final int TWO_DECIMAL_PLACES = 2; /** * BigDecimal精确三个小数 */ public static final int THREE_DECIMAL_PLACES = 3; /** * POS默认最小金额(门店只能收到1角钱) */ public static final BigDecimal POS_DEFAULT_BIGDECIMAL = new BigDecimal("0.1"); /** * POS默认最小金额(发票最少1元钱) */ public static final BigDecimal POS_DEFAULT_INVOCIE_BIGDECIMAL = new BigDecimal("1"); /** * POS默认最小金额(门店只能收到1角钱) */ public static final String POS_DEFAULT_STRING = "0.1"; /** * 默认的保留两位小数的bigdecimal金额 */ public static final BigDecimal ZERO_BIGDECIMAL = new BigDecimal("0.00"); /** * 默认的保留三位小数的bigdecimal金额 */ public static final BigDecimal ZERO_BIGDECIMAL_THREE = new BigDecimal("0.000"); /** * 默认的保留一位小数的bigdecimal金额 */ public static final BigDecimal ZERO_BIGDECIMAL_1 = new BigDecimal("0.0"); /** * 0.1 */ public static final BigDecimal ZERO_POINT_ONE = new BigDecimal("0.1"); /** * 默认的不带小数的bigdecimal金额0 */ public static final BigDecimal ZERO_BIGDECIMAL_0 = new BigDecimal("0"); /** * 默认的不带小数的bigdecimal金额1 */ public static final BigDecimal ONE_BIGDECIMAL_1 = new BigDecimal("1"); /** * 默认的不带小数的bigdecimal金额2 */ public static final BigDecimal TWO_BIGDECIMAL_2 = new BigDecimal("2"); /** * 默认的不带小数的bigdecimal金额3 */ public static final BigDecimal THREE_BIGDECIMAL_3 = new BigDecimal("3"); /** * 默认不带小数的String金额 */ public static final String ZERO_STRING_0 = "0"; /** * 默认的保留1位小数的String金额 */ public static final String ZERO_STRING_1 = "0.0"; /** * 默认的保留两位小数的String金额 */ public static final String ZERO_STRING_2 = "0.00"; /** * pos默认发票的String金额 */ public static final String POS_DEFAULT_INVOICE_STRING = "1"; /** * 默认的无小数的金额 */ public static final int ZERO_INT = 0; /** * 功能描述: * 〈将BigDecimal值保留一位小数,四舍五入〉 * * @param num * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static BigDecimal HalfUpOneDecimal(BigDecimal num) { return num.setScale(ONE_DECIMAL_PLACES, RoundingMode.HALF_UP); } /** * 功能描述: * 〈将BigDecimal值保留两位小数,四舍五入〉 * * @param num * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static BigDecimal HalfUpTwoDecimal(BigDecimal num) { return num.setScale(TWO_DECIMAL_PLACES, RoundingMode.HALF_UP); } /** * 功能描述: * 〈将BigDecimal值保留三位小数,四舍五入〉 * * @param num * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static BigDecimal HalfUpThreeDecimal(BigDecimal num) { return num.setScale(THREE_DECIMAL_PLACES, RoundingMode.HALF_UP); } /** * 功能描述:当字符金额精度大于制定的精度时,按照舍入的规则保留指定标度 * * @param scale * @param bigDecimalString 金额字符创 * 返回值: 类型 <说明> * @return 返回值 */ public static String convertBdStringPrecision(int scale, String bigDecimalString) { if (StringUtils.isBlank(bigDecimalString)) { return bigDecimalString; } else { BigDecimal bd = new BigDecimal(bigDecimalString); //取标度 int bdscale = bd.scale(); if (bdscale > scale) { return bd.setScale(scale, BigDecimal.ROUND_DOWN).toString(); } else { return bigDecimalString; } } } /** * 功能描述: 将BigDecimal金额按照四舍五入的规则转化为自定义精度的string类型金额 * * @param num * @param bd * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static String bdConvertString(int num, BigDecimal bd) { if (bd == null) { if (0 == num) { return ZERO_STRING_0; } else if (1 == num) { return ZERO_STRING_1; } else { return ZERO_STRING_2; } } return bd.setScale(num, BigDecimal.ROUND_HALF_UP).toString(); } /** * 功能描述:将BigDecimal金额按照佘位的规则转化为自定义精度的string类型金额 * 输入参数:<按照参数定义顺序> * * @param num * @param bd * @return 返回值 */ public static String bdRoundDownToString(int num, BigDecimal bd) { if (bd == null) { if (0 == num) { return ZERO_STRING_0; } else if (1 == num) { return ZERO_STRING_1; } else { return ZERO_STRING_2; } } return bd.setScale(num, BigDecimal.ROUND_DOWN).toString(); } /** * 功能描述:将Object金额按照佘位规则转化为自定义精度的BigDecimal类型金额 默认精度为两位小数 * * @param obj 必须又阿拉伯数字或小数点组成 * @return BigDecimal * @throw NumberFormatException if {@code val} is not a valid * representation of a {@code BigDecimal} */ public static BigDecimal objConvertBd(Object obj) { if (null == obj) { return ZERO_BIGDECIMAL; } if (obj instanceof String) { if (StringUtils.isBlank((CharSequence) obj)) { return ZERO_BIGDECIMAL; } else if (StringUtils.equals((CharSequence) obj, "null")) { return ZERO_BIGDECIMAL; } } if (obj instanceof BigDecimal) { return ((BigDecimal) obj).setScale(TWO_DECIMAL_PLACES, BigDecimal.ROUND_DOWN); } BigDecimal bd = new BigDecimal(String.valueOf(obj)); return bd.setScale(TWO_DECIMAL_PLACES, BigDecimal.ROUND_DOWN); } /** * 功能描述:将Object金额按照佘位规则转化为自定义精度的BigDecimal类型金额 若入参为空默认精度为两位小数 不可空 则转化为定义的精度 * * @param num 精度位数 * @param obj 必须又阿拉伯数字或小数点组成 * @return BigDecimal * @throw NumberFormatException if {@code val} is not a valid * representation of a {@code BigDecimal} */ public static BigDecimal objConvertBd(int num, Object obj) { if (null == obj) { return ZERO_BIGDECIMAL; } if (obj instanceof String) { if (StringUtils.isBlank((CharSequence) obj)) { return ZERO_BIGDECIMAL; } else if (StringUtils.equals((CharSequence) obj, "null")) { return ZERO_BIGDECIMAL; } } if (obj instanceof BigDecimal) { return ((BigDecimal) obj).setScale(num, BigDecimal.ROUND_DOWN); } BigDecimal bd = new BigDecimal(String.valueOf(obj)); return bd.setScale(num, BigDecimal.ROUND_DOWN); } /** * 比较大小,若orginalBd大于targtBd则返回true,否则返回false 功能描述: * 〈功能详细描述〉 * * @param orginalBd * @param targtBd * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static boolean isMoreBd(BigDecimal orginalBd, int targtBd) { return isMoreBd(orginalBd, new BigDecimal(targtBd)); } /** * 功能描述: 比较大小,若orginalBd大于targtBd则返回true,否则返回false * 〈功能详细描述〉 * * @param orginalBd * @param targtBd * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static boolean isMoreBd(BigDecimal orginalBd, BigDecimal targtBd) { if (null == orginalBd) { orginalBd = ZERO_BIGDECIMAL; } if (null == targtBd) { targtBd = ZERO_BIGDECIMAL; } return orginalBd.compareTo(targtBd) == 1 ? true : false; } /** * 功能描述: 比较大小,若orginalBd大于等于targtBd则返回true,否则返回false * 〈功能详细描述〉 * * @param orginalBd * @param targtBd * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static boolean isMoreOrEqualBd(BigDecimal orginalBd, BigDecimal targtBd) { if (null == orginalBd) { orginalBd = ZERO_BIGDECIMAL; } if (null == targtBd) { targtBd = ZERO_BIGDECIMAL; } return orginalBd.compareTo(targtBd) >= 0 ? true : false; } /** * 功能描述:判断是否大于0 * * @param orginalBd * @return true or false * @throw 异常描述 */ public static boolean isMoreZero(BigDecimal orginalBd) { if (null != orginalBd) { return orginalBd.compareTo(ZERO_BIGDECIMAL) == 1 ? true : false; } else { return false; } } /** * 功能描述:判断是否大于0.1 * * @param orginalBd * @return true or false * @throw 异常描述 */ public static boolean isMoreZeroPointOne(BigDecimal orginalBd) { if (null != orginalBd) { return formatDecimal2CurrencyNumber(orginalBd).compareTo(ZERO_POINT_ONE) > 0; } else { return false; } } /** * 功能描述:判断是否小于0 * * @param orginalBd * @return true or false * @throw 异常描述 */ public static boolean isLessZero(BigDecimal orginalBd) { if (null != orginalBd) { return orginalBd.compareTo(ZERO_BIGDECIMAL) == -1 ? true : false; } else { return false; } } /** * 功能描述:判断是否等于0 * * @param orginalBd * @return true or false * @throw 异常描述 */ public static boolean isEqualZero(BigDecimal orginalBd) { if (null != orginalBd) { return orginalBd.compareTo(ZERO_BIGDECIMAL) == 0 ? true : false; } else { return false; } } /** * 功能描述:判断是否小于等于0 * * @param orginalBd * @return true or false * @throw 异常描述 */ public static boolean isLessOrEqualZero(BigDecimal orginalBd) { return !isMoreZero(orginalBd); } /** * 功能描述: 比较大小,若orginalBd小于等于targtBd则返回true,否则返回false * 〈功能详细描述〉 * * @param orginalBd * @param targtBd * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static boolean isLessOrEqualBd(BigDecimal orginalBd, BigDecimal targtBd) { if (null == orginalBd) { orginalBd = ZERO_BIGDECIMAL; } if (null == targtBd) { targtBd = ZERO_BIGDECIMAL; } return orginalBd.compareTo(targtBd) <= 0 ? true : false; } /** * 功能描述: 比较大小,若orginalBd小于targtBd则返回true,否则返回false * 〈功能详细描述〉 * * @param orginalBd * @param targtBd * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static boolean isLessBd(BigDecimal orginalBd, BigDecimal targtBd) { if (null == orginalBd) { orginalBd = ZERO_BIGDECIMAL; } if (null == targtBd) { targtBd = ZERO_BIGDECIMAL; } return orginalBd.compareTo(targtBd) < 0 ? true : false; } /** * 比较大小,若orginalBd等于targtBd则返回true,否则返回false 功能描述: * 〈功能详细描述〉 * * @param orginalBd * @param targtBd * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static boolean isEqualsBd(BigDecimal orginalBd, int targtBd) { if (null == orginalBd) { orginalBd = ZERO_BIGDECIMAL; } return orginalBd.compareTo(new BigDecimal(targtBd)) == 0 ? true : false; } /** * 比较大小,若orginalBd等于targtBd则返回true,否则返回false 功能描述: * 〈功能详细描述〉 * * @param orginalBd * @param targtBd * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static boolean isEquals(BigDecimal orginalBd, BigDecimal targtBd) { if (null == orginalBd) { orginalBd = ZERO_BIGDECIMAL; } if (null == targtBd){ targtBd = ZERO_BIGDECIMAL; } return orginalBd.compareTo(targtBd) == 0 ? true : false; } /** * 比较大小,若orginalBd不等于targtBd则返回true,否则返回false 功能描述: * 〈功能详细描述〉 * * @param orginalBd * @param targtBd * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static boolean isNotEquals(BigDecimal orginalBd, BigDecimal targtBd) { return !isEquals(orginalBd, targtBd); } /** * 功能描述:如果orginalBd等于0则返回空,否则返回根据四舍五入规则截取的保留0位小数的string字符串 * * @param orginalBd BigDecimal * @return String * @throw 异常描述 */ public static String defaultIfZero(BigDecimal orginalBd) { return isEqualsBd(orginalBd, 0) == true ? "" : bdConvertString(0, orginalBd); } /** * 功能描述:将double类型按照佘位规则转化为bigdecimal类型 * * @param num * @param db * @return 返回值 */ public static BigDecimal dbConvertBd(int num, Double db) { if (db == null) { return ZERO_BIGDECIMAL; } return new BigDecimal(db).setScale(num, BigDecimal.ROUND_DOWN); } /** * 功能描述:转换成POS的到角的价格精度(ps:POS大陆店价格只可到角 暂不考虑香港店到元的情况) 不支持处理为负的金额 * 若金额精度为分 ps: 0.01-0.09 则进一位到角 返回0.1 若金额精度有角有分 ps:0.12 则舍去一位返回 0.1 * * @param bd * @return 返回值 */ public static String convertPosJiaoScale(BigDecimal bd) { if (bd == null) { return ZERO_STRING_1; } int bdScale = bd.scale(); //如果精度小于等1则表示进度到角,无需处理直接返回 if (bdScale <= 1) { return bd.toString(); } else { // 如果大于0 if (isMoreZero(bd)) { //小于等于0.1的 则视为0.1 if (isLessOrEqualBd(bd, POS_DEFAULT_BIGDECIMAL)) { return POS_DEFAULT_STRING; } else { //大于0.1 则 舍去小数点1位以后的位数 return bd.setScale(1, BigDecimal.ROUND_DOWN).toString(); } } else { return bd.toString(); } } } /** * 功能描述:转换成POS的到元的价格精度(ps:线下记收入发票金额至少1元) 不支持处理为负的金额 * 若金额精度为分 ps: 0.0001...-0.9999... 则进一位到元 返回1 若金额精度有角有分 ps:1.12 则舍去一位返回 1.1 * * @param bd * @return 返回值 */ public static String convertPosYuanScale(BigDecimal bd) { if (bd == null) { return ZERO_STRING_0; } int bdScale = bd.scale(); //如果精度小于1则表示进度到元,无需处理直接返回 if (bdScale < 1) { return bd.toString(); } else { // 精度如果大于1 // 大于0 if (isMoreZero(bd)) { //小于等于1的 则视为1 if (isLessOrEqualBd(bd, POS_DEFAULT_INVOCIE_BIGDECIMAL)) { return POS_DEFAULT_INVOICE_STRING; } else { //大于1 则 舍去小数点1位以后的位数 return bd.setScale(1, BigDecimal.ROUND_DOWN).toString(); } } else { return bd.toString(); } } } /** * 用于计算用券金额时,保留一位小数,舍入模式RoundingMode.DOWN * 10.999 = 10.9;10.921 = 10.9 * 功能描述: * 〈功能详细描述〉 * * @param bd1 * @param bd2 * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static BigDecimal couponMultiply(BigDecimal bd1, BigDecimal bd2) { BigDecimal rtn = null; if (bd1 == null || bd2 == null) { rtn = ZERO_BIGDECIMAL_1; } else { rtn = bd1.multiply(bd2).setScale(1, RoundingMode.DOWN); } return rtn; } public static BigDecimal multiply(BigDecimal bd1, BigDecimal bd2) { BigDecimal rtn = null; if (bd1 == null || bd2 == null) { rtn = ZERO_BIGDECIMAL; } else { rtn = bd1.multiply(bd2).setScale(2, RoundingMode.DOWN); } return rtn; } /** * 用于计算商品价格--精度1,舍入模式RoundingMode.DOWN * 1/3 = 0.3; 2/3 = 0.6 * 功能描述: * 〈功能详细描述〉 * * @param bd1 商品销售金额 * @param pisor * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static BigDecimal productDivide(BigDecimal bd1, BigDecimal pisor) { BigDecimal rtn = null; if (bd1 == null || pisor == null || isEquals(pisor, BigDecimal.ZERO)) { rtn = ZERO_BIGDECIMAL_1; } else { rtn = bd1.pide(pisor, 1, RoundingMode.DOWN); } return rtn; } /** * 用于计算商品价格--精度1,舍入模式RoundingMode.UP * 1/3 = 0.3; 2/3 = 0.6 * 功能描述: * 〈功能详细描述〉 * * @param bd1 商品销售金额 * @param pisor * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static BigDecimal productDivideUpmodel(BigDecimal bd1, BigDecimal pisor) { BigDecimal rtn = null; if (bd1 == null || pisor == null || isEquals(pisor, BigDecimal.ZERO)) { rtn = ZERO_BIGDECIMAL_1; } else { rtn = bd1.pide(pisor, 1, RoundingMode.UP); } return rtn; } /** * 格式化金额为两位小数 * * @param input BigDecimal 类型 * @return */ public static BigDecimal formatDecimal2CurrencyNumber(BigDecimal input) { DecimalFormat format = new DecimalFormat(ZERO_STRING_2); return new BigDecimal(format.format(input)); } /** * 格式化金额为两位小数 * * @param input 整形类型 * @return */ public static BigDecimal formatInteger2CurrencyNumber(Integer input) { DecimalFormat format = new DecimalFormat(ZERO_STRING_2); return new BigDecimal(format.format(input)); } /** * 两个bigdecimal类型数据相加 * @param orginalBd * @param targetBd * @return BigDecimal */ public static BigDecimal add(BigDecimal orginalBd, BigDecimal targetBd) { if(null != orginalBd && null != targetBd){ return orginalBd.add(targetBd); }else{ return objConvertBd(orginalBd).add(objConvertBd(targetBd)); } } }
编码方式 工具类
/** * 封装各种格式的编码解码工具类. *
* 1.Commons-Codec的 hex/base64 编码 2.自制的base62 编码 3.Commons-Lang的xml/html escape 4.JDK提供的URLEncoder * * @author 袁兵 15041220 */ public class EncodeUtils { private EncodeUtils() { } private static final String DEFAULT_URL_ENCODING = "UTF-8"; private static final char[] BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray(); /** * Hex编码. */ public static String encodeHex(byte[] input) { return Hex.encodeHexString(input); } /** * Hex解码. */ public static byte[] decodeHex(String input) { try { return Hex.decodeHex(input.toCharArray()); } catch (DecoderException e) { throw ExceptionUtils.unchecked(e); } } /** * Base64编码. */ public static String encodeBase64(byte[] input) { return Base64.encodeBase64String(input); } /** * Base64编码, URL安全(将Base64中的URL非法字符'+'和'/'转为'-'和'_', 见RFC3548). */ public static String encodeUrlSafeBase64(byte[] input) { return Base64.encodeBase64URLSafeString(input); } /** * Base64解码. */ public static byte[] decodeBase64(String input) { return Base64.decodeBase64(input); } /** * Base62编码。 */ public static String encodeBase62(byte[] input) { char[] chars = new char[input.length]; for (int i = 0; i < input.length; i++) { chars[i] = BASE62[(input[i] & 0xFF) % BASE62.length]; } return new String(chars); } /** * Html 转码. */ public static String escapeHtml(String html) { return StringEscapeUtils.escapeHtml4(html); } /** * Html 解码. */ public static String unescapeHtml(String htmlEscaped) { return StringEscapeUtils.unescapeHtml4(htmlEscaped); } /** * Xml 转码. */ public static String escapeXml(String xml) { return StringEscapeUtils.escapeXml(xml); } /** * Xml 解码. */ public static String unescapeXml(String xmlEscaped) { return StringEscapeUtils.unescapeXml(xmlEscaped); } /** * URL 编码, Encode默认为UTF-8. */ public static String urlEncode(String part) { try { return URLEncoder.encode(part, DEFAULT_URL_ENCODING); } catch (UnsupportedEncodingException e) { throw ExceptionUtils.unchecked(e); } } /** * URL 解码, Encode默认为UTF-8. */ public static String urlDecode(String part) { try { return URLDecoder.decode(part, DEFAULT_URL_ENCODING); } catch (UnsupportedEncodingException e) { throw ExceptionUtils.unchecked(e); } } }
登陆使用工具类
package com.suning.sdipospc.util; import goja.QRCode; import goja.QRCodeFormat; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang.StringUtils; import org.apache.commons.net.util.Base64; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; /** * 登录工具类 * * @author 17040365 * */ public class LoginUtils { /** 日志对象 **/ private static final Logger logger = LoggerFactory .getLogger(LoginUtils.class); private static final int QRCODESIZE = 160; private LoginUtils() { } /** * 生成二维码 * * @param macAddress * @return * @throws IOException */ public static String getQrCodeImage(String macAddress) throws IOException { QRCodeFormat codeFormat = QRCodeFormat.NEW(); codeFormat.setSize(QRCODESIZE); if (StringUtils.isNotBlank(macAddress)) { BufferedImage bi = QRCode.toQRCode(macAddress, codeFormat); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bi, "jpg", baos); byte[] bytes = baos.toByteArray(); return Base64.encodeBase64String(bytes).trim(); } else { return null; } } /** * 获取远程MAC地址 * * @return */ public static String getRemoteMac() { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder .getRequestAttributes()).getRequest(); return getRemoteMac(request); } /** * 获取操作系统名称 */ public static String getOsName() { return System.getProperty("os.name"); } /** * 获取远程MAC地址 * * @param request * @return */ public static String getRemoteMac(HttpServletRequest request) { String ip = getIpAddress(request); logger.info("============remote ip=======" + ip); String os = System.getProperty("os.name"); String mac = ""; if (os.startsWith("Windows")) { mac = getMACAddressWindows(ip); } else if (os.startsWith("Linux")) { try { mac = getMACAddressLinux(ip); } catch (Exception e) { logger.error("linux os get mac address fail", e); } } return mac; } /** * 获取IP地址 * * @param request * @return */ public static String getIpAddress(HttpServletRequest request) { String ip = request.getHeader("X-Forwarded-For"); if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) { // 多次反向代理后会有多个ip值,第一个ip才是真实ip int index = ip.indexOf(','); if (index != -1) { return ip.substring(0, index); } else { return ip; } } ip = request.getHeader("X-Real-IP"); if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) { return ip; } return request.getRemoteAddr(); } /** * 通过远程IP获取MAC地址(windows) * * @param ip * 远程IP地址 * @return */ public static String getMACAddressWindows(String ip) { String str = ""; String macAddress = ""; try { Process p = Runtime.getRuntime().exec("nbtstat -a " + ip); InputStreamReader ir = new InputStreamReader(p.getInputStream(), "GBK"); LineNumberReader input = new LineNumberReader(ir); logger.info("nbtstat -a " + ip + "\n mac info:" + str); while ((str = input.readLine()) != null) { if (str.indexOf("MAC 地址", 1) > -1) { // 客户端使用的是中文版操作系统 macAddress = str.substring(str.indexOf("MAC 地址") + 9, str.length()); break; } else if (str.indexOf("MAC Address", 1) > -1) { // 客户端使用的是英文版操作系统 macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length()); break; } } } catch (IOException e) { logger.error("=========获取远程mac地址异常=======IOException:", e); } logger.info("客户端MAC地址:" + macAddress); return macAddress; } /** * 执行单条指令 * * @param cmd * 命令 * @return 执行结果 * @throws Exception */ public static String command(String cmd) throws Exception { Process process = Runtime.getRuntime().exec(cmd); process.waitFor(); InputStream in = process.getInputStream(); StringBuilder result = new StringBuilder(); byte[] data = new byte[256]; while (in.read(data) != -1) { String encoding = System.getProperty("sun.jnu.encoding"); logger.info("===========system encodeing========="+ encoding); result.append(new String(data, encoding)); } return result.toString(); } /** * 获取mac地址(linux) * * @param ip * @return * @throws Exception */ public static String getMACAddressLinux(String ip) throws Exception { String result = command("ping " + ip + " -n 2"); if (result.contains("TTL")) { result = command("arp -a " + ip); } String regExp = "([0-9A-Fa-f]{2})([-:][0-9A-Fa-f]{2}){5}"; Pattern pattern = Pattern.compile(regExp); Matcher matcher = pattern.matcher(result); StringBuilder mac = new StringBuilder(); while (matcher.find()) { String temp = matcher.group(); mac.append(temp); } return mac.toString(); } }
NSFDateUtil
/** * * 日期公用类 */ public class NSFDateUtils { /** * 日志 */ private static final Logger LOGGER = LoggerFactory.getLogger(NSFDateUtils.class); /** yyyy-MM-dd时间格式 */ public static final String FORMAT_10 = "yyyy-MM-dd"; /** yyyy/MM/dd时间格式 */ public static final String FORMAT2_10 = "yyyy/MM/dd"; /** yyyyMMdd时间格式 */ public static final String FORMAT_8 = "yyyyMMdd"; /** yyyyMMddHHmmss时间格式 */ public static final String FORMAT_14 = "yyyyMMddHHmmss"; /** yyyy-MM-dd HH:mm:ss时间格式 */ public static final String FORMAT_19 = "yyyy-MM-dd HH:mm:ss"; /** yyyy-MM-dd HH:mm时间格式 */ public static final String FORMAT_16 = "yyyy-MM-dd HH:mm"; /** yyyy-MM-dd hh:mm:ss.SSS 毫秒 */ public static final String format22 = "yyyy-MM-dd HH:mm:ss.SSS"; /** * pos上午时间段 */ private static final long POS_AM_TIME = 90000; /** * pos中午时间段 */ private static final long POS_NOON_TIME = 120000; /** * pos下午时间段 */ private static final long POS_PM_TIME = 180000; /** * 09:00—12:00 */ public static final String AM_CHINESE = "上午"; /** * 12:00-18:00 */ public static final String PM_CHINESE = "下午"; public static final String ALL_DAY_CHINESE = "全天"; /** * 周末 */ public static final String[] WEEKENDS = { "周日", "周六" }; public static String getChar8() { return DateFormatUtils.format(new Date(), FORMAT_8); } public static String getChar8(Timestamp time) { return DateFormatUtils.format(time.getTime(), FORMAT_8); } public static String getChar14() { return DateFormatUtils.format(new Date(), "yyyyMMddHHmmss"); } public static String getChar19() { return DateFormatUtils.format(new Date(), FORMAT_19); } public static String toChar(String datestr) { return datestr.replaceAll("-", "").replaceAll(":", ""); } /** * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss * * @param strDate * @return */ public static Date strToDateLong(String strDate, String format) { SimpleDateFormat formatter = new SimpleDateFormat(format); ParsePosition pos = new ParsePosition(0); Date strtodate = formatter.parse(strDate, pos); return strtodate; } /** * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss * * @param strDate * @return */ public static Date strToDate(String strDate) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ParsePosition pos = new ParsePosition(0); Date strtodate = formatter.parse(strDate, pos); return strtodate; } /** * 将长时间格式时间转换为字符串 * * @param dateDate * @return */ public static String dateToStrLong(java.util.Date dateDate, String format) { SimpleDateFormat formatter = new SimpleDateFormat(format); String dateString = formatter.format(dateDate); return dateString; } /** * 格式化字符串日期由YYYY-MM-DD 转为 YYYYMMDD * * @param dateDate * @return */ public static String convertDateString(String date) { return StringUtils.replace(date, "-", ""); } /** * 取当前时间的字符串,精确到分钟 * * @return 取当前时间字符串,格式为“yyyyMMddHHmm” */ public static String getChar12() { return DateFormatUtils.format(new Date(), "yyyyMMddHHmm"); } public static String getChar6() { return DateFormatUtils.format(new Date(), "HHmmss"); } public static String formatChar14(String char14) { if (char14 == null || char14.length() == 0) { return char14; } return char14.substring(0, 4) + "-" + char14.substring(4, 6) + "-" + char14.substring(6, 8) + " " + char14.substring(8, 10) + ":" + char14.substring(10, 12) + ":" + char14.substring(12, 14) + " "; } public static String formatChar12(String char14) { if (char14 == null || char14.length() == 0) { return char14; } return char14.substring(0, 4) + "-" + char14.substring(4, 6) + "-" + char14.substring(6, 8) + " " + char14.substring(8, 10) + ":" + char14.substring(10, 12); } /** * 判断是否超过指定的结束时间 * * @param srcBeginDate String 开始时间(yyy-MM-dd) * @param srcEndDate String 结束时间(有两种可能性:1:相对开始时间后的天数或者月数,2:绝对的结束时间)开始时间(yyy-MM-dd) * @param relatetivelyFlag int 相对标志(1:天数,2:月数) * @return boolean */ public static boolean judgeIfExceedEndDate(String srcBeginDate, String srcEndDate, int relatetivelyFlag) { if (srcEndDate.trim().length() != 8) { Calendar cal = Calendar.getInstance(); cal.set(Integer.parseInt(srcBeginDate.substring(0, 4)), Integer.parseInt(srcBeginDate.substring(5, 7)), Integer.parseInt(srcBeginDate.substring(8, 10))); if (relatetivelyFlag == 1) { cal.roll(Calendar.DAY_OF_YEAR, Integer.parseInt(srcEndDate)); cal.roll(Calendar.YEAR, Integer.parseInt(srcEndDate) / 365); } else if (relatetivelyFlag == 2) { cal.roll(Calendar.MONTH, Integer.parseInt(srcEndDate)); cal.roll(Calendar.YEAR, Integer.parseInt(srcEndDate) / 12); } srcEndDate = formatToChar8(cal); } if (Long.parseLong(getChar8()) >= Long.parseLong(srcEndDate)) { return true; } return false; } /** * 将当前日期向后滚动n个月 * * @param srcDate String 当前日期 * @param rollMonth String 待滚动的月数 * @return String */ public static String rollMonth(String srcDate, String rollMonth) { Calendar cal = Calendar.getInstance(); cal.set(Integer.parseInt(srcDate.substring(0, 4)), Integer.parseInt(srcDate.substring(4, 6)) - 1, Integer.parseInt(srcDate.substring(6, 8))); cal.roll(Calendar.MONTH, Integer.parseInt(rollMonth)); cal.roll(Calendar.YEAR, Integer.parseInt(rollMonth) / 12); return formatToChar8(cal); } /** * 将当前日期向前滚动n个月 * * @param srcDate String 当前日期 * @param rollMonth String 待滚动的月数 * @return String */ public static String preMonth(String srcDate, String rollMonth) { Calendar cal = Calendar.getInstance(); cal.set(Integer.parseInt(srcDate.substring(0, 4)), Integer.parseInt(srcDate.substring(4, 6)) - 1, Integer.parseInt(srcDate.substring(6, 8))); cal.add(Calendar.MONTH, Integer.parseInt(rollMonth)); cal.add(Calendar.YEAR, Integer.parseInt(rollMonth) / 12); return formatToChar8(cal); } public static String formatToChar8(Calendar tmpcal) { String tmpYear = Integer.toString(tmpcal.get(Calendar.YEAR)); String tmpMonth = Integer.toString(tmpcal.get(Calendar.MONTH) + 1); String tmpDay = Integer.toString(tmpcal.get(Calendar.DAY_OF_MONTH)); String tmpDate = tmpYear + (tmpMonth.length() == 1 ? "0" + tmpMonth : tmpMonth) + (tmpDay.length() == 1 ? "0" + tmpDay : tmpDay); return tmpDate; } public static String formatChar8(String char8) { if (char8 == null || char8.length() == 0) { return char8; } return char8.substring(0, 4) + "-" + char8.substring(4, 6) + "-" + char8.substring(6, 8) + " "; } public static String formatCha6(String char6) { if (char6 == null || char6.length() == 0) { return char6; } return char6.substring(0, 2) + ":" + char6.substring(2, 4) + ":" + char6.substring(4, 6); } /** * 获取指定日期 向前或向后滚动特定小时数后的日期 * * @param nowDate String 当前日期 * @param rollDate String 待滚动的月数 * @return String 指定日期 +/- 特定天数 后的日期(格式 CCYYMMDD) */ public static String rollHour(String nowDate, int rollDate) { String dateReturn = ""; if (nowDate == null || nowDate.trim().length() < 14) { return dateReturn; } String dateNow = nowDate.trim(); Calendar cal = Calendar.getInstance(); int nYear = Integer.parseInt(dateNow.substring(0, 4)); int nMonth = Integer.parseInt(dateNow.substring(4, 6)); int nDate = Integer.parseInt(dateNow.substring(6, 8)); int nHour = Integer.parseInt(dateNow.substring(8, 10)); int nMinute = Integer.parseInt(dateNow.substring(10, 12)); int nSecond = Integer.parseInt(dateNow.substring(12, 14)); cal.set(nYear, nMonth - 1, nDate, nHour, nMinute, nSecond); cal.add(Calendar.HOUR_OF_DAY, rollDate); String strYear = String.valueOf(cal.get(Calendar.YEAR)); String strMonth = String.valueOf(cal.get(Calendar.MONTH) + 1); String strDay = String.valueOf(cal.get(Calendar.DAY_OF_MONTH)); String strHour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY)); String strMinute = String.valueOf(cal.get(Calendar.MINUTE)); String strSecond = String.valueOf(cal.get(Calendar.SECOND)); strMonth = (strMonth.length() == 1) ? "0" + strMonth : strMonth; strDay = (strDay.length() == 1) ? "0" + strDay : strDay; strHour = (strHour.length() == 1) ? "0" + strHour : strHour; strMinute = (strMinute.length() == 1) ? "0" + strMinute : strMinute; strSecond = (strSecond.length() == 1) ? "0" + strSecond : strSecond; dateReturn = strYear + strMonth + strDay + strHour + strMinute + strSecond; return dateReturn; } /** * 获取指定日期 向前或向后滚动特定天数后的日期 * * @param nowDate String 当前日期 * @param rollDate String 待滚动的天数 * @return String 指定日期 +/- 特定天数 后的日期(格式 YYMMDD) */ public static String rollDate(String nowDate, int rollDate) { String dateReturn = ""; if (nowDate == null || nowDate.trim().length() < 8) { return dateReturn; } String dateNow = nowDate.trim(); Calendar cal = Calendar.getInstance(); int nYear = Integer.parseInt(dateNow.substring(0, 4)); int nMonth = Integer.parseInt(dateNow.substring(4, 6)); int nDate = Integer.parseInt(dateNow.substring(6, 8)); cal.set(nYear, nMonth - 1, nDate); cal.add(Calendar.DATE, rollDate); String strYear = String.valueOf(cal.get(Calendar.YEAR)); String strMonth = String.valueOf(cal.get(Calendar.MONTH) + 1); String strDay = String.valueOf(cal.get(Calendar.DAY_OF_MONTH)); strMonth = (strMonth.length() == 1) ? "0" + strMonth : strMonth; strDay = (strDay.length() == 1) ? "0" + strDay : strDay; dateReturn = strYear + strMonth + strDay; return dateReturn; } /** * 获取指定日期 向前或向后滚动特定天数后的日期 * * @param nowDate String 当前日期 * @param rollDate String 待滚动的天数 * @return String 指定日期 +/- 特定天数 后的日期(格式 YYYY-MM-DD) */ public static String rollDate2(String nowDate, int rollDate) { String dateReturn = ""; if (nowDate == null || nowDate.trim().length() < 8) { return dateReturn; } String dateNow = nowDate.trim(); Calendar cal = Calendar.getInstance(); int nYear = Integer.parseInt(dateNow.substring(0, 4)); int nMonth = Integer.parseInt(dateNow.substring(5, 7)); int nDate = Integer.parseInt(dateNow.substring(8, 10)); cal.set(nYear, nMonth - 1, nDate); cal.add(Calendar.DATE, rollDate); String strYear = String.valueOf(cal.get(Calendar.YEAR)); String strMonth = String.valueOf(cal.get(Calendar.MONTH) + 1); String strDay = String.valueOf(cal.get(Calendar.DAY_OF_MONTH)); strMonth = (strMonth.length() == 1) ? "0" + strMonth : strMonth; strDay = (strDay.length() == 1) ? "0" + strDay : strDay; dateReturn = strYear + "-" + strMonth + "-" + strDay; return dateReturn; } /** * 将完整的时间格式格式化成char14位的 * * @param fullTime String * @return String */ public static String formatFullTimeToChar14(String fullTime) { return StringUtils.replace( StringUtils.replace(StringUtils.replace(StringUtils.replace(fullTime, "-", ""), ":", ""), " ", ""), ".", ""); } /****** * 返回当天所在的年份 * * @return yyyy */ public static String getYearByCurrentDate() { return getChar8().substring(0, 4); } /****** * 返回当天所在的月份 * * @return mm */ public static String getMonthByCurrentDate() { return getChar8().substring(4, 6); } public static String formatDate(String date, String formatter) { SimpleDateFormat myFormatter = null; Date da = null; if (date.length() < 15) { myFormatter = new SimpleDateFormat("yyyyMMddHHmmss"); } else myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { da = myFormatter.parse(date); } catch (Exception e) { LOGGER.error("formatDate error", e); } return DateFormatUtils.format(da, formatter); } /** * 转化日期时间(yyyyMMddHHmmss)的格式 * * @param date String 日期时间 * @param informatter String 输入的格式 * @param outformatter String 输出的格式 * @return String */ public static String formatDate(String date, String informatter, String outformatter) { Date da = null; try { SimpleDateFormat myFormatter = new SimpleDateFormat(informatter); da = myFormatter.parse(date); } catch (Exception e) { LOGGER.error("formatDate error", e); } return DateFormatUtils.format(da, outformatter); } private static DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); /** * 根据对象转换成日期 * * @param obj */ public static final Date getDate(Object obj) { return getDate(obj, format, null); } public static final Date getDate(Object obj, Date defValue) { return getDate(obj, format, defValue); } public static final Date getDate(Object obj, DateFormat format) { return getDate(obj, format, null); } public static final Date getDate(Object obj, DateFormat format, Date defValue) { if (obj instanceof Date) { return (Date) obj; } else { if (obj instanceof Timestamp) { Timestamp timestamp = (Timestamp) obj; return new Date(timestamp.getTime() + timestamp.getNanos() / 1000000); } else if (obj instanceof Long) { return new Date(((Long) obj).longValue()); } else if (obj instanceof String) { synchronized (format) { try { return format.parse((String) obj); } catch (Exception e) { LOGGER.error(e.getMessage()); return defValue; } } } return defValue; } } public static final String getDateBeforeMonthLastDay() { String str = ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar lastDate = Calendar.getInstance(); lastDate.add(Calendar.MONTH, -1);// 减一个月 lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天 lastDate.roll(Calendar.DATE, -1);// 日期回滚一天,也就是本月最后一天 str = sdf.format(lastDate.getTime()); return str; } /** * * 功能描述:判断格式为yyyy-MM-dd的日期是否为本月今天到上月今天之内的日期 (如:今天为2012-5-18日,2012-4-19到2012-5-18之间的日期返回1)输入参数:<按照参数定义顺序> * * @param param yyyy-MM-dd类型的日期 返回值: 类型 <说明> * @return 1-代表在此一个月内,0-不在此一个月内,-3-时间转换报错 */ public static final int compareWithInOneMonth(String param) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Calendar afterCalendar = Calendar.getInstance(); Calendar beforeCalendar = Calendar.getInstance(); beforeCalendar.add(Calendar.MONTH, -1); Date afterDate = afterCalendar.getTime(); Date beforeDate = beforeCalendar.getTime(); try { Date date = format.parse(param); if (date.before(afterDate) && date.after(beforeDate)) { return 1; } else { return 0; } } catch (Exception e) { LOGGER.error(e.getMessage()); return -3; } } /** * * 功能描述:判定yyyy-MM-dd类型的两个日期的时间段跨度是否在一个月内(如:2012-4-18到2012-05-18是一个月内) 输入参数:<按照参数定义顺序> * * @param arg2 后一个日期 * @param arg1 前一个日期 返回值: 类型 <说明> * @return 0-时间段不在一个月内 ,1-在一个月内,-3-输入日期参数有问题 */ public static final int monthWidthWithInOne(String arg1, String arg2) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); try { Date before = format.parse(arg1); Date after = format.parse(arg2); Calendar calendar = Calendar.getInstance(); calendar.setTime(after); calendar.add(Calendar.MONTH, -1); Date afterMulOneMonth = calendar.getTime(); if (before.before(afterMulOneMonth) || before.after(after)) { return 0; } else { return 1; } } catch (Exception e) { LOGGER.error(e.getMessage()); return -3; } } /** * 转换pos的YYYYMMDD格式的日期到Timestamp格式 (YYYY-MM-DD HH:MM:SS) * * @param strDate * @return Timestamp */ public static Timestamp convertToTimestamp17Byte(String strDate) { Timestamp dateTime = null; String formatDate = ""; formatDate = strDate.substring(0, 4) + "-" + strDate.substring(4, 6) + "-" + strDate.substring(6, 8) + " 00:00:00"; dateTime = Timestamp.valueOf(formatDate); return dateTime; } /** * 转换pos的YYYYMMDDHHMMSS格式的日期到Timestamp格式 (YYYY-MM-DD HH:MM:SS) * * @param strDate * @return Timestamp */ public static Timestamp convertToTimestamp(String strDate) { Timestamp dateTime = null; String formatDate = ""; formatDate = StringUtils.substring(strDate, 0, 4) + "-" + StringUtils.substring(strDate, 4, 6) + "-" + StringUtils.substring(strDate, 6, 8) + " " + StringUtils.substring(strDate, 8, 10) + ":" + StringUtils.substring(strDate, 10, 12) + ":" + StringUtils.substring(strDate, 12, 14); dateTime = Timestamp.valueOf(formatDate); return dateTime; } /** * 转换pos的YYYYMMDD格式的日期到Date格式(YYYY-MM-DD) * * @param strDate * @return Date */ public static java.sql.Date convertToDate10Byte(String strDate) { java.sql.Date dateTime = null; String formatDate = ""; formatDate = strDate.substring(0, 4) + "-" + strDate.substring(4, 6) + "-" + strDate.substring(6, 8); dateTime = java.sql.Date.valueOf(formatDate); return dateTime; } /** * 转换pos的YYYYMMDD格式的日期到格式(YYYY-MM-DD) * * @param strDate * @return Date */ public static String convertDate(String strDate) { String formatDate = ""; if (StringUtils.length(strDate) < 8) { formatDate = getNowDate2(); } else { formatDate = strDate.substring(0, 4) + "-" + strDate.substring(4, 6) + "-" + strDate.substring(6, 8); } return formatDate; } /** * * 功能描述: TimeStamp转化为String * * @param tp * @param sdf * @return */ public static final String timeStamp2String(Timestamp tp, SimpleDateFormat sdf) { Date d = new Date(tp.getTime()); return sdf.format(d); } public static String getChar14(Timestamp time) { return DateFormatUtils.format(time.getTime(), "yyyyMMddHHmmss"); } public static String dateToString(java.util.Date paramDate, String paramString) { SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(paramString); localSimpleDateFormat.setLenient(false); return localSimpleDateFormat.format(paramDate); } /** * * 功能描述:获取系统当前日期(YYYY-MM-DD) * * @param 参数说明 * @return 返回值 * @throw 异常描述 * @see 需要参见的其它内容 */ public static String getNowDate2() { return DateFormatUtils.format(new Date(), FORMAT_10); } /** * * 功能描述: 获取当前日期时间(yyyy-MM-dd hh:mm:ss.SSS) 〈功能详细描述〉 * * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static String getNowDate22() { return DateFormatUtils.format(new Date(), format22); } /** * 获取系统当前日期(yyyyMMdd) 功能描述: * 〈功能详细描述〉 * * @return * @throws ParseException * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static String getNowDate() { return DateFormatUtils.format(new Date(), FORMAT_8); } public static String getNowTime() { return DateFormatUtils.format(new Date(), "HHmmss"); } public static java.util.Date stringToDate(String paramString1, String paramString) { SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(paramString); localSimpleDateFormat.setLenient(false); try { return localSimpleDateFormat.parse(paramString1); } catch (ParseException e) { LOGGER.error("stringToDate error", e); } return null; } /** * 日期相减 * * @param qsDate 起始日期 * @param jzDate 截止日期 * @return 返回截止日期减起始日期的天数 * @throws ParseException * @throws Exception */ public static int diffDate(String qsDate, String jzDate) { try { return (int) ((getMillis(jzDate) - getMillis(qsDate)) / (24 * 3600 * 1000)); } catch (ParseException e) { LOGGER.error("diffDate error", e); } return 0; } public static long getMillis(String date) throws ParseException { java.util.Calendar c = java.util.Calendar.getInstance(); c.setTime(stringToDate(date, FORMAT_8)); return c.getTimeInMillis(); } public static long getMillis(String date, String formate) { java.util.Calendar c = java.util.Calendar.getInstance(); c.setTime(stringToDate(date, formate)); return c.getTimeInMillis(); } /** * * 功能描述: 根据传入时间获取所属时间段的中文描述 日期格式为hhmmss 或 hh:mm:ss 〈功能详细描述〉 默认全天 * * @return 上午、下午、全天 * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static String getChinesTime(String time) { String realTime = StringUtils.replaceChars(time, ":", ""); if (6 == StringUtils.length(realTime)) { long timeNumber = Long.parseLong(realTime); if (timeNumber >= POS_AM_TIME && timeNumber < POS_NOON_TIME) { return AM_CHINESE; } else if (timeNumber >= POS_NOON_TIME && timeNumber <= POS_PM_TIME) { return PM_CHINESE; } else { return ALL_DAY_CHINESE; } } else { return ALL_DAY_CHINESE; } } /** * * 功能描述:获取指定格式时间 * * @param pattern 格式 * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static Timestamp getTimeStamp(String pattern) { DateFormat df = new SimpleDateFormat(pattern); Timestamp ts = Timestamp.valueOf(df.format(new Date())); return ts; } /** * * 功能描述:获取指定格式时间字符串 * * @param pattern 格式 * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static String getDateString(String pattern) { DateFormat df = new SimpleDateFormat(pattern); return df.format(new Date()); } /** * 功能描述: * 计算出endDate-strDate相差的天数-注意:较大的天数作为第二个入参 * 传入参数为空值时,返回结果为-1 * * @param strDate * @param endDate * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static int diffDate(Date strDate, Date endDate) { int result = -1; if (null != strDate && null != endDate) { result = (int) ((endDate.getTime() - strDate.getTime()) / (24 * 3600 * 1000)); } return result; } /** * 获取当前日期是周几 * * @param dt * @return 当前日期是周几 */ public static String getWeekOfDate(String date) { String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" }; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); try { c.setTime(format.parse(date)); } catch (ParseException e) { LOGGER.error("getWeekOfDate error", e); } int dayForWeek = 0; dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1; return weekDays[dayForWeek]; } /** * * 功能描述:把字符串日期后延多少天 * * @param addDays 多少天 * @param dateStr 如20150909 * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static String strDateAddDay(String dateStr, int addDays) { SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_8); ParsePosition pos = new ParsePosition(0); Date dt = sdf.parse(dateStr, pos); Calendar rightNow = Calendar.getInstance(); rightNow.setTime(dt); rightNow.add(Calendar.DAY_OF_YEAR, addDays);// 日期加天 return sdf.format(rightNow.getTime()); } /** * * 功能描述: 字符串日期转换成long * * @param strDate 待转换的日期字符串 * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static long dateStrToLong(String dateStr) { SimpleDateFormat formatter = new SimpleDateFormat(FORMAT_8); ParsePosition pos = new ParsePosition(0); Date strtodate = formatter.parse(dateStr, pos); return strtodate.getTime(); } }
IntegerUtil:
public class NSFIntegerUtils { private NSFIntegerUtils(){} public static final Integer NUM_MINUS_ONE = -1; public static final Integer NUM_ZERO = 0; public static final Integer NUM_ONE = 1; public static final Integer NUM_TWO = 2; public static final Integer NUM_THREE = 3; public static final Integer NUM_FOUR = 4; public static final Integer NUM_FIVE = 5; public static final Integer NUM_SIX = 6; public static final Integer NUM_SEVEN = 7; public static final Integer NUM_EIGHT = 8; public static final Integer NUM_NINE = 9; public static final Integer NUM_TEN = 10; public static final Integer NUM_ELEVEN = 11; public static final Integer NUM_THIRTEEN = 13; public static final Integer NUM_FOURTEEN = 14; public static final Integer NUM_FIFTEEN = 15; public static final Integer NUM_SIXTEEN = 16; /** * * 功能描述: 错误返回0,慎用 * 〈功能详细描述〉 * * @param str * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static int parseFromString(String str){ int temp=0; try { temp=Integer.parseInt(str); } catch (Exception e) { temp=0; } return temp; } }
PaymentUtil.java
/** * 支付Util类 * * @author 14062651 * @see [相关类/方法](可选) * @since [产品/模块版本] (可选) */ public class PaymentUtils { private PaymentUtils() { // empty -init() } /** * 标记:0 成功 */ public static final String SUCCESS_FLAG = "0"; /** * 标记: 1 失败 */ public static final String FAILURE_FLAG = "1"; /** * 支付标记 <<成功前>> 0’成功前检查,'1'失败,'2'撤销检查(是否展示撤销还是退货) */ public static final String PAY_TYPE_SUCCESS = "0"; /** * 支付标记 <<失败>> 0’成功前检查,'1'失败,'2'撤销检查(是否展示撤销还是退货) */ public static final String PAY_TYPE_FAILURE = "1"; /** * 支付标记<<撤销检查>> 0’成功前检查,'1'失败,'2'撤销检查(是否展示撤销还是退货) */ public static final String PAY_TYPE_CANCEL = "2"; /* * 不合法的入参 */ public static final String ERRCODE_ARGS_NOT_VALID = "E000"; /* * 当前状态不可用 */ public static final String ERRCODE_PAY_STATUS_NOT_VALID = "E001"; /* * 付款金额大于剩余应付金额 */ public static final String ERRCODE_AMMOUNT_NOT_VALID = "E002"; protected static Validator validator = null; static { validator = new Validator(); } /** * mis 退款标记 */ public interface MisRefundFlag { /** * 撤销 */ String REVOKE = "1"; /** * 退款 */ String REFUND = "2"; } public static void processPaymentError(PaymentErrorEnum error, NsfBaseResult result) { result.setFlag(FAILURE_FLAG); result.setErrCode(error.getErrorCode()); result.setErrMsg(error.getErrorMsg()); } /** * 根据系统来源 判断 当前系统的数据是否迁移到了Mysql数据库上 * * @param systemSource 系统来源 * @return true:已经是迁移到了Mysql数据库的系统, false:还未迁移到Mysql数据库上 */ public static boolean isMysqlDBEnable(String systemSource) { return StringUtils.equals(SystemSourceEnum.RMPOS.name(), systemSource); } /** * 根据系统来源判断是否RMPOS * * @param systemSource 系统来源 * @return */ public static boolean isRmPos(String systemSource) { // RMPOS特殊的扫码付 return StringUtils.equals(SystemSourceEnum.RMPOS.name(), systemSource); } /** * 根据系统来源判断是否CCT * * @param systemSource 系统来源 * @return */ public static boolean isCct(String systemSource) { // 收银台 return StringUtils.equals(SystemSourceEnum.CCT.name(), systemSource); } /** * 功能描述: * 校验 消息体 * * @param target * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static ListvalidateBean(Object target) { return validator.validate(target); } /** * 将IvOrdDetailMisBean转换成CctMisDetailDto类型的bean * * @param misList * @param results * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static void wrapDetailMisInfo(List misList, List results) { if (CollectionUtils.isNotEmpty(results)) { CctMisDetailDto tmpMisDetailDto; for (IvOrdDetailMisBean misBean : results) { tmpMisDetailDto = new CctMisDetailDto(); // 结算银行-收单银行 tmpMisDetailDto.setAccquirer(misBean.getJsbank()); // 银行日期 tmpMisDetailDto.setAcctDate(misBean.getYhrq()); // 应用标示符-暂不入 tmpMisDetailDto.setaID(StringUtils.EMPTY); // 应用标签 tmpMisDetailDto.setAppLable(StringUtils.EMPTY); // 授权号 tmpMisDetailDto.setAuthorNo(misBean.getSqh()); // 银行交易日期 tmpMisDetailDto.setBankTransDate(misBean.getYhrq()); // 银行交易时间 tmpMisDetailDto.setBankTransTime(misBean.getYhsj()); // 批次号 tmpMisDetailDto.setBatchNo(misBean.getBatchnum()); // 单据状态 tmpMisDetailDto.setBillState(misBean.getDjState()); // 单据类型 tmpMisDetailDto.setBillType(misBean.getDjlx()); // 卡名称 tmpMisDetailDto.setCardName(misBean.getCardname()); // 卡类型 tmpMisDetailDto.setCardType(misBean.getCardtype()); // 收银员号码 tmpMisDetailDto.setCashierNo(misBean.getSyyh()); // 标记位 tmpMisDetailDto.setFlag(misBean.getFlg()); // 备注信息 tmpMisDetailDto.setMemo(misBean.getRspmsg()); // 商户号 tmpMisDetailDto.setMerchantNo(misBean.getShnum()); // 商户名称 tmpMisDetailDto.setMerchantName(misBean.getShname()); // 原批次号 tmpMisDetailDto.setOldBatchNo(misBean.getOldbatchnum()); // 原流水号 tmpMisDetailDto.setOldPosTrace(misBean.getOldpostracenum()); // 原交易参考号 tmpMisDetailDto.setOldTrasRefNo(misBean.getStr10()); // 订单号 tmpMisDetailDto.setOrderNo(misBean.getDh()); // 支付方式 tmpMisDetailDto.setPaymc(misBean.getStr2()); // 流水号 tmpMisDetailDto.setPosTrace(misBean.getPostracenum()); // 门店编码 tmpMisDetailDto.setStoreId(misBean.getStr()); // tc tmpMisDetailDto.settC(StringUtils.EMPTY); // 卡-有效期 tmpMisDetailDto.setValidityDate(misBean.getYxq()); // 交易类型 tmpMisDetailDto.setTransType(misBean.getTranstype()); // 交易参考号 tmpMisDetailDto.setTransRefNo(misBean.getStr4()); // 卡号 tmpMisDetailDto.setTransCardNo(misBean.getTranscard()); //让利金额 BigDecimal couponAmtAndAutoDisctAmt = BigDecimalUtils.add(BigDecimalUtils.dbConvertBd(2, misBean.getNum5()), BigDecimalUtils.dbConvertBd(2, misBean.getNum6())); // 付款金额 tmpMisDetailDto.setTransAmount(BigDecimalUtils.add(misBean.getJyje(),couponAmtAndAutoDisctAmt)); // 终端号 tmpMisDetailDto.setTerminalNo(misBean.getZdnum()); // 已退款金额 // tmpMisDetailDto.setRefundMoney(misBean.getNum2()); //实付金额 tmpMisDetailDto.setRealPayAmt(misBean.getJyje()); //优惠券优惠金额 tmpMisDetailDto.setCouponAmt(BigDecimalUtils.dbConvertBd(2, misBean.getNum5())); //自动折扣立减金额 tmpMisDetailDto.setAutoDisctAmt(BigDecimalUtils.dbConvertBd(2, misBean.getNum6())); misList.add(tmpMisDetailDto); } } } public static MisTransInfoDto wrapMisTrans(DetailMisVo transDBModel) { MisTransInfoDto misTrans = new MisTransInfoDto(); String transCard = transDBModel.getTranscard(); // 支付名称 = 银行卡名称 + 卡号后4位 misTrans.setPaymc(transDBModel.getCardname() + StringUtils.right(transCard, 4)); if (transDBModel.getJyje() != null) { BigDecimal jyje = transDBModel.getJyje(); misTrans.setPaymoney(jyje.setScale(2, BigDecimal.ROUND_UP).toString()); } misTrans.setBatchnum(transDBModel.getBatchnum()); misTrans.setTranscard(transCard); misTrans.setPostracenum(transDBModel.getPostracenum()); misTrans.setZdnum(transDBModel.getZdnum()); misTrans.setDjlx(transDBModel.getDjlx()); misTrans.setOldpostracenum(transDBModel.getOldpostracenum()); // 处理时间 if (null != transDBModel.getYhrq()) { misTrans.setTransDate(transDBModel.getYhrq()); } if (null != transDBModel.getYhsj()) { misTrans.setTransTime(transDBModel.getYhsj()); } misTrans.setShnum(transDBModel.getShnum()); misTrans.setStr6(transDBModel.getStr6()); return misTrans; } }
UUID 生成:
/** * 支付Util类 * * @author 14062651 * @see [相关类/方法](可选) * @since [产品/模块版本] (可选) */ public class PaymentUtils { private PaymentUtils() { // empty -init() } /** * 标记:0 成功 */ public static final String SUCCESS_FLAG = "0"; /** * 标记: 1 失败 */ public static final String FAILURE_FLAG = "1"; /** * 支付标记 <<成功前>> 0’成功前检查,'1'失败,'2'撤销检查(是否展示撤销还是退货) */ public static final String PAY_TYPE_SUCCESS = "0"; /** * 支付标记 <<失败>> 0’成功前检查,'1'失败,'2'撤销检查(是否展示撤销还是退货) */ public static final String PAY_TYPE_FAILURE = "1"; /** * 支付标记<<撤销检查>> 0’成功前检查,'1'失败,'2'撤销检查(是否展示撤销还是退货) */ public static final String PAY_TYPE_CANCEL = "2"; /* * 不合法的入参 */ public static final String ERRCODE_ARGS_NOT_VALID = "E000"; /* * 当前状态不可用 */ public static final String ERRCODE_PAY_STATUS_NOT_VALID = "E001"; /* * 付款金额大于剩余应付金额 */ public static final String ERRCODE_AMMOUNT_NOT_VALID = "E002"; protected static Validator validator = null; static { validator = new Validator(); } /** * mis 退款标记 */ public interface MisRefundFlag { /** * 撤销 */ String REVOKE = "1"; /** * 退款 */ String REFUND = "2"; } public static void processPaymentError(PaymentErrorEnum error, NsfBaseResult result) { result.setFlag(FAILURE_FLAG); result.setErrCode(error.getErrorCode()); result.setErrMsg(error.getErrorMsg()); } /** * 根据系统来源 判断 当前系统的数据是否迁移到了Mysql数据库上 * * @param systemSource 系统来源 * @return true:已经是迁移到了Mysql数据库的系统, false:还未迁移到Mysql数据库上 */ public static boolean isMysqlDBEnable(String systemSource) { return StringUtils.equals(SystemSourceEnum.RMPOS.name(), systemSource); } /** * 根据系统来源判断是否RMPOS * * @param systemSource 系统来源 * @return */ public static boolean isRmPos(String systemSource) { // RMPOS特殊的扫码付 return StringUtils.equals(SystemSourceEnum.RMPOS.name(), systemSource); } /** * 根据系统来源判断是否CCT * * @param systemSource 系统来源 * @return */ public static boolean isCct(String systemSource) { // 收银台 return StringUtils.equals(SystemSourceEnum.CCT.name(), systemSource); } /** * 功能描述: * 校验 消息体 * * @param target * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static ListvalidateBean(Object target) { return validator.validate(target); } /** * 将IvOrdDetailMisBean转换成CctMisDetailDto类型的bean * * @param misList * @param results * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static void wrapDetailMisInfo(List misList, List results) { if (CollectionUtils.isNotEmpty(results)) { CctMisDetailDto tmpMisDetailDto; for (IvOrdDetailMisBean misBean : results) { tmpMisDetailDto = new CctMisDetailDto(); // 结算银行-收单银行 tmpMisDetailDto.setAccquirer(misBean.getJsbank()); // 银行日期 tmpMisDetailDto.setAcctDate(misBean.getYhrq()); // 应用标示符-暂不入 tmpMisDetailDto.setaID(StringUtils.EMPTY); // 应用标签 tmpMisDetailDto.setAppLable(StringUtils.EMPTY); // 授权号 tmpMisDetailDto.setAuthorNo(misBean.getSqh()); // 银行交易日期 tmpMisDetailDto.setBankTransDate(misBean.getYhrq()); // 银行交易时间 tmpMisDetailDto.setBankTransTime(misBean.getYhsj()); // 批次号 tmpMisDetailDto.setBatchNo(misBean.getBatchnum()); // 单据状态 tmpMisDetailDto.setBillState(misBean.getDjState()); // 单据类型 tmpMisDetailDto.setBillType(misBean.getDjlx()); // 卡名称 tmpMisDetailDto.setCardName(misBean.getCardname()); // 卡类型 tmpMisDetailDto.setCardType(misBean.getCardtype()); // 收银员号码 tmpMisDetailDto.setCashierNo(misBean.getSyyh()); // 标记位 tmpMisDetailDto.setFlag(misBean.getFlg()); // 备注信息 tmpMisDetailDto.setMemo(misBean.getRspmsg()); // 商户号 tmpMisDetailDto.setMerchantNo(misBean.getShnum()); // 商户名称 tmpMisDetailDto.setMerchantName(misBean.getShname()); // 原批次号 tmpMisDetailDto.setOldBatchNo(misBean.getOldbatchnum()); // 原流水号 tmpMisDetailDto.setOldPosTrace(misBean.getOldpostracenum()); // 原交易参考号 tmpMisDetailDto.setOldTrasRefNo(misBean.getStr10()); // 订单号 tmpMisDetailDto.setOrderNo(misBean.getDh()); // 支付方式 tmpMisDetailDto.setPaymc(misBean.getStr2()); // 流水号 tmpMisDetailDto.setPosTrace(misBean.getPostracenum()); // 门店编码 tmpMisDetailDto.setStoreId(misBean.getStr()); // tc tmpMisDetailDto.settC(StringUtils.EMPTY); // 卡-有效期 tmpMisDetailDto.setValidityDate(misBean.getYxq()); // 交易类型 tmpMisDetailDto.setTransType(misBean.getTranstype()); // 交易参考号 tmpMisDetailDto.setTransRefNo(misBean.getStr4()); // 卡号 tmpMisDetailDto.setTransCardNo(misBean.getTranscard()); //让利金额 BigDecimal couponAmtAndAutoDisctAmt = BigDecimalUtils.add(BigDecimalUtils.dbConvertBd(2, misBean.getNum5()), BigDecimalUtils.dbConvertBd(2, misBean.getNum6())); // 付款金额 tmpMisDetailDto.setTransAmount(BigDecimalUtils.add(misBean.getJyje(),couponAmtAndAutoDisctAmt)); // 终端号 tmpMisDetailDto.setTerminalNo(misBean.getZdnum()); // 已退款金额 // tmpMisDetailDto.setRefundMoney(misBean.getNum2()); //实付金额 tmpMisDetailDto.setRealPayAmt(misBean.getJyje()); //优惠券优惠金额 tmpMisDetailDto.setCouponAmt(BigDecimalUtils.dbConvertBd(2, misBean.getNum5())); //自动折扣立减金额 tmpMisDetailDto.setAutoDisctAmt(BigDecimalUtils.dbConvertBd(2, misBean.getNum6())); misList.add(tmpMisDetailDto); } } } public static MisTransInfoDto wrapMisTrans(DetailMisVo transDBModel) { MisTransInfoDto misTrans = new MisTransInfoDto(); String transCard = transDBModel.getTranscard(); // 支付名称 = 银行卡名称 + 卡号后4位 misTrans.setPaymc(transDBModel.getCardname() + StringUtils.right(transCard, 4)); if (transDBModel.getJyje() != null) { BigDecimal jyje = transDBModel.getJyje(); misTrans.setPaymoney(jyje.setScale(2, BigDecimal.ROUND_UP).toString()); } misTrans.setBatchnum(transDBModel.getBatchnum()); misTrans.setTranscard(transCard); misTrans.setPostracenum(transDBModel.getPostracenum()); misTrans.setZdnum(transDBModel.getZdnum()); misTrans.setDjlx(transDBModel.getDjlx()); misTrans.setOldpostracenum(transDBModel.getOldpostracenum()); // 处理时间 if (null != transDBModel.getYhrq()) { misTrans.setTransDate(transDBModel.getYhrq()); } if (null != transDBModel.getYhsj()) { misTrans.setTransTime(transDBModel.getYhsj()); } misTrans.setShnum(transDBModel.getShnum()); misTrans.setStr6(transDBModel.getStr6()); return misTrans; } }
HTTPClient.java
@SuppressWarnings("deprecation") public class HttpClientUtil { private static final Logger LOGGER = Logger.getLogger(HttpClientUtil.class); protected HttpClientUtil() { } public static String post(String url, Mapparams, boolean isIgnoreCredential) { DefaultHttpClient httpclient; if (isIgnoreCredential) { httpclient = (DefaultHttpClient) HttpsClient.newHttpsClient(); } else { httpclient = new DefaultHttpClient(); } httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 900000000); httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 900000000); httpclient.getParams().setParameter(CoreConnectionPNames.MAX_LINE_LENGTH, 0); httpclient.getParams().setParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 81920); httpclient.getParams().setParameter(CoreConnectionPNames.MAX_HEADER_COUNT, 0); LOGGER.info("create httppost:" + url); HttpPost post = postForm(url, params); String body = invoke(httpclient, post); httpclient.getConnectionManager().shutdown(); return body; } public static String get(String url) { DefaultHttpClient httpclient = new DefaultHttpClient(); LOGGER.info("create httpget:" + url); HttpGet get = new HttpGet(url); String body = invoke(httpclient, get); httpclient.getConnectionManager().shutdown(); return body; } private static String invoke(DefaultHttpClient httpclient, HttpUriRequest httpost) { HttpResponse response = sendRequest(httpclient, httpost); String body = paseResponse(response); return body; } private static String paseResponse(HttpResponse response) { LOGGER.info("Get response from http server..."); HttpEntity entity = response.getEntity(); LOGGER.info("response status: " + response.getStatusLine()); String charset = EntityUtils.getContentCharSet(entity); LOGGER.info(charset); String body = null; try { body = EntityUtils.toString(entity); LOGGER.info(body); } catch (ParseException | IOException e) { LOGGER.error(e.getMessage(), e); } return body; } private static HttpResponse sendRequest(DefaultHttpClient httpclient, HttpUriRequest httpost) { LOGGER.info("execute post..."); HttpResponse response = null; try { response = httpclient.execute(httpost); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } return response; } private static HttpPost postForm(String url, Map params) { HttpPost httpost = new HttpPost(url); List nvps = new ArrayList (); Iterator > it = params.entrySet().iterator(); while (it.hasNext()) { Entry entry = it.next(); nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } try { LOGGER.info("set utf-8 form entity to httppost"); httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); } catch (UnsupportedEncodingException e) { LOGGER.error(e.getMessage(), e); } return httpost; } }
public class CommonUtils { /** * 1:零售商商品编码 */ public static final int MER_CMMCODE_TYPE = 1; /** * 2:标品库SKU编码 */ public static final int STAND_SKU_CMMCODE_TYPE = 2; /** * 3:标品库SPU编码 */ public static final int STAND_SPU_CMMCODE_TYPE = 3; /** * 主站 */ public static final int MAIN_SITE = 1; /** * 自有系统 */ public static final int SELF_SITE = 2; private CommonUtils() { } /** * 功能描述: 生成商品编码 * 〈功能详细描述〉 生成商品编码 * * @param codeType 生成编码类型(1:零售商商品编码 2:标品库SKU编码 3:标品库SPU编码) * @return 商品编码 * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static String getCmmdtyCode(int codeType) { String cmmdtyCode = ""; Long currentTime = Calendar.getInstance().getTime().getTime(); switch (codeType) { case MER_CMMCODE_TYPE: cmmdtyCode = "MER" + getRandom(0, 99999) + currentTime; break; case STAND_SKU_CMMCODE_TYPE: cmmdtyCode = "SKU" + getRandom(0, 99999) + currentTime; break; case STAND_SPU_CMMCODE_TYPE: cmmdtyCode = "SPU" + getRandom(0, 99999) + currentTime; break; default: break; } return cmmdtyCode; } /** * 功能描述: 获取图片路径 * 〈功能详细描述〉 获取图片路径 * * @param source 图片来源 * @return 图片路径 * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static String getImageFolder(int source) { String folder = StringUtils.EMPTY; switch (source) { case MAIN_SITE: folder = File.separator + "b2c" + File.separator + "newcatentries" + File.separator; break; case SELF_SITE: folder = File.separator + "sdipos" + File.separator + "catentries" + File.separator; break; default: break; } return folder; } /** * 功能描述: 获取图片URL路径 * 〈功能详细描述〉 获取图片URL路径 * * @param source 图片来源 * @return 图片路径 * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static String getImageUrlFolder(int source) { String folder = StringUtils.EMPTY; switch (source) { case MAIN_SITE: folder = "/b2c/newcatentries/"; break; case SELF_SITE: folder = "/sdipos/catentries/"; break; default: break; } return folder; } /** * 功能描述: 生成给定区间的随机数 * 〈功能详细描述〉 生成给定区间的随机数 * * @param min 生成随机数最小 * @param max 生成随机数最大 * @return 随机数 * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static String getRandom(int min, int max) { int size = Integer.toString(max).length(); int random = new Random().nextInt(max) % (max - min + 1) + min; int zorecount = size - Integer.toString(random).length(); StringBuilder sb = new StringBuilder(StringUtils.EMPTY); for (int i = 0; i < zorecount; i++) { sb.append("0"); } return sb.toString() + random; } /** * * 随机获取图片服务器 * * @return */ public static String getRandomUimgServer() { String uimgUrl = StaticVars.getInstance().getPropsValDefaultString("uimgUrl", "https://image.suning.cn"); String[] urlArr = uimgUrl.split(","); int len = urlArr.length; if (len > 1) { int number = new Random().nextInt(len); return urlArr[number] + "/uimg"; } else { return uimgUrl + "/uimg"; } } }
MapUtils
public class MapUtils { /** * JavaBean 转 Map */ public static MapbeanToMap(Object object) throws IllegalAccessException { Class cls = object.getClass(); Field[] fields = cls.getDeclaredFields(); Map map = new HashMap (); for (Field field : fields) { field.setAccessible(true); map.put(field.getName(), field.get(object)); } return map; } /** * Map 转 JavaBean */ public static Object mapToBean(Map map, Class cls) throws InstantiationException, IllegalAccessException, NoSuchFieldException { Object object = cls.newInstance(); Iterator > it = map.entrySet().iterator(); while (it.hasNext()) { Entry entry = it.next(); Field temFiels = cls.getDeclaredField(entry.getKey()); temFiels.setAccessible(true); temFiels.set(object, entry.getValue()); } return object; } }
解析文件攻击类
public class ParseFileUtil { /** *日志 */ private static final Logger LOGGER = LoggerFactory.getLogger(ParseFileUtil.class); /** * 〈功能描述〉 解析易付宝账务汇总查询,返回list结果集 * 〈功能详细描述〉 * * @param array * @return * @see [相关类/方法](可选) * @since [产品/模块版本](可选) */ public static ListparseEppStatementSummaryFile(String result){ LOGGER.info("Enter parseEppStatementSummaryFile", result); //返回结果列表 List dtos = new ArrayList (); String[] array = result.split("\n"); //循环读取数组数据 for(int i=5;i parseEppStatementDetaiFile(String result){ LOGGER.info("Enter parseEppStatementDetaiFile", result); //返回结果列表 List dtos = new ArrayList (); String[] array = result.split("\n"); //循环读取数组数据 for(int i=5;i RSA.java
public class RSA { private static final Logger LOGGER = LoggerFactory.getLogger(RSA.class); public static void main(String[] args) throws IOException { try { KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA"); gen.initialize(2048); KeyPair pair = gen.generateKeyPair(); // rsa生成一对公私钥 PublicKey publicKey = pair.getPublic(); PrivateKey privateKey = pair.getPrivate(); // SHA1withRSA算法进行签名 Signature sign = Signature.getInstance("SHA1withRSA"); sign.initSign(privateKey); byte[] data = "sss".getBytes(); // 更新用于签名的数据 sign.update(data); byte[] signature = sign.sign(); Signature verifySign = Signature.getInstance("SHA1withRSA"); verifySign.initVerify(publicKey); // 用于验签的数据 verifySign.update(data); boolean flag = verifySign.verify(signature); System.out.println(flag); } catch (Exception e) { LOGGER.error(e.getMessage(), e); } } }RSAUtils.java
public class RSAUtils { private static final Logger LOGGER = LoggerFactory.getLogger(RSAUtils.class); public static final String MAP_KEY_PUBLIC_KEY = "publicKey"; public static final String MAP_KEY_PRIVATE_KEY = "privateKey"; public static MapgenerateRSAKeyPlain() { Map map = new HashMap (); try { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); KeyPair keyPair = keyPairGenerator.generateKeyPair(); map.put(MAP_KEY_PUBLIC_KEY, getPublicKeyPlain(keyPair.getPublic())); map.put(MAP_KEY_PRIVATE_KEY, getPrivateKeyPlain(keyPair.getPrivate())); } catch (NoSuchAlgorithmException e) { LOGGER.error("无此算法", e); } return map; } public static byte[] encrypt(PublicKey publicKey, byte[] data) throws InvalidKeyException, IllegalBlockSizeException, NoSuchAlgorithmException, NoSuchPaddingException, ShortBufferException, BadPaddingException { return encrypt(publicKey, "RSA/ECB/PKCS1Padding", data); } public static byte[] encrypt(PublicKey publicKey, String transformation, byte[] data) throws InvalidKeyException, IllegalBlockSizeException, NoSuchAlgorithmException, NoSuchPaddingException, ShortBufferException, BadPaddingException { Cipher cipher; try { cipher = Cipher.getInstance(transformation); cipher.init(1, publicKey); int blockSize = cipher.getBlockSize(); int outputSize = cipher.getOutputSize(data.length); int leavedSize = data.length % blockSize; int blocksSize = leavedSize != 0 ? data.length / blockSize + 1 : data.length / blockSize; int i = 0; byte[] raw = new byte[outputSize * blocksSize]; while (data.length - i * blockSize > 0) { if (data.length - i * blockSize > blockSize) { cipher.doFinal(data, i * blockSize, blockSize, raw, i * outputSize); } else { cipher.doFinal(data, i * blockSize, data.length - i * blockSize, raw, i * outputSize); } i++; } return raw; } catch (IllegalBlockSizeException e) { LOGGER.error(e.getMessage(), e); throw new IllegalBlockSizeException(e.getMessage()); } catch (NoSuchAlgorithmException e) { LOGGER.error(e.getMessage(), e); throw new NoSuchAlgorithmException(e.getMessage()); } catch (NoSuchPaddingException e) { LOGGER.error(e.getMessage(), e); throw new NoSuchPaddingException(e.getMessage()); } catch (ShortBufferException e) { LOGGER.error(e.getMessage(), e); throw new ShortBufferException(e.getMessage()); } catch (BadPaddingException e) { LOGGER.error(e.getMessage(), e); throw new BadPaddingException(e.getMessage()); } catch (InvalidKeyException e) { LOGGER.error(e.getMessage(), e); throw new InvalidKeyException(e.getMessage()); } } public static boolean verify(PublicKey publicKey, String algorithm, byte[] data, byte[] signData) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException { Signature signature = Signature.getInstance(algorithm); signature.initVerify(publicKey); signature.update(data); return signature.verify(signData); } public static byte[] sign(PrivateKey privateKey, byte[] data, String algorithm) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException { Signature signature = Signature.getInstance(algorithm); signature.initSign(privateKey); signature.update(data); return signature.sign(); } public static String getPublicKeyPlain(PublicKey publicKey) { byte[] pbk = publicKey.getEncoded(); return Base64.encodeToString(pbk, 0); } public static String getPrivateKeyPlain(PrivateKey privateKey) { byte[] prk = privateKey.getEncoded(); return Base64.encodeToString(prk, 0); } public static PublicKey loadPublicKey(String publicKeyStr) { PublicKey publicKey = null; try { byte[] buffer = Base64.decode(publicKeyStr, 0); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); publicKey = keyFactory.generatePublic(keySpec); } catch (NoSuchAlgorithmException e) { LOGGER.error("无此算法", e); } catch (InvalidKeySpecException e) { LOGGER.error("非法公钥", e); } return publicKey; } public static PrivateKey loadPrivateKey(String privateKeyStr) { PrivateKey privateKey = null; try { byte[] buffer = Base64.decode(privateKeyStr, 0); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); privateKey = keyFactory.generatePrivate(keySpec); } catch (NoSuchAlgorithmException e) { LOGGER.error("无此算法", e); } catch (InvalidKeySpecException e) { LOGGER.error("非法私钥", e); } return privateKey; } } SuningPayUtils
public class SuningPaySignUtil { private static final Logger LOGGER = LoggerFactory.getLogger(SuningPaySignUtil.class); private static Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create(); private SuningPaySignUtil() { } public static MapinitParams(Object obj, Date date, String version, String notifyUrl) { Map params = Maps.newHashMap(); try { Field[] fields = obj.getClass().getSuperclass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { if (!"serialVersionUID".equals(fields[i].getName())) { fields[i].setAccessible(true); if (null != fields[i].get(obj)) { if (fields[i].get(obj) instanceof List) { params.put(fields[i].getName(), gson.toJson(fields[i].get(obj), List.class)); } else if (fields[i].getType().getName().startsWith("com.suning")) { params.put(fields[i].getName(), gson.toJson(fields[i].get(obj), fields[i].getType())); } else { params.put(fields[i].getName(), fields[i].get(obj).toString()); } } } } fields = obj.getClass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { if (!"serialVersionUID".equals(fields[i].getName())) { fields[i].setAccessible(true); if (null != fields[i].get(obj)) { if (fields[i].get(obj) instanceof List) { params.put(fields[i].getName(), gson.toJson(fields[i].get(obj), List.class)); } else if (fields[i].getType().getName().startsWith("com.suning")) { params.put(fields[i].getName(), gson.toJson(fields[i].get(obj), fields[i].getType())); } else { if ("storeCode".equals(fields[i].getName())) { continue; } params.put(fields[i].getName(), fields[i].get(obj).toString()); } } } } params.put("publicKeyIndex", "0001"); // 公钥索引 params.put("inputCharset", "UTF-8"); // 编码类型 params.put("merchantNo", "70056371"); // 系统接入方 params.put("submitTime", DateUtil.format(date, "yyyyMMddHHmmss")); // 提交时间 if (StringUtils.isNotBlank(version)) { params.put("version", version); } if (StringUtils.isNotBlank(notifyUrl)) { params.put("notifyUrl", notifyUrl); } initSigns(params); } catch (IllegalArgumentException e) { LOGGER.error("IllegalArgumentException", e); } catch (IllegalAccessException e) { LOGGER.error("IllegalAccessException", e); } catch (SecurityException e) { LOGGER.error("SecurityException", e); } return params; } private static void initSigns(Map map) { List > list = new ArrayList(); list.addAll(map.entrySet()); KeyComparator kc = new KeyComparator(); Collections.sort(list, kc); StringBuilder sb = new StringBuilder(); for (Iterator > it = list.iterator(); it.hasNext();) { Map.Entry mapEntity = it.next(); sb.append(mapEntity.getKey()).append("=").append(mapEntity.getValue()).append("&"); } String str = sb.toString(); String waitSignStr = str.endsWith("&") ? str.substring(0, str.length() - 1) : str; String sign = null; String signAlgorithm = null; try { sign = SignatureUtil.sign((MD5Util.Md5(waitSignStr)).toUpperCase(), ScmConfUtil.getInstance().getString("PAY_PRIVATEKEY", "")); signAlgorithm = "RSA"; } catch (Exception e) { LOGGER.error("Init signs error.", e); } map.put("signature", sign); // 签名 map.put("signAlgorithm", signAlgorithm); // 签名算法 } static class KeyComparator implements Comparator > { @Override public int compare(Map.Entry m, Map.Entry n) { return m.getKey().compareTo(n.getKey()); } } } 银联支付:
public class UnionPaySignUtil { private static final Logger LOGGER = LoggerFactory.getLogger(UnionPaySignUtil.class); private UnionPaySignUtil() { } public static String signData(String privateKeyStr, String content) throws UnsupportedEncodingException { PrivateKey privateKey = RSAUtils.loadPrivateKey(privateKeyStr); byte[] data = new byte[0]; try { data = content.getBytes("utf-8"); } catch (UnsupportedEncodingException e) { LOGGER.error(e.getMessage(), e); } String signature = null; try { byte[] signData = RSAUtils.sign(privateKey, data, "SHA1withRSA"); signature = Base64.encodeToString(signData, 6); } catch (Exception e) { LOGGER.error(e.getMessage(), e); } return signature; } public static boolean verifySignData(String publicKeyStr, String content, String signData) { PublicKey publicKey = RSAUtils.loadPublicKey(publicKeyStr); byte[] data = new byte[0]; try { data = content.getBytes("utf-8"); } catch (UnsupportedEncodingException e) { LOGGER.error(e.getMessage(), e); } boolean valid = false; try { byte[] sign = Base64.decode(signData, 4); valid = RSAUtils.verify(publicKey, "SHA1withRSA", data, sign); } catch (Exception e) { LOGGER.error(e.getMessage(), e); } return valid; } }