时间的加减
public Date setTime(Date time1) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(time1); //time1为初始时间
calendar.add(Calendar.DAY_OF_MONTH, 1);//在初始时间加上1天
calendar.add(Calendar.DAY_OF_MONTH, -1);//在初始时间减去1天
calendar.add(Calendar.HOUR, 2);//在初始时间加上2小时
calendar.add(Calendar.HOUR, -2);//在初始时间减去2小时
calendar.add(Calendar.MINUTE, 3);//在初始时间加上3分钟
calendar.add(Calendar.SECOND, 4);//在初始时间加上4秒
calendar.add(Calendar.MILLISECOND, 5);//在初始时间加上5毫秒
Date time2 = calendar.getTime();
return time2; //time2为变更后的最终时间
}
两个时间点间的时间差
mysql实现:
SELECT TIMESTAMPDIFF(MINUTE,start_time,end_time);
SECOND 秒
MINUTE 分钟
HOUR 时间
DAY 天
MONTH 月
YEAR 年
java代码实现:
public String restTime(Date startTime, Date endTime) {
//与当前时间的差值
// long rest = endTime.getTime() - new Date(System.currentTimeMillis()).getTime();
//与固定时间的差值
long rest = endTime.getTime() - startTime.getTime();
long d = rest / (24 * 60 * 60 * 1000);
long h = (rest / (60 * 60 * 1000) - d * 24);
long m = ((rest / (60 * 1000)) - d * 24 * 60 - h * 60);
d = d > 0 ? d : -d;
h = h > 0 ? h : -h;
m = m > 0 ? m : -m;
String a = "相差" + d + "天" + h + "小时" + m + "分";
return a;
}
两个时间点间的日期集合 (注释部分不包含begin和end)
private static List<String> getBetweenDates(String begin, String end) {
try {
List<String> result = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Calendar tempStart = Calendar.getInstance();
Date beginDate = sdf.parse(begin);
Date endDate = sdf.parse(end);
tempStart.setTime(beginDate);
// Calendar tempEnd = Calendar.getInstance();
// tempStart.add(Calendar.DAY_OF_YEAR, 1);
// tempEnd.setTime(endDate);
// while (tempStart.before(tempEnd)) {
// result.add(sdf.format(tempStart.getTime()));
// tempStart.add(Calendar.DAY_OF_YEAR, 1);
// }
while (beginDate.getTime() <= endDate.getTime()) {
result.add(sdf.format(tempStart.getTime()));
tempStart.add(Calendar.DAY_OF_YEAR, 1);
beginDate = tempStart.getTime();
}
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
两个时间点间的月份集合
public static List<Integer> getMonthBetween(String minDate, String maxDate) {
try {
ArrayList<Integer> result = new ArrayList<Integer>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
// min.setTime(minDate);
min.setTime(sdf.parse(minDate));
min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
// max.setTime(maxDate);
max.setTime(sdf.parse(maxDate));
max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
Calendar curr = min;
while (curr.before(max)) {
result.add(Integer.valueOf(sdf.format(curr.getTime())));
curr.add(Calendar.MONTH, 1);
}
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
获取当月所有日期的集合
public static List<String> getDateOfTheMonth(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<String> list = new ArrayList<>();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DATE, 1);
int month = cal.get(Calendar.MONTH);
while (cal.get(Calendar.MONTH) == month) {
list.add(sdf.format(cal.getTime()));
cal.add(Calendar.DATE, 1);
}
return list;
}