在java7中使用Calendar类进行时间差计算,测试各种情况均正确,但是SimpleDateFormat线程不安全,需要用线程创建SimpleDateFormat对象可以放心使用
static SimpleDateFormat threadsaft(){
ThreadLocal<SimpleDateFormat> local = new ThreadLocal<SimpleDateFormat>();
SimpleDateFormat sdf = local.get();
if (sdf == null){
sdf = new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA)
}
return sdf;
}
//计算时间
static ArrayList<Integer> Computertime(String time){
try {Calendar from = Calendar.getInstance();
ArrayList<Integer> list = new ArrayList<>();
// String starttime = "2021-12-22";
// String endtime = "2022-01-12";
String starttime = time;
//开始时间 2020-03-12 结束时间 2022-02-11
SimpleDateFormat simpleDateFormat = threadsaft()
// String endtime = simpleDateFormat.format(new Date());
from.setTime(simpleDateFormat.parse(time));
Calendar to = Calendar.getInstance();
// to.setTime(new Date());
Date tt = simpleDateFormat.parse(levelCreateTime)
to.setTime(tt);
/*结束时间的上个月份*/
Calendar perviousMonth = Calendar.getInstance();
perviousMonth.setTime(tt);
perviousMonth.add(Calendar.MONTH, -1);
perviousMonth.set(Calendar.DAY_OF_MONTH, perviousMonth.getActualMaximum(Calendar.DAY_OF_MONTH));
// System.out.println("1111111111"+simpleDateFormat.format(perviousMonth.getTime()));
int fromYear = from.get(Calendar.YEAR);
int fromMonth = from.get(Calendar.MONTH);
int fromDay = from.get(Calendar.DAY_OF_MONTH);
System.out.println("fromDay"+fromDay);
int toYear = to.get(Calendar.YEAR);
int toMonth = to.get(Calendar.MONTH);
//结束时间的最后一天,以2021-03-11为例 toMonth为11
int toDay = to.get(Calendar.DAY_OF_MONTH);
System.out.println("toDay"+toDay);
// int year = toYear - fromYear;
// int month = toMonth - fromMonth;
// int day = toDay - fromDay;
int year = 0;
int month = 0;
int day = 0;
//如果开始时间和结束时间是同一年
if (String.valueOf(toYear).equals(String.valueOf(fromYear))){
year = 0;
//如果开始时间和结束时间是同一年同一月
if (toMonth == fromMonth){
if (toDay >= fromDay){
month = 0;
day = toDay-fromDay;
}else {
System.out.println("结束时间不能小于开始时间");
}
} else{
//结束时间的天是结束时间月最后一天,开始时间的天是开始时间的月的第一天
if (toDay == to.getActualMaximum(Calendar.DAY_OF_MONTH) && fromDay == from.getActualMinimum(Calendar.DAY_OF_MONTH) && (toMonth -fromMonth) ==1){
month = toMonth - fromMonth + 1;
day = 0;
} else {
//如果开始时间和结束时间是同一年但不是同一月(因为结束时间一定大于开始时间所以结束时间的月大于开始时间的月)
//结束时间的日小于开始时间的日
if (toDay < fromDay){
month = toMonth - fromMonth -1;
/*@perviousMonth:结束时间的上个月份*/
day = perviousMonth.getActualMaximum(Calendar.DAY_OF_MONTH) - fromDay + toDay;
System.out.println("月月:"+month);
System.out.println("天天:"+day);
}
if (toDay == fromDay){
//结束时间的日等于开始时间的日
month = toMonth - fromMonth;
day = 0;
}
if (toDay > fromDay){
// day = from.getActualMaximum(Calendar.DAY_OF_MONTH) - fromDay + toDay + 1;
month = toMonth - fromMonth;
day = toDay - fromDay;
}
}
}
}
//开始时间和结束时间不是同一年
if (!String.valueOf(toYear).equals(String.valueOf(fromYear))){
//结束时间的月小于开始时间的月,或(结束时间的月等于开始时间的月,结束时间的天小于开始时间的天)
//year = toYear - fromYear - 1;
if (toMonth < fromMonth){
year = toYear - fromYear - 1;
if (toMonth == fromMonth && toDay == fromDay){
year = toYear - fromYear;
}
if (toMonth < fromMonth){
if (toDay == to.getActualMaximum(Calendar.DAY_OF_MONTH) && fromDay == from.getActualMinimum(Calendar.DAY_OF_MONTH) && (toMonth -fromMonth) ==1){
month = toMonth - fromMonth + 1;
year = toYear - fromYear;
day = 0;
}else {
//因为在Calendar中代表12个月份的数字是从0-11,所以12月用数字“11”表示,最后得出的月份比实际中小一个数,比如得出月份是0月,在现实中其实是1月
month = 11 - fromMonth + toMonth;
if (toDay < fromDay){
month-= 1;
}
//因为在Calendar中代表12个月份的数字是从0-11,最后得出的月份比实际中小一个数,比如得出月份是0月,在现实中其实是1月
//因为我们要显示现实中的月份所以要加一
month+=1;
/*@perviousMonth:结束时间的上个月份*/
if (toDay == fromDay){
day = 0;
}
if (toDay != fromDay){
day = to.getActualMaximum(Calendar.DAY_OF_MONTH);
// - fromDay + toDay;
// 开始时间 2020-03-12 结束时间 2022-02-11
}
}
//
}
if (toMonth == fromMonth){
if (toDay < fromDay){
//Calender中的10代表现实中的11月份
month=10;
//因为我们要显示现实中的月份所以要加一
month+=1;
/*@perviousMonth:结束时间的上个月份*/
day = perviousMonth.getActualMaximum(Calendar.DAY_OF_MONTH) - fromDay + toDay;
}
if (toDay == fromDay){
month=0;
day = 0;
}
}
// day = from.getActualMaximum(Calendar.DAY_OF_MONTH) - fromDay + toDay + 1;
// day = perviousMonth.getActualMaximum(Calendar.DAY_OF_MONTH) - fromDay + toDay;
}
if (toMonth >= fromMonth){
year = toYear - fromYear;
//结束时间的天是结束时间月最后一天,开始时间的天是开始时间的月的第一天
if (toDay == to.getActualMaximum(Calendar.DAY_OF_MONTH) && fromDay == from.getActualMinimum(Calendar.DAY_OF_MONTH) && (toMonth -fromMonth) ==1){
month = toMonth - fromMonth + 1;
day = 0;
} else {
//如果开始时间和结束时间不是同一年不是同一月(因为结束时间一定大于开始时间所以结束时间的月大于开始时间的月)
//结束时间的日小于开始时间的日
if (toDay < fromDay){
if (toMonth == fromMonth){
year-=1;
month = 11;
}else {
month = toMonth - fromMonth - 1;
}
/*@perviousMonth:结束时间的上个月份*/
day = perviousMonth.getActualMaximum(Calendar.DAY_OF_MONTH) - fromDay + toDay;
System.out.println("月月:"+month);
System.out.println("天天:"+day);
}
if (toDay == fromDay){
//结束时间的日等于开始时间的日
month = toMonth - fromMonth;
day = 0;
}
if (toDay > fromDay){
day = toDay - fromDay;
month = toMonth - fromMonth;
}
}
}
}
list.add(year);
list.add(month);
list.add(day);
// System.out.println("开始:"+starttime);
// System.out.println("结束:"+endtime);
System.out.println(year+"年:"+month+"月:"+day+"日:");
return list
} catch (Exception e) {
e.printStackTrace();
}
}
利用时间戳计算
/*
*计算time2减去time1的差值 差值只设置 几天 几个小时 或 几分钟
* 根据差值返回多长之间前或多长时间后
* */
public static String getDistanceTime(long time1, long time2) {
long day = 0;
long hour = 0;
long min = 0;
long sec = 0;
long diff;
if (time1 < time2) {
diff = time2 - time1;
} else {
diff = time1 - time2;
}
day = diff / (24 * 60 * 60 * 1000);
hour = (diff / (60 * 60 * 1000) - day * 24);
min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
if (day != 0) return day + "天"+hour + "小时"+min + "分钟" + sec + "秒";
if (hour != 0) return hour + "小时"+ min + "分钟" + sec + "秒";
if (min != 0) return min + "分钟" + sec + "秒";
if (sec != 0) return sec + "秒" ;
return "0秒";
}
下面是在Java8中使用 LocalDate和线程安全的DateTimeFormat来实现计算时间差,但是不对有些情况会计算错误需要改进
//计算时间
static ArrayList<Integer> Computertime(String time){
// String time = one.getFujing_entry_time();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String nowdate = df.format(new Date());// new Date()为获取当前系统时间
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); //这里可以指定接收日期的格式
String term = "";
ArrayList<Integer> list = new ArrayList<>();
try{
// 2019-01-31,2019-04-30
// LocalDate startDate = LocalDate.of(2018, 01, 31);
//
// LocalDate endDate = LocalDate.of(2019, 04, 28);
LocalDate startDate = LocalDate.parse(time, formatter);
LocalDate endDate = LocalDate.parse(nowdate, formatter);
Integer start_year = startDate.getYear();
Integer start_month = startDate.getMonthValue();
Integer start_day = startDate.getDayOfMonth();
Integer end_year = endDate.getYear();
Integer end_month = endDate.getMonthValue();
Integer end_day = endDate.getDayOfMonth();
long y = ChronoUnit.YEARS.between(startDate, endDate); //获取两个日期间隔年
long m = ChronoUnit.MONTHS.between(startDate, endDate); //获取两个日期间隔月
long d = ChronoUnit.DAYS.between(startDate, endDate); //获取两个日期间隔天
Integer lastDayOfEndDate = endDate.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth(); //获取某个月的最后一天
if(start_year.equals(end_year)) {
if(start_day.equals(end_day) || lastDayOfEndDate.equals(end_day)) {
m = end_month - start_month;
}else {
d = endDate.getDayOfMonth(); //获取传入时间对象的Day值
}
}else {
if(m >= 12) {
m = m - y * 12;
}
if(start_day.equals(end_day) || lastDayOfEndDate.equals(end_day)) {
d = 0;
}else {
d = endDate.getDayOfMonth();
}
}
term = (y == 0 ? "" : y + "年" ) + (m == 0 ? "" : + m + "个月" ) + (d == 0 ? "" : d + "天");
list.add((int) y);
list.add((int) m);
list.add((int) d);
} catch (Exception e) {
e.printStackTrace();
}
return list;
}