map和bean的互相转换
// bean->map
Map<String, String> map = BeanUtils.describe(user);
// map->bean
BeanUtils.populate(user, map);
Codec
常见的编码,解码方法封装
// Base64
Base64.encodeBase64String(byte[] binaryData)
Base64.decodeBase64(String base64String)
// MD5
DigestUtils.md5Hex(String data)
// URL
URLCodec.decodeUrl(byte[] bytes);
URLCodec.encodeUrl(BitSet urlsafe, byte[] bytes);
StringUtils
//判断是否是空格字符
System.out.println(StringUtils.isBlank(" "));
//将数组中的内容以,分隔
System.out.println(StringUtils.join(test,","));
//在右边加下字符,使之总长度为6
System.out.println(StringUtils.rightPad("abc", 6, 'T'));
//首字母大写
System.out.println(StringUtils.capitalize("abc"));
//Deletes all whitespaces from a String 删除所有空格
System.out.println( StringUtils.deleteWhitespace(" ab c "));
//判断是否包含这个字符
System.out.println( StringUtils.contains("abc", "ba"));
//包含任何一个就返回true
StringUtils.containsAny(response.getReturn_msg(), "退款", "已退款")
//如果是a开头就去掉a 返回bc
System.out.println(StringUtils.stripStart("abc", "a"));
//如果是c结尾就去掉c 返回ab
System.out.println(StringUtils.stripEnd("abc", "c"));
//如果是a开头返回true
System.out.println(StringUtils.startsWith("abc", "a"));
//如果是c结尾返回true
System.out.println(StringUtils.endsWith("abc","c"));
ArrayList<String> strings = new ArrayList<>();
strings.add("1");
strings.add("2");
strings.add("3");
System.out.println(strings.toString());//返回[1, 2, 3]
System.out.println(StringUtils.join(strings,","));//返回1,2,3 join 等价于去掉两端的中括号
Collections
交并差等操作
// 判空
CollectionUtils.isEmpty(collA);
// 交集
CollectionUtils.retainAll(collA, collB);
// 并集
CollectionUtils.union(collA, collB);
// 差集
CollectionUtils.subtract(collA, collB);
// 判等
CollectionUtils.isEqualCollection(collA, collB);
I/O
IOUtils对IO操作的封装
// 拷贝流
IOUtils.copy(InputStream input, OutputStream output);
// 从流中读取内容,转为list
List<String> line = IOUtils.readLines(InputStream input, Charset encoding);
FileUtils对文件操作类的封装
File file = new File("/show/data.text");
// 按行读取文件
List<String> lines = FileUtils.readLines(file, "UTF-8");
// 将字符串写入文件
FileUtils.writeStringToFile(file, "test", "UTF-8");
// 文件复制
FileUtils.copyFile(srcFile, destFile);
Pair和Triple
当想返回2个或3个值,但这几个值没有相关性,没有必要单独封装一个对象,就可以用到如下数据结构,返回Pair或Triple对象
Pair<Integer, Integer> pair = new ImmutablePair<>(1, 2);
// 1 2
System.out.println(pair.getLeft() + " " + pair.getRight());
Triple<Integer, Integer, Integer> triple = new ImmutableTriple<>(1,2,3);
// 1 2 3
System.out.println(triple.getLeft() + " " + triple.getMiddle() + " " + triple.getRight());
Joda Time
package com.xxx.producer.utils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
/**
* JodaTime 时间工具
*
* @author yangjunxiong
* @date 2018/11/6 16:31
*/
public final class JodaTimeUtils {
private JodaTimeUtils() {
}
/**
* 根据当天日期,获取当天的时间临界点时间
*
* @param localDate
* @return
*/
public static ImmutablePair<LocalDateTime, LocalDateTime> criticalTime(LocalDate localDate) {
Asserts.notNull(localDate);
LocalDateTime beginTime = new LocalDateTime(localDate.getYear(), localDate.getMonthOfYear(), localDate.getDayOfMonth(), 0, 0, 0, 0);
LocalDateTime endTime = new LocalDateTime(localDate.getYear(), localDate.getMonthOfYear(), localDate.getDayOfMonth(), 23, 59, 59, 999);
return ImmutablePair.of(beginTime, endTime);
}
/**
* 获取当天的最小值
*
* @param localDate
* @return
*/
public static LocalDateTime minTime(LocalDate localDate) {
Asserts.notNull(localDate);
return new LocalDateTime(localDate.getYear(), localDate.getMonthOfYear(), localDate.getDayOfMonth(), 0, 0, 0, 0);
}
/**
* 获取当天的最大值
*
* @param localDate
* @return
*/
public static LocalDateTime maxTime(LocalDate localDate) {
Asserts.notNull(localDate);
return new LocalDateTime(localDate.getYear(), localDate.getMonthOfYear(), localDate.getDayOfMonth(), 23, 59, 59, 999);
}
/*
* 【 获取时间段内的所有日期 】
*
* @author YJX
* @date 2019/11/11 22:46
* @param startTime
* @param endTime
* @return java.util.ArrayList<org.joda.time.LocalDate>
**/
public static ArrayList<LocalDate> getLocalDates(LocalDate startTime, LocalDate endTime) {
ArrayList<LocalDate> localDates = new ArrayList<>();
do {
localDates.add(startTime);
startTime = startTime.plusDays(1);
} while (endTime.toDate().getTime() > startTime.toDate().getTime());
return localDates;
}
/*
* 【 获取时间段内的所有日期 】
*
* @author YJX
* @date 2019/11/11 22:46
* @param startTime
* @param endTime
* @return java.util.ArrayList<org.joda.time.LocalDate>
**/
private ArrayList<LocalDate> getLocalDates(Date startTime, Date endTime) {
LocalDate tradeDate = new LocalDate(startTime);
ArrayList<LocalDate> localDates = new ArrayList<>();
do {
localDates.add(tradeDate);
tradeDate = tradeDate.plusDays(1);
} while (endTime.getTime() > tradeDate.toDate().getTime());
return localDates;
}
}