当前位置:网站首页>Those commonly used tool classes and methods in hutool
Those commonly used tool classes and methods in hutool
2022-07-06 11:43:00 【Ride the wind to break the bug】
List of articles
Convert
Type conversion utility class , For conversion of various types of data .
// Convert to string
int a = 1;
String aStr = Convert.toStr(a);
// Convert to array of specified type
String[] b = {"1", "2", "3", "4"};
Integer[] bArr = Convert.toIntArray(b);
// Convert to date object
String dateStr = "2017-05-06";
Date date = Convert.toDate(dateStr);
// Convert to list
String[] strArr = {"a", "b", "c", "d"};
List<String> strList = Convert.toList(String.class, strArr);
DateUtil
Date time tool class , Some commonly used date time operation methods are defined .
//Date、long、Calendar Mutual conversion between
// current time
Date date = DateUtil.date();
//Calendar turn Date
date = DateUtil.date(Calendar.getInstance());
// Time stamping Date
date = DateUtil.date(System.currentTimeMillis());
// Automatic recognition of format conversion
String dateStr = "2017-03-01";
date = DateUtil.parse(dateStr);
// Custom format conversion
date = DateUtil.parse(dateStr, "yyyy-MM-dd");
// Format output date
String format = DateUtil.format(date, "yyyy-MM-dd");
// Get part of the year
int year = DateUtil.year(date);
// Get the month , from 0 Start counting
int month = DateUtil.month(date);
// Get the start of a day 、 End time
Date beginOfDay = DateUtil.beginOfDay(date);
Date endOfDay = DateUtil.endOfDay(date);
// Calculate the date time after the offset
Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 2);
// Calculate the offset between date and time
long betweenDay = DateUtil.between(date, newDate, DateUnit.DAY);
StrUtil
String utility class , Some common string manipulation methods are defined .
// Determine if it is an empty string
String str = "test";
StrUtil.isEmpty(str);
StrUtil.isNotEmpty(str);
// Remove the prefixes and suffixes of strings
StrUtil.removeSuffix("a.jpg", ".jpg");
StrUtil.removePrefix("a.jpg", "a.");
// Formatted string
String template = " It's just a placeholder :{}";
String str2 = StrUtil.format(template, " I'm a placeholder ");
LOGGER.info("/strUtil format:{}", str2);
ClassPathResource
obtain classPath The files under the , stay Tomcat Wait for the container ,classPath It's usually WEB-INF/classes.
// Access is defined in src/main/resources Profile in folder
ClassPathResource resource = new ClassPathResource("generator.properties");
Properties properties = new Properties();
properties.load(resource.getStream());
LOGGER.info("/classPath:{}", properties);
ReflectUtil
Java Reflection tools , It can be used to reflect the methods of getting classes and creating objects .
// Get all the methods of a class
Method[] methods = ReflectUtil.getMethods(PmsBrand.class);
// Get the specified method of a class
Method method = ReflectUtil.getMethod(PmsBrand.class, "getId");
// Use reflection to create objects
PmsBrand pmsBrand = ReflectUtil.newInstance(PmsBrand.class);
// The method of reflecting the execution object
ReflectUtil.invoke(pmsBrand, "setId", 1);
NumberUtil
Digital processing tools , It can be used for addition, subtraction, multiplication and division of various types of numbers and judgment types .
double n1 = 1.234;
double n2 = 1.234;
double result;
// Yes float、double、BigDecimal Do addition, subtraction, multiplication and division
result = NumberUtil.add(n1, n2);
result = NumberUtil.sub(n1, n2);
result = NumberUtil.mul(n1, n2);
result = NumberUtil.div(n1, n2);
// Keep two decimal places
BigDecimal roundNum = NumberUtil.round(n1, 2);
String n3 = "1.234";
// Judge whether it is a number 、 Integers 、 Floating point numbers
NumberUtil.isNumber(n3);
NumberUtil.isInteger(n3);
NumberUtil.isDouble(n3);
BeanUtil
JavaBean Tool class of , Can be used for Map And JavaBean The mutual transformation of objects and the copy of object properties .
PmsBrand brand = new PmsBrand();
brand.setId(1L);
brand.setName(" millet ");
brand.setShowStatus(0);
//Bean turn Map
Map<String, Object> map = BeanUtil.beanToMap(brand);
LOGGER.info("beanUtil bean to map:{}", map);
//Map turn Bean
PmsBrand mapBrand = BeanUtil.mapToBean(map, PmsBrand.class, false);
LOGGER.info("beanUtil map to bean:{}", mapBrand);
//Bean Property copy
PmsBrand copyBrand = new PmsBrand();
BeanUtil.copyProperties(brand, copyBrand);
LOGGER.info("beanUtil copy properties:{}", copyBrand);
CollUtil
Tool class for collection operation , Some common set operations are defined .
// Array to list
String[] array = new String[]{"a", "b", "c", "d", "e"};
List<String> list = CollUtil.newArrayList(array);
//join: When the array is converted to a string, add the connection symbol
String joinStr = CollUtil.join(list, ",");
LOGGER.info("collUtil join:{}", joinStr);
// Reconverts a string separated by a join symbol to a list
List<String> splitList = StrUtil.split(joinStr, ',');
LOGGER.info("collUtil split:{}", splitList);
// Create a new Map、Set、List
HashMap<Object, Object> newMap = CollUtil.newHashMap();
HashSet<Object> newHashSet = CollUtil.newHashSet();
ArrayList<Object> newList = CollUtil.newArrayList();
// Determine whether the list is empty
CollUtil.isEmpty(list);
MapUtil
Map Operation tool class , Can be used to create Map Object and judgment Map Is it empty .
// Add multiple key value pairs to Map in
Map<Object, Object> map = MapUtil.of(new String[][]{
{"key1", "value1"},
{"key2", "value2"},
{"key3", "value3"}
});
// Judge Map Is it empty
MapUtil.isEmpty(map);
MapUtil.isNotEmpty(map);
AnnotationUtil
Annotation tool class , Can be used to get comments and values specified in comments .
// Get the specified class 、 Method 、 Field 、 List of annotations on the constructor
Annotation[] annotationList = AnnotationUtil.getAnnotations(HutoolController.class, false);
LOGGER.info("annotationUtil annotations:{}", annotationList);
// Get the specified type annotation
Api api = AnnotationUtil.getAnnotation(HutoolController.class, Api.class);
LOGGER.info("annotationUtil api value:{}", api.description());
// Gets the value of the specified type annotation
Object annotationValue = AnnotationUtil.getAnnotationValue(HutoolController.class, RequestMapping.class);
SecureUtil
Encryption and decryption tool class , Can be used for MD5 encryption .
//MD5 encryption
String str = "123456";
String md5Str = SecureUtil.md5(str);
LOGGER.info("secureUtil md5:{}", md5Str);
CaptchaUtil
Verification code tool class , Can be used to generate graphic verification codes .
// Generate captcha image
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
try {
request.getSession().setAttribute("CAPTCHA_KEY", lineCaptcha.getCode());
response.setContentType("image/png");// Tell the browser to output pictures
response.setHeader("Pragma", "No-cache");// Disable browser caching
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expire", 0);
lineCaptcha.write(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
边栏推荐
- Did you forget to register or load this tag
- [Kerberos] deeply understand the Kerberos ticket life cycle
- Vs2019 desktop app quick start
- When using lambda to pass parameters in a loop, the parameters are always the same value
- 机器学习笔记-Week02-卷积神经网络
- ImportError: libmysqlclient. so. 20: Cannot open shared object file: no such file or directory solution
- L2-006 树的遍历 (25 分)
- C语言读取BMP文件
- [NPUCTF2020]ReadlezPHP
- 库函数--(持续更新)
猜你喜欢

Learn winpwn (2) -- GS protection from scratch

Kept VRRP script, preemptive delay, VIP unicast details

人脸识别 face_recognition

How to configure flymcu (STM32 serial port download software) is shown in super detail
![[yarn] CDP cluster yarn configuration capacity scheduler batch allocation](/img/85/0121478f8fc427d1200c5f060d5255.png)
[yarn] CDP cluster yarn configuration capacity scheduler batch allocation

第4阶段 Mysql数据库

Vs2019 use wizard to generate an MFC Application

保姆级出题教程

Nanny level problem setting tutorial

vs2019 使用向导生成一个MFC应用程序
随机推荐
Solution of deleting path variable by mistake
Solve the problem of installing failed building wheel for pilot
error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_ s instead
Niuke novice monthly race 40
[Bluebridge cup 2021 preliminary] weight weighing
AcWing 1294. Cherry Blossom explanation
C语言读取BMP文件
MTCNN人脸检测
QT creator support platform
MySQL与c语言连接(vs2019版)
[蓝桥杯2020初赛] 平面切分
Wangeditor rich text component - copy available
L2-007 family real estate (25 points)
AcWing 1298.曹冲养猪 题解
4、安装部署Spark(Spark on Yarn模式)
小L的试卷
02 staff information management after the actual project
Codeforces Round #771 (Div. 2)
AcWing 179. Factorial decomposition problem solution
L2-006 树的遍历 (25 分)