1

java.lang.Objects / org.apache.commons.lang.ObjectUtils;

  • boolean isNull(Object obj); 判断null
  • boolean equals(Object a, Object b); 等值判断
  • int hashCode(Object o); 哈希值
  • String toString(Object o, String defaultValue);

java.util.Collections

  • void sort(List list); 排序
  • ? max(collection); 最大值
  • ? min(collection); 最小值
  • void shuffle(list); 洗牌,随机重排序

CollectionUtils

org.apache.commons.collections.CollectionUtils

  • boolean isEmpty(collection); 非空判断
  • void addAll(Collection collection, Iterator iterator); 批量添加集合(Iterator)
  • void addAll(Collection collection, Enumeration enumeration); 批量添加集合(Enumeration)
  • void addAll(Collection collection, Object[] elements); 批量添加集合(数组)

DateFormatUtils 时间格式化

String format(Date date, String pattern);
String format(Calendar calendar, String pattern);
String format(long millis, String pattern);

DateUtils 时间操作

时间计算

  • Date addYears(Date date, int amount)
  • Date addMonths(Date date, int amount)
  • Date addWeeks(Date date, int amount)
  • Date addDays(Date date, int amount)
  • Date addHours(Date date, int amount)
  • Date addMinutes(Date date, int amount)
  • Date addSeconds(Date date, int amount)
  • Date addMilliseconds(Date date, int amount)
  • Date setYears(Date date, int amount)
  • Date setMonths(Date date, int amount)
  • Date setDays(Date date, int amount)
  • Date setHours(Date date, int amount)
  • Date setMinutes(Date date, int amount)
  • Date setSeconds(Date date, int amount)
  • Date setMilliseconds(Date date, int amount)

时间范围计算

  • Date ceiling(Date date, inti field); //根据时间单位字段,计算最大值,如 "2017-12-07 16:46:59",field为Calendar.DATE,计算出的Date为:2017-12-08 00:00:00
Date ceiling1 = DateUtils.ceiling(new Date(), Calendar.YEAR);
Date ceiling2 = DateUtils.ceiling(new Date(), Calendar.MONTH);
Date ceiling3 = DateUtils.ceiling(new Date(), Calendar.DATE);
Date ceiling4 = DateUtils.ceiling(new Date(), Calendar.HOUR);

// 输出结果为
2018-01-01 00:00:00
2018-01-01 00:00:00
2017-12-08 00:00:00
2017-12-07 17:00:00

时间比较

boolean isSameDay(Date date1, Date date2);

parse 字符串转化为时间

DateUtils.parseDate("2017-12-07",new String[]{"yyyy-MM-dd"})


我有切糕
863 声望45 粉丝

Java架构师,技术经理