文章目录

  • 1. [Java开发常用Util工具类-StringUtil、CastUtil、CollectionUtil、ArrayUtil、PropsUtil](https://www.cnblogs.com/aeolian/p/9484247.html)
  • 2.[Java常用工具类集合](https://blog.csdn.net/justdb/article/details/8653166)
  • 3.[Java Utils工具类大全](https://blog.csdn.net/rj597306518/article/details/71480467)
  • 4.[分布式ID生成 - 雪花算法](https://blog.csdn.net/u012488504/article/details/82194495?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param)
  • 5.小写金额转成大写金额
  • 6.生成二维码

1. Java开发常用Util工具类-StringUtil、CastUtil、CollectionUtil、ArrayUtil、PropsUtil

2.Java常用工具类集合

3.Java Utils工具类大全

4.分布式ID生成 - 雪花算法

5.小写金额转成大写金额

import java.text.DecimalFormat;
import java.util.Scanner;/*** 金额转换*/
public class Example {// 大写数字private final static String[] STR_NUMBER = { "零", "壹", "贰", "叁", "肆", "伍","陆", "柒", "捌", "玖" };private final static String[] STR_UNIT = { "", "拾", "佰", "仟", "万", "拾","佰", "仟", "亿", "拾", "佰", "仟" };// 整数单位private final static String[] STR_UNIT2 = { "角", "分", "厘" };// 小数单位public static void main(String[] args) {Scanner scan = new Scanner(System.in);// 创建扫描器System.out.println("请输入一个金额");// 获取金额转换后的字符串String convert = convert(scan.nextDouble());System.out.println(convert);// 输出转换结果}/*** 获取整数部分*/public static String getInteger(String num) {if (num.indexOf(".") != -1) { // 判断是否包含小数点num = num.substring(0, num.indexOf("."));}num = new StringBuffer(num).reverse().toString(); // 反转字符串StringBuffer temp = new StringBuffer(); // 创建一个StringBuffer对象for (int i = 0; i < num.length(); i++) {// 加入单位temp.append(STR_UNIT[i]);temp.append(STR_NUMBER[num.charAt(i) - 48]);}num = temp.reverse().toString();// 反转字符串num = numReplace(num, "零拾", "零"); // 替换字符串的字符num = numReplace(num, "零佰", "零"); // 替换字符串的字符num = numReplace(num, "零仟", "零"); // 替换字符串的字符num = numReplace(num, "零万", "万"); // 替换字符串的字符num = numReplace(num, "零亿", "亿"); // 替换字符串的字符num = numReplace(num, "零零", "零"); // 替换字符串的字符num = numReplace(num, "亿万", "亿"); // 替换字符串的字符// 如果字符串以零结尾将其除去if (num.lastIndexOf("零") == num.length() - 1) {num = num.substring(0, num.length() - 1);}return num;}/*** 获取小数部分*/public static String getDecimal(String num) {// 判断是否包含小数点if (num.indexOf(".") == -1) {return "";}num = num.substring(num.indexOf(".") + 1);// 反转字符串num = new StringBuffer(num).reverse().toString();// 创建一个StringBuffer对象StringBuffer temp = new StringBuffer();// 加入单位for (int i = 0; i < num.length(); i++) {temp.append(STR_UNIT2[i]);temp.append(STR_NUMBER[num.charAt(i) - 48]);}num = temp.reverse().toString(); // 替换字符串的字符num = numReplace(num, "零角", "零"); // 替换字符串的字符num = numReplace(num, "零分", "零"); // 替换字符串的字符num = numReplace(num, "零厘", "零"); // 替换字符串的字符num = numReplace(num, "零零", "零"); // 替换字符串的字符// 如果字符串以零结尾将其除去if (num.lastIndexOf("零") == num.length() - 1) {num = num.substring(0, num.length() - 1);}return num;}/*** 替换字符串中内容*/public static String numReplace(String num, String oldStr, String newStr) {while (true) {// 判断字符串中是否包含指定字符if (num.indexOf(oldStr) == -1) {break;}// 替换字符串num = num.replaceAll(oldStr, newStr);}// 返回替换后的字符串return num;}/*** 金额转换*/public static String convert(double d) {// 实例化DecimalFormat对象DecimalFormat df = new DecimalFormat("#0.###");// 格式化double数字String strNum = df.format(d);// 判断是否包含小数点if (strNum.indexOf(".") != -1) {String num = strNum.substring(0, strNum.indexOf("."));// 整数部分大于12不能转换if (num.length() > 12) {System.out.println("数字太大,不能完成转换!");return "";}}String point = "";// 小数点if (strNum.indexOf(".") != -1) {point = "元";} else {point = "元整";}// 转换结果String result = getInteger(strNum) + point + getDecimal(strNum);if (result.startsWith("元")) { // 判断是字符串是否已"元"结尾result = result.substring(1, result.length()); // 截取字符串}return result; // 返回新的字符串}
}

6.生成二维码

<dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.3.3</version>
</dependency>
<dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.3.3</version>
</dependency>
    package com.util.cccm;import java.awt.BasicStroke;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Image;import java.awt.Shape;import java.awt.geom.RoundRectangle2D;import java.awt.image.BufferedImage;import java.io.File;import java.io.OutputStream;import java.util.Hashtable;import java.util.Random;import javax.imageio.ImageIO;import com.google.zxing.BarcodeFormat;import com.google.zxing.BinaryBitmap;import com.google.zxing.DecodeHintType;import com.google.zxing.EncodeHintType;import com.google.zxing.MultiFormatReader;import com.google.zxing.MultiFormatWriter;import com.google.zxing.Result;import com.google.zxing.common.BitMatrix;import com.google.zxing.common.HybridBinarizer;import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;/*** 二维码工具类* */public class QRCodeUtil {private static final String CHARSET = "utf-8";private static final String FORMAT_NAME = "JPG";// 二维码尺寸private static final int QRCODE_SIZE = 300;// LOGO宽度private static final int WIDTH = 60;// LOGO高度private static final int HEIGHT = 60;private static BufferedImage createImage(String content, String imgPath,boolean needCompress) throws Exception {Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();//  二维码容错率用字母表示,容错能力等级分为:L(7%)、M(15%)、Q(25%)、H(30%)四级,容错率越高,越容易被快速扫描hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);hints.put(EncodeHintType.CHARACTER_SET, CHARSET);hints.put(EncodeHintType.MARGIN, 1);BitMatrix bitMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);int width = bitMatrix.getWidth();int height = bitMatrix.getHeight();BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);for (int x = 0; x < width; x++) {for (int y = 0; y < height; y++) {image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000: 0xFFFFFFFF);}}if (imgPath == null || "".equals(imgPath)) {return image;}// 插入图片QRCodeUtil.insertImage(image, imgPath, needCompress);return image;}/*** 插入LOGO* * @param source*            二维码图片* @param imgPath*            LOGO图片地址* @param needCompress*            是否压缩* @throws Exception*/private static void insertImage(BufferedImage source, String imgPath,boolean needCompress) throws Exception {File file = new File(imgPath);if (!file.exists()) {System.err.println(""+imgPath+"   该文件不存在!");return;}Image src = ImageIO.read(new File(imgPath));int width = src.getWidth(null);int height = src.getHeight(null);if (needCompress) { // 压缩LOGOif (width > WIDTH) {width = WIDTH;}if (height > HEIGHT) {height = HEIGHT;}Image image = src.getScaledInstance(width, height,Image.SCALE_SMOOTH);BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);Graphics g = tag.getGraphics();g.drawImage(image, 0, 0, null); // 绘制缩小后的图g.dispose();src = image;}// 插入LOGOGraphics2D graph = source.createGraphics();int x = (QRCODE_SIZE - width) / 2;int y = (QRCODE_SIZE - height) / 2;graph.drawImage(src, x, y, width, height, null);Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);graph.setStroke(new BasicStroke(3f));graph.draw(shape);graph.dispose();}/*** 生成二维码(内嵌LOGO)* * @param content*            内容* @param imgPath*            LOGO地址* @param destPath*            存放目录* @param needCompress*            是否压缩LOGO* @throws Exception*/public static void encode(String content, String imgPath, String destPath,boolean needCompress) throws Exception {BufferedImage image = QRCodeUtil.createImage(content, imgPath,needCompress);mkdirs(destPath);String file = new Random().nextInt(99999999)+".jpg";ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file));}/*** 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)* @author lanyuan* Email: mmm333zzz520@163.com* @date 2013-12-11 上午10:16:36* @param destPath 存放目录*/public static void mkdirs(String destPath) {File file =new File(destPath);    //当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)if (!file.exists() && !file.isDirectory()) {file.mkdirs();}}/*** 生成二维码(内嵌LOGO)* * @param content*            内容* @param imgPath*            LOGO地址* @param destPath*            存储地址* @throws Exception*/public static void encode(String content, String imgPath, String destPath)throws Exception {QRCodeUtil.encode(content, imgPath, destPath, false);}/*** 生成二维码* * @param content*            内容* @param destPath*            存储地址* @param needCompress*            是否压缩LOGO* @throws Exception*/public static void encode(String content, String destPath,boolean needCompress) throws Exception {QRCodeUtil.encode(content, null, destPath, needCompress);}/*** 生成二维码* * @param content*            内容* @param destPath*            存储地址* @throws Exception*/public static void encode(String content, String destPath) throws Exception {QRCodeUtil.encode(content, null, destPath, false);}/*** 生成二维码(内嵌LOGO)* * @param content*            内容* @param imgPath*            LOGO地址* @param output*            输出流* @param needCompress*            是否压缩LOGO* @throws Exception*/public static void encode(String content, String imgPath,OutputStream output, boolean needCompress) throws Exception {BufferedImage image = QRCodeUtil.createImage(content, imgPath,needCompress);ImageIO.write(image, FORMAT_NAME, output);}/*** 生成二维码* * @param content*            内容* @param output*            输出流* @throws Exception*/public static void encode(String content, OutputStream output)throws Exception {QRCodeUtil.encode(content, null, output, false);}/*** 解析二维码* * @param file*            二维码图片* @return* @throws Exception*/public static String decode(File file) throws Exception {BufferedImage image;image = ImageIO.read(file);if (image == null) {return null;}BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));Result result;Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();hints.put(DecodeHintType.CHARACTER_SET, CHARSET);result = new MultiFormatReader().decode(bitmap, hints);String resultStr = result.getText();return resultStr;}/*** 解析二维码* * @param path*            二维码图片地址* @return* @throws Exception*/public static String decode(String path) throws Exception {return QRCodeUtil.decode(new File(path));}public static void main(String[] args) throws Exception {String text = "薯 灯可分列式本上楞珂要瓜熟蒂落!000000000000000";QRCodeUtil.encode(text, "c:/df.jsp", "c:/a/", true);}}
package com.util.cccm;import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;import com.google.zxing.LuminanceSource;public class BufferedImageLuminanceSource extends LuminanceSource {private final BufferedImage image;private final int left;private final int top;public BufferedImageLuminanceSource(BufferedImage image) {this(image, 0, 0, image.getWidth(), image.getHeight());}public BufferedImageLuminanceSource(BufferedImage image, int left,int top, int width, int height) {super(width, height);int sourceWidth = image.getWidth();int sourceHeight = image.getHeight();if (left + width > sourceWidth || top + height > sourceHeight) {throw new IllegalArgumentException("Crop rectangle does not fit within image data.");}for (int y = top; y < top + height; y++) {for (int x = left; x < left + width; x++) {if ((image.getRGB(x, y) & 0xFF000000) == 0) {image.setRGB(x, y, 0xFFFFFFFF); // = white}}}this.image = new BufferedImage(sourceWidth, sourceHeight,BufferedImage.TYPE_BYTE_GRAY);this.image.getGraphics().drawImage(image, 0, 0, null);this.left = left;this.top = top;}public byte[] getRow(int y, byte[] row) {if (y < 0 || y >= getHeight()) {throw new IllegalArgumentException("Requested row is outside the image: " + y);}int width = getWidth();if (row == null || row.length < width) {row = new byte[width];}image.getRaster().getDataElements(left, top + y, width, 1, row);return row;}public byte[] getMatrix() {int width = getWidth();int height = getHeight();int area = width * height;byte[] matrix = new byte[area];image.getRaster().getDataElements(left, top, width, height, matrix);return matrix;}public boolean isCropSupported() {return true;}public LuminanceSource crop(int left, int top, int width, int height) {return new BufferedImageLuminanceSource(image, this.left + left,this.top + top, width, height);}public boolean isRotateSupported() {return true;}public LuminanceSource rotateCounterClockwise() {int sourceWidth = image.getWidth();int sourceHeight = image.getHeight();AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0,0.0, 0.0, sourceWidth);BufferedImage rotatedImage = new BufferedImage(sourceHeight,sourceWidth, BufferedImage.TYPE_BYTE_GRAY);Graphics2D g = rotatedImage.createGraphics();g.drawImage(image, transform, null);g.dispose();int width = getWidth();return new BufferedImageLuminanceSource(rotatedImage, top,sourceWidth - (left + width), getHeight(), width);}
}

【basepro】常用util相关推荐

  1. 轻量型互联网应用架构方式

    点击上方 Java后端,选择 设为星标 优质文章,及时送达 作者 | 天如 链接 | http://suo.im/4qRPkj 一.前言 说到互联网应用架构,就绕不开微服务,当下(2019)最热门的微 ...

  2. Java 时间类汇总

    Java 7 六个时间类 时间类的介绍与对比 类名称 时间格式 java.util.Date(父类) 年月日时分秒 java.sql.Date(子类) 年月日 java.sql.Time(子类) 时分 ...

  3. java.util类,GitHub - yutaolian/JavaUtils: 总结的一些Java常用的util类

    JavaUtils 总结的一些Java常用的util类 ###1.格式化时间 SimpleDateFormat(DateFormat)实现线程安全的使用 众所周知SimpleDateFormat(Da ...

  4. Node.js中的常用工具类util

    util是一个Node.js核心模块,提供常用函数的集合,用于弥补JavaScript的功能的不足,util模块设计的主要目的是为了满足Node内部API的需求.其中包括:格式化字符串.对象的序列化. ...

  5. java中常用的包 类和接口_java.util包常用的类和接口

    标签:ash   可变   支持   set   组成   arraylist   层次结构   有序   结构 1. 常用接口 (1)Collection Collection 层次结构 中的根接口 ...

  6. android.util.Log常用的方法

    2019独角兽企业重金招聘Python工程师标准>>> android.util.Log常用的方法有以下5个: Log.v() Log.d() Log.i() Log.w() 以及 ...

  7. Java基础:Util包下常用的数据结构介绍

    内容摘要:线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应的类来实现基本的数据结构. 线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经 ...

  8. java常用类库——util包

    文章目录 util包 集合 Collection Iterator 集合的比较 子接口比较 List实现类比较 Set实现类比较 Map实现类比较 Collections 数组 Arrays 时间 D ...

  9. 用Java实现几种常用排序算法(先实现一个org.rut.util.algorithm.SortUtil)

    先实现org.rut.util.algorithm.SortUtil这个类(以后每个排序都会用到): package org.rut.util.algorithm; import org.rut.ut ...

最新文章

  1. steamvr unity 连接眼镜_150度FOV,自研显示方案,Kura公布全新AR眼镜Gallium
  2. thymeleaf模板的使用(转)
  3. 硅谷程序员佛系养生法:我不修bug, 谁修bug
  4. 实现threadlocal_ThreadLocal如何实现?
  5. Java基础————理解Integer对象的缓存策略
  6. 看FusionInsight Spark如何支持JDBCServer的多实例特性
  7. c语言成绩管理系统不用结构体,不用指针链表和结构体数组怎么编学生成绩管理系统啊...
  8. idea配置maven后提示 commond not found
  9. BZOJ 1067 降雨量(RMQ-ST+有毒的分类讨论)
  10. jmeter录制脚本(针对谷歌)
  11. 妇产科护理学名词解释
  12. android apk上架流程,Android apk上架国内应用市场流程
  13. c语言 结构体ppt,C语言知识学习结构体.ppt
  14. 关于 TRTC (实时音视频通话模式)在我司的实践
  15. 术语解释(PV、UV、QPS、TPS)
  16. d3.js 刷新折线图(包括坐标轴及路径的刷新及信息点提示)
  17. CentOS系统安装haproxy
  18. ubuntu16.04系统出现问题解决方案集锦。
  19. 3D地图+智能导航,用微信小程序轻松实现校园内导航
  20. unity LeapMotion 手势旋转,位移,缩放

热门文章

  1. ETL数据清洗Kettle工具
  2. 常规放大电路和差分放大电路
  3. Linux C语言 创建一个简单的守护进程
  4. 逻辑回归LogisticRegression
  5. 网页收藏栏小图标_如何设置在网页地址栏中的小图标
  6. textarea的placeholder怎么实现换行-新的方法
  7. MySQL的隐式类型转换
  8. 超简单 不进PE 不用U盘 自己重装电脑系统步骤
  9. Java小游戏-幸运抽奖-进阶版(可多抽取多次)
  10. ubuntu拷贝和移动文件和文件夹