1.阿里fastjson使用
//用户组对象转JSON串
String jsonString = JSON.toJSONString(group);
//JSON串转用户组对象
UserGroup group2 = JSON.parseObject(jsonString, UserGroup.class);
// 用户对象数组转JSON串
String jsonString2 = JSON.toJSONString(users);
// JSON串转用户对象列表
List users2 = JSON.parseArray(jsonString2, User.class);
2.日期转化
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
//日期格式转字符串
sdf.format();
//字符串转日期格式
sdf.parse();
3.日期计算
计算两个日期相差天数
/**
* 计算日期相差天数
* @return int
*/
public static int getDaysBetween(Calendar cal01,Calendar cal02){
if(cal01.after(cal02)){
Calendar swap = cal01;
cal01 = cal02;
cal02 = swap;
}
int days = cal02.get(Calendar.DAY_OF_YEAR) - cal01.get(Calendar.DAY_OF_YEAR);
int year2 = cal02.get(Calendar.YEAR);
if(cal01.get(Calendar.YEAR) != year2){
cal01 = (Calendar) cal01.clone();
do {
//得到当年实际天数
days += cal01.getActualMaximum(Calendar.DAY_OF_YEAR);
cal01.add(Calendar.YEAR,1);
}while (cal01.get(Calendar.YEAR) != year2);
}
return days;
}
计算一天剩余天数
/**
* 获取截至当天的时间差 单位秒
* @return
*/
public static int getStartTimeOfDay(String timeZone){
//时区
String tz = StringUtils.isEmpty(timeZone)?"GMT+8":timeZone;
TimeZone curTz = TimeZone.getTimeZone(tz);
Calendar calendar = Calendar.getInstance(curTz);
Date date = new Date();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH,1);
calendar.set(Calendar.HOUR_OF_DAY,0);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND,0);
calendar.set(Calendar.MILLISECOND,0);
return (int) ((calendar.getTime().getTime() - date.getTime())/1000);
}
/**
* 基于jdk8 获取当天剩余时间 单位秒
* @param currentDate
* @return
*/
public static int getEndTimeOfDay(Date currentDate){
LocalDateTime night = LocalDateTime.ofInstant(currentDate.toInstant(),
ZoneId.systemDefault()).plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
LocalDateTime curr = LocalDateTime.ofInstant(currentDate.toInstant(),ZoneId.systemDefault());
return (int) ChronoUnit.SECONDS.between(curr,night);
}