Java工具集

阅读: 评论:0

Java工具集

Java工具集

简单工具类

写作初衷:由于日常开发经常需要用到很多工具类,经常根据需求自己写也比较麻烦
网上好了一些工具类例如commom.lang3或者hutool或者Jodd这样的开源工具,但是
发现他们之中虽然设计不错,但是如果我想要使用,就必须要引入依赖并且去维护依赖,有些
甚至会有存在版本编译不通过问题,故此想要写作一个每个类都可以作为独立工具类使用
每个使用者只需要复制该类,到任何项目当中都可以使用,所以需要尊从以下两个原则才能
做到.在此诚邀各位大佬参与.可以把各自用过的工具,整合成只依赖JDK,每个类都能够单独
使用的工具.每个人当遇到业务需求需要使用的时候,只需要到这里单独拷贝一个即可使用.
抛弃传统的需要引入依赖的烦恼.让大家一起来解决你所面临的业务问题吧!

介绍

遵从两大原则

  • 1.绝不依赖JDK以外的源码
  • 2.牺牲代码复用性,每个类都必须是单独的组件,绝不互相引用,做到完全解耦
package *;/*** @program: simple_tools* @description: 中文数字与阿拉伯数字转换工具* @author: ChenWenLong* @create: 2019-10-28 10:07**/
public class ChineseNumberUtil {public static final String ZERO = "零";public static final String ONE = "一";public static final String TWO = "二";public static final String THREE = "三";public static final String FOUR = "四";public static final String FIVE = "五";public static final String SIX = "六";public static final String SEVEN = "七";public static final String EIGHT = "八";public static final String NINE = "九";public static final String POINT = "点";// OLD CHINESE NUMBERpublic static final String OLD_ONE = "壹";public static final String OLD_TWO = "贰";public static final String OLD_THREE = "叁";public static final String OLD_FOUR = "肆";public static final String OLD_FIVE = "伍";public static final String OLD_SIX = "陆";public static final String OLD_SEVEN = "柒";public static final String OLD_EIGHT = "捌";public static final String OLD_NINE = "玖";public static final String HUNDRED = "佰";public static final String THOUSAND = "千";public static final String TEN_THOUSAND = "萬";public static final String HUNDRED_THOUSAND = "十萬";public static final String MILLION = "佰万";public static final String TEN_MILLION = "千万";public static final int NEW_TPYE = 0;public static final int OLD_TPYE = 1;/*** 功能描述:* 〈将阿拉伯数字转换成中文数字〉** @params : [number]* @return : java.lang.String* @author : cwl* @date : 2019/10/28 10:16*/public static String getChineseNumber(double number){String numStr = String.valueOf(number);return handleBytes(numStr,NEW_TPYE);}/*** 功能描述:* 〈获得中文数字,指定类型〉** @params : [number, type]* @return : java.lang.String* @author : cwl* @date : 2019/10/28 16:12*/public static String getChineseNumber(double number,int type){String numStr = String.valueOf(number);return handleBytes(numStr,type);}/*** 功能描述:* 〈处理具体的Byte[],拼接成想要的格式〉** @params : [bytes, sb, especially]* @return : void* @author : cwl* @date : 2019/10/28 10:56*/private static String handleBytes(String str, int type) {StringBuilder sb = new StringBuilder();byte[] bytes = Bytes();if(NEW_TPYE == type){return handelNewType(bytes,sb);}return handleOldType(bytes,sb).toString();}/*** 功能描述:* 〈处理发票式数字〉** @params : [bytes, sb]* @return : java.lang.String* @author : cwl* @date : 2019/10/28 11:04*/private static StringBuilder handleOldType(byte[] bytes, StringBuilder sb) {for (int i=0;i<bytes.length;i++) {switch (bytes[i]){case 46:sb.append(POINT);sb = handleUnit(sb,i,bytes.length);break;case 48:sb.append(ZERO);sb = handleUnit(sb,i,bytes.length);break;case 49:sb.append(OLD_ONE);sb = handleUnit(sb,i,bytes.length);break;case 50:sb.append(OLD_TWO);sb = handleUnit(sb,i,bytes.length);break;case 51:sb.append(OLD_THREE);sb = handleUnit(sb,i,bytes.length);break;case 52:sb.append(OLD_FOUR);sb = handleUnit(sb,i,bytes.length);break;case 53:sb.append(OLD_FIVE);sb = handleUnit(sb,i,bytes.length);break;case 54:sb.append(OLD_SIX);sb = handleUnit(sb,i,bytes.length);break;case 55:sb.append(OLD_SEVEN);sb = handleUnit(sb,i,bytes.length);break;case 56:sb.append(OLD_EIGHT);sb = handleUnit(sb,i,bytes.length);break;case 57:sb.append(OLD_NINE);sb = handleUnit(sb,i,bytes.length);break;default:break;}}return sb;}/*** 功能描述:* 〈处理数字单位〉** @params : [sb, i]* @return : void* @author : cwl* @date : 2019/10/28 11:57*/private static StringBuilder handleUnit(StringBuilder sb, int i, int length) {if(length <= 1){return sb;}switch(length){case 3 :sb = handleHundred(i,sb);break;case 4 :sb = handleThousand(i,sb);break;case 5 :sb = handleTenThousand(i,sb);break;case 6 :sb = handleHundredThousand(i,sb);break;case 7 :sb = handleMillion(i,sb);break;case 8 :sb = handleTenMillion(i,sb);}return sb;}/*** 功能描述:* 〈拼接千万〉** @params : [i, sb]* @return : java.lang.StringBuilder* @author : cwl* @date : 2019/10/28 15:42*/private static StringBuilder handleTenMillion(int i, StringBuilder sb) {if(i == 0){sb.append(TEN_MILLION);}if(i == 1){sb.append(MILLION);}if(i == 2){sb.append(HUNDRED_THOUSAND);}if(i == 3){sb.append(TEN_THOUSAND);}if(i == 4){sb.append(THOUSAND);}if(i == 5){sb.append(HUNDRED);}return sb;}/*** 功能描述:* 〈拼接百万〉** @params : [i, sb]* @return : java.lang.StringBuilder* @author : cwl* @date : 2019/10/28 15:42*/private static StringBuilder handleMillion(int i, StringBuilder sb) {if(i == 0){sb.append(MILLION);}if(i == 1){sb.append(HUNDRED_THOUSAND);}if(i == 2){sb.append(TEN_THOUSAND);}if(i == 3){sb.append(THOUSAND);}if(i == 4){sb.append(HUNDRED);}return sb;}/*** 功能描述:* 〈拼接十万〉** @params : [i, sb]* @return : java.lang.StringBuilder* @author : cwl* @date : 2019/10/28 15:39*/private static StringBuilder handleHundredThousand(int i, StringBuilder sb) {if(i == 0){sb.append(HUNDRED_THOUSAND);}if(i == 1){sb.append(TEN_THOUSAND);}if(i == 2){sb.append(THOUSAND);}if(i == 3){sb.append(HUNDRED);}return null;}/*** 功能描述:* 〈拼接万〉** @params : [i, sb]* @return : java.lang.StringBuilder* @author : cwl* @date : 2019/10/28 15:31*/private static StringBuilder handleTenThousand(int i, StringBuilder sb) {if(i == 0){sb.append(TEN_THOUSAND);}if(i == 1){sb.append(THOUSAND);}if(i == 2){sb.append(HUNDRED);}return sb;}/*** 功能描述:* 〈拼接千位数〉** @params : [i, sb]* @return : java.lang.StringBuilder* @author : cwl* @date : 2019/10/28 15:30*/private static StringBuilder handleThousand(int i, StringBuilder sb) {if(i == 0){sb.append(THOUSAND);}if(i == 1){sb.append(HUNDRED);}return sb;}/*** 功能描述:* 〈拼接百位数〉** @params : [i, sb]* @return : void* @author : cwl* @date : 2019/10/28 15:28*/private static StringBuilder handleHundred(int i, StringBuilder sb) {if(i == 0){sb.append(HUNDRED);}return sb;}/*** 功能描述:* 〈处理简单中文数字〉** @params : [bytes, sb]* @return : void* @author : cwl* @date : 2019/10/28 11:02*/private static String handelNewType(byte[] bytes, StringBuilder sb) {for (byte aByte : bytes) {switch (aByte){case 46:sb.append(POINT);break;case 48:sb.append(ZERO);break;case 49:sb.append(ONE);break;case 50:sb.append(TWO);break;case 51:sb.append(THREE);break;case 52:sb.append(FOUR);break;case 53:sb.append(FIVE);break;case 54:sb.append(SIX);break;case 55:sb.append(SEVEN);break;case 56:sb.append(EIGHT);break;case 57:sb.append(NINE);break;default:break;}}String();}/*** 功能描述:* 〈〉** @params : [number]* @return : double* @author : cwl* @date : 2019/10/28 10:38*/public static double getNumber(String number){if(null == number || "".equals(number)){throw new RuntimeException("number is not be null or blank");}char[] chars = CharArray();char[] everyChar;StringBuilder sb = new StringBuilder();for (char aChar : chars) {everyChar = new char[1];everyChar[0] = aChar;switch (new String(everyChar)){case POINT :sb.append(".");break;case ZERO :sb.append("0");break;case ONE :sb.append("1");break;case TWO :sb.append("2");break;case THREE :sb.append("3");break;case FOUR :sb.append("4");break;case FIVE :sb.append("5");break;case SIX :sb.append("6");break;case SEVEN :sb.append("7");break;case EIGHT :sb.append("8");break;case NINE :sb.append("9");break;default:break;}}return Double.String());}/*** 功能描述:* 〈获得发票上的中文数字〉** @params : [number]* @return : java.lang.String* @author : cwl* @date : 2019/10/28 10:50*/public static String getInvoiceNumber(double number){StringBuilder sb = new StringBuilder();String valueOf = String.valueOf(number);byte[] bytes = Bytes();StringBuilder builder = new StringBuilder();StringBuilder builderFloat = new StringBuilder();for (int i=0;i<bytes.length;i++) {if(bytes[i] == 46){for (int j=i;j<bytes.length;j++) {builderFloat.append(new String(new byte[]{bytes[j]}));}break;}builder.append(new String(new byte[]{bytes[i]}));}StringBuilder result = String().getBytes(), sb);StringBuilder chineseNumber = String());result.append(chineseNumber);String();}/*** 功能描述:* 〈处理老式发票抬头数字 忽略单位,仅为小数点后的数字〉** @params : [toString]* @return : java.lang.StringBuilder* @author : cwl* @date : 2019/10/28 16:22*/private static StringBuilder handleOldTypeIgnoreUnit(String toString) {byte[] bytes = Bytes();StringBuilder sb = new StringBuilder();for (int i=0;i<bytes.length;i++) {switch (bytes[i]){case 46:sb.append(POINT);break;case 48:sb.append(ZERO);break;case 49:sb.append(OLD_ONE);break;case 50:sb.append(OLD_TWO);break;case 51:sb.append(OLD_THREE);break;case 52:sb.append(OLD_FOUR);break;case 53:sb.append(OLD_FIVE);break;case 54:sb.append(OLD_SIX);break;case 55:sb.append(OLD_SEVEN);break;case 56:sb.append(OLD_EIGHT);break;case 57:sb.append(OLD_NINE);break;default:break;}}return sb;}}

本文发布于:2024-02-05 04:01:05,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170723596862886.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:工具   Java
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23