当前时间往后多久
public class TestDate {
public static void main(String[] args) {
// 根据有效天数获取失效日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
Timestamp nowDate = new Timestamp(System.currentTimeMillis());
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(nowDate);
rightNow.add(Calendar.DAY_OF_MONTH, 10);// 当前时间往后推迟10天
Date dt = rightNow.getTime();
String reStr = sdf.format(dt);
System.out.println(reStr);
// 活动有效时间
Calendar rightNow2 = Calendar.getInstance();
rightNow2.setTime(nowDate);
rightNow2.add(Calendar.MONTH, 1); // 当前时间往后推一个月
Date dt2 = rightNow2.getTime();
String reStr2 = sdf.format(dt2);
System.out.println(reStr2);
}
}
时间差
1 /**
2 * 获取时间差
3 * @param time1 time2 需要计算的时间
4 * @return
5 */
6 public static String getTimeDeffer(String time1,String time2)
7 {
8 String result = "";
9 if(StringUtils.isNotBlank(time1)&&StringUtils.isNotBlank(time2)){
10 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
11 try {
12 long dt1 = sdf.parse( time1 ).getTime();
13 long dt2 = sdf.parse( time2 ).getTime();
14 long dc = Math.abs(dt2-dt1);
16 long seconds = dc/1000;
17 long date = seconds/(24*60*60); //相差的天数
18 long hour = (seconds-date*24*60*60)/(60*60);//相差的小时数
19 long minut = (seconds-date*24*60*60-hour*60*60)/(60);//相差的分钟数
20 long second = (seconds-date*24*60*60-hour*60*60-minut*60);//相差的秒数
21 return (date==0?"":(date+"天"))+(hour==0?"":(hour+"小时"))+(minut==0?"":(minut+"分"))+(second==0?"":(second+"秒"));
22 } catch (ParseException e) {
23 e.printStackTrace();
24 }
25 }
26 return result;
27 }