获得当前时间差

sql

DECLARE param_second BIGINT;

DECLARE param_minute BIGINT;
DECLARE param_hour BIGINT;
DECLARE param_day BIGINT;
DECLARE param_month BIGINT;
DECLARE param_year BIGINT;
DECLARE param_return VARCHAR(10) DEFAULT '刚刚';


SET param_second = UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(param_biggerTime);
SET param_minute = param_second/60;
SET param_hour = param_minute/60;
SET param_day = param_hour/24;
SET param_month = param_day/30;
SET param_year = param_month/12;

IF param_year>0 THEN RETURN CONCAT(param_year,' 年前');
ELSEIF param_month>0 THEN RETURN CONCAT(param_month,' 月前');
ELSEIF param_day>0 THEN RETURN CONCAT(param_day,' 天前');
ELSEIF param_hour>0 THEN RETURN CONCAT(param_hour,' 小时前');
ELSEIF param_minute>0 THEN RETURN CONCAT(param_minute,' 分钟前');
ELSE RETURN param_return;

END IF; 



java

public static String getLastTime(Date biggerTime, Date smallerTime) {
long daterange = biggerTime.getTime() - smallerTime.getTime();
long time = 1000 * 3600 * 24;
long day = daterange / time;
long month = daterange / time / 30;
long year = daterange / time / 30 / 12;
long hour = daterange / 1000 / 60 / 60;
long minute = daterange / 1000 / 60;
while (daterange >= 0) {
if (year > 0) {
String lastime = null;
lastime = year + "年前";
return lastime;
} else if (month > 0) {
String lastime = null;
lastime = month + "个月前";
return lastime;
} else if (day > 0) {
String lastime = null;
lastime = day + "天前";
return lastime;
} else if (hour > 0) {
String lastime = null;
lastime = hour + "小时前";
return lastime;
} else if (minute > 0) {
String lastime = null;
lastime = minute + "分钟前";
return lastime;
} else {
return "刚刚";
}
}
return "刚刚";
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值