java中日期处理的一些工具方法_java


大家好,我是雄雄,欢迎关注微信公众号:雄雄的小课堂


前言

现在是2022年4月16日15:35:14!忙里偷闲,直接来看方法吧,写完文章继续去改Bug:

1.计算两个日期之间相差的天数

/**
* @param stratTime 开始时间
* @param endTime 结束时间
* @return 计算两个日期之间相差的天数
*/
public static Map<String, Object> dateDiff(Long stratTime, Long endTime) {
Map<String, Object> map = new HashMap<>();
Long diff = endTime - stratTime;
Long day = diff / (24 * 60 * 60 * 1000);
Long hour = (diff / (60 * 60 * 1000) - day * 24);
Long min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
Long sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
map.put("day", day);
map.put("hour", hour);
map.put("min", min);
map.put("sec", sec);
return map;
}

2.将时间戳转换成时间

/**
* 将时间戳转换成时间
*/
public static Map<String, Object> formatTime(Long time) {
Map<String, Object> map = new HashMap<>();
Long day = time / (24 * 60 * 60 * 1000);
Long hour = (time / (60 * 60 * 1000) - day * 24);
Long min = ((time / (60 * 1000)) - day * 24 * 60 - hour * 60);
Long sec = (time / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
map.put("day", day);
map.put("hour", hour);
map.put("min", min);
map.put("sec", sec);
return map;
}

3. 获取当前24小时的时间前后时间戳

/**
* 将时间戳转换成时间
*/
public static Map<String, Object> getTimeChuo() {
Map<String, Object> map = new HashMap<>();
Long nowTime = System.currentTimeMillis();
Long startTime = nowTime - 24 * 60 * 60 * 1000;
Long endTime = nowTime + 24 * 60 * 60 * 1000;
map.put("startTime", startTime);
map.put("endTime", endTime);
return map;
}

4.由出生日期获得年龄

public static int getAge(Date birthDay) throws Exception {
Calendar cal = Calendar.getInstance();
if (cal.before(birthDay)) {
throw new IllegalArgumentException("出生日期小于当前时间,无效的日期!");
}
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH);
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
cal.setTime(birthDay);

int yearBirth = cal.get(Calendar.YEAR);
int monthBirth = cal.get(Calendar.MONTH);
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

int age = yearNow - yearBirth;

if (monthNow <= monthBirth) {
if (monthNow == monthBirth) {
if (dayOfMonthNow < dayOfMonthBirth) age--;
} else {
age--;
}
}
return age;
}

5.统计两个日期之间的所有日期

/**
* 统计两个日期之间的所有日期
*/
public static List<String> getBeginTimeAndEndTime(Date beginTime, Date endTime) {
List<String> listDate = new ArrayList<>();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Calendar calendar = Calendar.getInstance();
calendar.setTime(beginTime);
while (calendar.getTime().before(endTime) || calendar.getTime().equals(endTime)) {
listDate.add(dateFormat.format(calendar.getTime()));
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
return listDate;
} catch (Exception e) {
e.printStackTrace();
}
return listDate;
}

6.根据生日获取年龄的另一种方法

/**
* 根据日期获取年龄
*/
public static int getAgeByDate(Date date) throws Exception {
Calendar cal = Calendar.getInstance();
int thisYear = cal.get(Calendar.YEAR);
int thisMonth = cal.get(Calendar.MONTH);
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
cal.setTime(date);
int birthYear = cal.get(Calendar.YEAR);
int birthMonth = cal.get(Calendar.MONTH);
int birthdayOfMonth = cal.get(Calendar.DAY_OF_MONTH);

int age = thisYear - birthYear;

// 未足月
if (thisMonth <= birthMonth) {
// 当月
if (thisMonth == birthMonth) {
// 未足日
if (dayOfMonth < birthdayOfMonth) {
age--;
}
} else {
age--;
}
}
return age;
}