commons lang包常用方法集锦
2024-01-07 23:33:48
在开发代码的时候,有一些工具类帮助的情况下,会更高效的完成代码实现。我们经常用的有commons-lang和guava工具包。下面针对common-lang包下常用的几个工具类进行函数说明。
NumberUtils
numberUtils中定义的常数
| 常量名 | 值 | 说明 |
|---|---|---|
LONG_ZERO | 0L | 长整数类型的零 |
LONG_ONE | 1L | 长整数类型的一 |
LONG_MINUS_ONE | -1L | 长整数类型的负一 |
NumberUtils中定义的常数有0、1、-1,除了上面展示的Long类型之外,还定义了Integer、Short、Byte、Double、FLoat类型的,另外Integer类型的还定义了2。
| 方法名 | 说明 |
|---|---|
toInt(String) | 将字符串转换为整数类型 |
toInt(String, int) | 将字符串转换为整数类型,如果转换失败则返回默认值 |
toLong(String) | 将字符串转换为长整数类型 |
toLong(String, long) | 将字符串转换为长整数类型,如果转换失败则返回默认值 |
toFloat(String) | 将字符串转换为单精度浮点数类型 |
toFloat(String, float) | 将字符串转换为单精度浮点数类型,如果转换失败则返回默认值 |
toDouble(String) | 将字符串转换为双精度浮点数类型 |
toDouble(String, double) | 将字符串转换为双精度浮点数类型,如果转换失败则返回默认值 |
toDouble(BigDecimal) | 将BigDecimal类型转换为双精度浮点数类型 |
toDouble(BigDecimal, double) | 将BigDecimal类型转换为双精度浮点数类型,如果转换失败则返回默认值 |
toByte(String) | 将字符串转换为字节类型 |
toByte(String, byte) | 将字符串转换为字节类型,如果转换失败则返回默认值 |
toShort(String) | 将字符串转换为短整数类型 |
toShort(String, short) | 将字符串转换为短整数类型,如果转换失败则返回默认值 |
toScaledBigDecimal(BigDecimal) | 将BigDecimal类型转换为指定精度的BigDecimal类型 |
toScaledBigDecimal(BigDecimal, int, RoundingMode) | 将BigDecimal类型转换为指定精度和舍入模式的BigDecimal类型 |
toScaledBigDecimal(Float) | 将Float类型转换为指定精度的BigDecimal类型 |
toScaledBigDecimal(Float, int, RoundingMode) | 将Float类型转换为指定精度和舍入模式的BigDecimal类型 |
toScaledBigDecimal(Double) | 将Double类型转换为指定精度的BigDecimal类型 |
toScaledBigDecimal(Double, int, RoundingMode) | 将Double类型转换为指定精度和舍入模式的BigDecimal类型 |
toScaledBigDecimal(String) | 将字符串转换为指定精度的BigDecimal类型 |
toScaledBigDecimal(String, int, RoundingMode) | 将字符串转换为指定精度和舍入模式的BigDecimal类型 |
createNumber(String) | 将字符串转换为对应的数字类型 |
createFloat(String) | 将字符串转换为Float类型 |
createDouble(String) | 将字符串转换为Double类型 |
createInteger(String) | 将字符串转换为Integer类型 |
createLong(String) | 将字符串转换为Long类型 |
createBigInteger(String) | 将字符串转换为BigInteger类型 |
createBigDecimal(String) | 将字符串转换为BigDecimal类型 |
min(long...) | 返回一组长整数中的最小值 |
min(int...) | 返回一组整数中的最小值 |
min(short...) | 返回一组短整数中的最小值 |
min(byte...) | 返回一组字节中的最小值 |
min(double...) | 返回一组双精度浮点数中的最小值 |
min(float...) | 返回一组单精度浮点数中的最小值 |
max(long...) | 返回一组长整数中的最大值 |
max(int...) | 返回一组整数中的最大值 |
max(short...) | 返回一组短整数中的最大值 |
max(byte...) | 返回一组字节中的最大值 |
max(double...) | 返回一组双精度浮点数中的最大值 |
max(float...) | 返回一组单精度浮点数中的最大值 |
isDigits(String) | 判断字符串是否只包含数字字符 |
isCreatable(String) | 判断字符串是否可以转换为数字类型 |
isParsable(String) | 判断字符串是否可以解析为数字类型 |
compare(int, int) | 比较两个整数的大小 |
compare(long, long) | 比较两个长整数的大小 |
compare(short, short) | 比较两个短整数的大小 |
compare(byte, byte) | 比较两个字节的大小 |
DateFormatUtils
| 方法名 | 说明 |
|---|---|
formatUTC(long, String) | 将指定的时间戳(毫秒)转换为指定格式的字符串,使用UTC时区 |
formatUTC(Date, String) | 将指定的日期对象转换为指定格式的字符串,使用UTC时区 |
formatUTC(long, String, Locale) | 将指定的时间戳(毫秒)转换为指定格式的字符串,使用UTC时区和指定的Locale |
formatUTC(Date, String, Locale) | 将指定的日期对象转换为指定格式的字符串,使用UTC时区和指定的Locale |
format(long, String) | 将指定的时间戳(毫秒)转换为指定格式的字符串 |
format(Date, String) | 将指定的日期对象转换为指定格式的字符串 |
format(Calendar, String) | 将指定的日历对象转换为指定格式的字符串 |
format(long, String, TimeZone) | 将指定的时间戳(毫秒)转换为指定格式的字符串,使用指定的时区 |
format(Date, String, TimeZone) | 将指定的日期对象转换为指定格式的字符串,使用指定的时区 |
format(Calendar, String, TimeZone) | 将指定的日历对象转换为指定格式的字符串,使用指定的时区 |
format(long, String, Locale) | 将指定的时间戳(毫秒)转换为指定格式的字符串,使用默认时区和指定的Locale |
format(Date, String, Locale) | 将指定的日期对象转换为指定格式的字符串,使用默认时区和指定的Locale |
format(Calendar, String, Locale) | 将指定的日历对象转换为指定格式的字符串,使用默认时区和指定的Locale |
format(long, String, TimeZone, Locale) | 将指定的时间戳(毫秒)转换为指定格式的字符串,使用指定的时区和Locale |
format(Date, String, TimeZone, Locale) | 将指定的日期对象转换为指定格式的字符串,使用指定的时区和Locale |
format(Calendar, String, TimeZone, Locale) | 将指定的日历对象转换为指定格式的字符串,使用指定的时区和Locale |
DateUtils
| 方法名 | 含义 |
|---|---|
| isSameDay(Date date1, Date date2) | 判断两个日期是否是同一天 |
| isSameDay(Calendar cal1, Calendar cal2) | 判断两个日历对象是否是同一天 |
| isSameInstant(Date date1, Date date2) | 判断两个日期是否是同一时刻 |
| isSameInstant(Calendar cal1, Calendar cal2) | 判断两个日历对象是否是同一时刻 |
| isSameLocalTime(Calendar cal1, Calendar cal2) | 判断两个日历对象是否是同一本地时间 |
| parseDate(String str, String… parsePatterns) | 解析日期字符串为Date对象 |
| parseDate(String str, Locale locale, String… parsePatterns) | 解析日期字符串为Date对象,指定Locale |
| parseDateStrictly(String str, String… parsePatterns) | 严格解析日期字符串为Date对象 |
| parseDateStrictly(String str, Locale locale, String… parsePatterns) | 严格解析日期字符串为Date对象,指定Locale |
| addYears(Date date, int amount) | 在日期上增加指定年数 |
| addMonths(Date date, int amount) | 在日期上增加指定月数 |
| addWeeks(Date date, int amount) | 在日期上增加指定周数 |
| addDays(Date date, int amount) | 在日期上增加指定天数 |
| addHours(Date date, int amount) | 在日期上增加指定小时数 |
| addMinutes(Date date, int amount) | 在日期上增加指定分钟数 |
| addSeconds(Date date, int amount) | 在日期上增加指定秒数 |
| addMilliseconds(Date date, int amount) | 在日期上增加指定毫秒数 |
| setYears(Date date, int amount) | 设置日期的年份 |
| setMonths(Date date, int amount) | 设置日期的月份 |
| setDays(Date date, int amount) | 设置日期的天数 |
| setHours(Date date, int amount) | 设置日期的小时数 |
| setMinutes(Date date, int amount) | 设置日期的分钟数 |
| setSeconds(Date date, int amount) | 设置日期的秒数 |
| setMilliseconds(Date date, int amount) | 设置日期的毫秒数 |
| toCalendar(Date date) | 将Date对象转换为Calendar对象 |
| toCalendar(Date date, TimeZone tz) | 将Date对象转换为指定时区的Calendar对象 |
| round(Date date, int field) | 对日期进行四舍五入 |
| round(Calendar date, int field) | 对日历对象进行四舍五入 |
| truncate(Date date, int field) | 对日期进行截断 |
| truncate(Calendar date, int field) | 对日历对象进行截断 |
| ceiling(Date date, int field) | 对日期进行向上取整 |
| ceiling(Calendar date, int field) | 对日历对象进行向上取整 |
| getFragmentInMilliseconds(Date date, int fragment) | 获取日期的指定精度的时间片段(毫秒) |
| getFragmentInSeconds(Date date, int fragment) | 获取日期的指定精度的时间片段(秒) |
| getFragmentInMinutes(Date date, int fragment) | 获取日期的指定精度的时间片段(分钟) |
| getFragmentInHours(Date date, int fragment) | 获取日期的指定精度的时间片段(小时) |
| getFragmentInDays(Date date, int fragment) | 获取日期的指定精度的时间片段(天) |
| getFragmentInMilliseconds(Calendar calendar, int fragment) | 获取日历对象的指定精度的时间片段(毫秒) |
| getFragmentInSeconds(Calendar calendar, int fragment) | 获取日历对象的指定精度的时间片段(秒) |
| getFragmentInMinutes(Calendar calendar, int fragment) | 获取日历对象的指定精度的时间片段(分钟) |
| getFragmentInHours(Calendar calendar, int fragment) | 获取日历对象的指定精度的时间片段(小时) |
| getFragmentInDays(Calendar calendar, int fragment) | 获取日历对象的指定精度的时间片段(天) |
| truncatedEquals(Calendar cal1, Calendar cal2, int field) | 判断两个日历对象在指定精度下是否相等 |
| truncatedEquals(Date date1, Date date2, int field) | 判断两个日期在指定精度下是否相等 |
| truncatedCompareTo(Calendar cal1, Calendar cal2, int field) | 比较两个日历对象在指定精度下的大小 |
| truncatedCompareTo(Date date1, Date date2, int field) | 比较两个日期在指定精度下的大小 |
ImmutablePair
| 方法名 | 含义 |
|---|---|
nullPair() | 返回一个空的ImmutablePair对象 |
of(L left, R right) | 创建一个包含指定左值和右值的ImmutablePair对象 |
getLeft() | 获取ImmutablePair对象的左值 |
getRight() | 获取ImmutablePair对象的右值 |
| 还有一个Pair类,和ImmutablePair是两个不同的类。Pair是可变的,而ImmutablePair是不可变的。 |
ImmutableTriple
| 方法名 | 含义 |
|---|---|
nullTriple() | 返回一个空的ImmutableTriple对象 |
of(L left, M middle, R right) | 创建一个包含指定左值、中值和右值的ImmutableTriple对象 |
getLeft() | 获取ImmutableTriple对象的左值 |
getMiddle() | 获取ImmutableTriple对象的中值 |
getRight() | 获取ImmutableTriple对象的右值 |
| 还有一个Triple类,和ImmutableTriple是两个不同的类。Triple是可变的,而ImmutableTriple是不可变的。 |
ArrayUtils
| 方法名 | 含义 |
|---|---|
| add | 向数组中添加元素 |
| addAll | 向数组中添加多个元素 |
| clone | 克隆数组 |
| contains | 判断数组是否包含指定元素 |
| containsAny | 判断数组是否包含指定的任意一个元素 |
| containsOnly | 判断数组是否只包含指定的元素 |
| containsOnlyOnce | 判断数组是否只包含指定元素一次 |
| containsAny | 判断数组是否包含指定的任意一个元素 |
| getLength | 获取数组的长度 |
| indexOf | 获取指定元素在数组中的索引 |
| insert | 在指定位置插入元素 |
| isEmpty | 判断数组是否为空 |
| isNotEmpty | 判断数组是否不为空 |
| isSameLength | 判断两个数组是否长度相同 |
| isSameType | 判断两个数组是否类型相同 |
| lastIndexOf | 获取指定元素在数组中的最后一个索引 |
| nullToEmpty | 将null转换为空数组 |
| remove | 移除数组中指定位置的元素 |
| removeAll | 移除数组中指定元素 |
| removeElement | 移除数组中指定元素 |
| removeElements | 移除数组中指定元素 |
| removeAllOccurrences | 移除数组中所有指定元素的出现 |
| reverse | 反转数组 |
| subarray | 获取数组的子数组 |
| swap | 交换数组中两个位置的元素 |
| toArray | 将集合转换为数组 |
| toMap | 将数组转换为Map |
| toString | 将数组转换为字符串 |
| toPrimitive | 将包装类型数组转换为基本类型数组 |
| toObject | 将基本类型数组转换为包装类型数组 |
| nullToDefault | 将null转换为默认值 |
| defaultIfNull | 如果数组为null,则返回默认数组 |
| isSameLength | 判断多个数组是否长度相同 |
| isSameType | 判断多个数组是否类型相同 |
| addAll | 合并多个数组 |
| clone | 克隆多个数组 |
| indexOf | 获取多个数组中指定元素的索引 |
| lastIndexOf | 获取多个数组中指定元素的最后一个索引 |
| removeAllOccurrences | 移除多个数组中所有指定元素的出现 |
| reverse | 反转多个数组 |
| subarray | 获取多个数组的子数组 |
| swap | 交换多个数组中两个位置的元素 |
| toString | 将多个数组转换为字符串 |
| toPrimitive | 将多个包装类型数组转换为基本类型数组 |
| toObject | 将多个基本类型数组转换为包装类型数组 |
| nullToDefault | 将多个数组中的null转换为默认值 |
| defaultIfNull | 如果多个数组中有任意一个为null,则返回默认数组 |
RandomUtils
| 方法名 | 含义 |
|---|---|
nextBoolean() | 生成一个随机的布尔值 |
nextBytes(int count) | 生成指定长度的随机字节数组 |
nextInt(int startInclusive, int endExclusive) | 生成一个指定范围内的随机整数,包括起始值但不包括结束值 |
nextInt() | 生成一个非负的随机整数 |
nextLong(long startInclusive, long endExclusive) | 生成一个指定范围内的随机长整数,包括起始值但不包括结束值 |
nextLong() | 生成一个非负的随机长整数 |
nextDouble(double startInclusive, double endInclusive) | 生成一个指定范围内的随机双精度浮点数,包括起始值和结束值 |
nextDouble() | 生成一个非负的随机双精度浮点数 |
nextFloat(float startInclusive, float endInclusive) | 生成一个指定范围内的随机单精度浮点数,包括起始值和结束值 |
nextFloat() | 生成一个非负的随机单精度浮点数 |
StringUtils
| 方法名 | 含义 |
|---|---|
| abbreviate | 缩略字符串 |
| abbreviateMiddle | 缩略字符串并在中间添加省略号 |
| appendIfMissing | 如果字符串不以指定后缀结尾,则追加后缀 |
| appendIfMissingIgnoreCase | 如果字符串不以指定后缀结尾(忽略大小写),则追加后缀 |
| capitalize | 将字符串的首字母大写 |
| center | 将字符串居中对齐 |
| chomp | 删除字符串末尾的指定字符 |
| chop | 删除字符串末尾的最后一个字符 |
| contains | 判断字符串是否包含指定字符 |
| containsAny | 判断字符串是否包含指定的任意一个字符 |
| containsIgnoreCase | 判断字符串是否包含指定字符(忽略大小写) |
| countMatches | 统计字符串中指定字符的出现次数 |
| defaultIfBlank | 如果字符串为空或只包含空白字符,则返回默认值 |
| defaultIfEmpty | 如果字符串为空,则返回默认值 |
| deleteWhitespace | 删除字符串中的空白字符 |
| difference | 获取两个字符串的不同部分 |
| endsWith | 判断字符串是否以指定后缀结尾 |
| endsWithIgnoreCase | 判断字符串是否以指定后缀结尾(忽略大小写) |
| equals | 判断两个字符串是否相等 |
| equalsIgnoreCase | 判断两个字符串是否相等(忽略大小写) |
| getCommonPrefix | 获取多个字符串的共同前缀 |
| indexOf | 获取指定字符在字符串中的索引 |
| indexOfIgnoreCase | 获取指定字符在字符串中的索引(忽略大小写) |
| isAllBlank | 判断多个字符串是否全部为空或只包含空白字符 |
| isAllEmpty | 判断多个字符串是否全部为空 |
| isAnyBlank | 判断多个字符串是否存在为空或只包含空白字符的情况 |
| isAnyEmpty | 判断多个字符串是否存在为空的情况 |
| isBlank | 判断字符串是否为空或只包含空白字符 |
| isEmpty | 判断字符串是否为空 |
| isNotBlank | 判断字符串是否不为空且不只包含空白字符 |
| isNotEmpty | 判断字符串是否不为空 |
| join | 将多个字符串连接起来 |
| joinWith | 使用指定的分隔符将多个字符串连接起来 |
| lastIndexOf | 获取指定字符在字符串中的最后一个索引 |
| left | 获取字符串的左侧指定长度的子串 |
| length | 获取字符串的长度 |
| lowerCase | 将字符串转换为小写 |
| normalizeSpace | 去除字符串中的多余空白字符 |
| ordinalIndexOf | 获取字符串中指定字符的第N次出现的索引 |
| overlay | 在字符串指定位置插入另一个字符串 |
| prependIfMissing | 如果字符串不以指定前缀开头,则在开头添加前缀 |
| prependIfMissingIgnoreCase | 如果字符串不以指定前缀开头(忽略大小写),则在开头添加前缀 |
| remove | 删除字符串中的指定字符 |
| removeEnd | 删除字符串末尾的指定后缀 |
| removeEndIgnoreCase | 删除字符串末尾的指定后缀(忽略大小写) |
| removeStart | 删除字符串开头的指定前缀 |
| removeStartIgnoreCase | 删除字符串开头的指定前缀(忽略大小写) |
| repeat | 重复字符串指定次数 |
| replace | 替换字符串中的指定字符 |
| replaceChars | 替换字符串中的指定字符序列 |
| replaceEach | 替换字符串中的指定字符序列 |
| replaceIgnoreCase | 替换字符串中的指定字符(忽略大小写) |
| replaceOnce | 替换字符串中的第一个匹配项 |
| reverse | 反转字符串 |
| right | 获取字符串的右侧指定长度的子串 |
| split | 分割字符串 |
| startsWith | 判断字符串是否以指定前缀开头 |
| startsWithIgnoreCase | 判断字符串是否以指定前缀开头(忽略大小写) |
| ------ | ---- |
| strip | 去除字符串首尾的空白字符 |
| stripToNull | 去除字符串首尾的空白字符,并将空字符串转换为null |
| stripToEmpty | 去除字符串首尾的空白字符,并将空字符串转换为空字符串 |
| substring | 获取字符串的子串 |
| substringAfter | 获取指定字符之后的子串 |
| substringAfterLast | 获取最后一个指定字符之后的子串 |
| substringBefore | 获取指定字符之前的子串 |
| substringBeforeLast | 获取最后一个指定字符之前的子串 |
| swapCase | 大小写互换 |
| trim | 去除字符串首尾的空白字符 |
| trimToEmpty | 去除字符串首尾的空白字符,并将空字符串转换为空字符串 |
| trimToNull | 去除字符串首尾的空白字符,并将空字符串转换为null |
| uncapitalize | 将字符串的首字母小写 |
| upperCase | 将字符串转换为大写 |
| wrap | 使用指定字符包裹字符串 |
文章来源:https://blog.csdn.net/u013978512/article/details/133908170
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!