引包
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 计算时间间隔
* @param start
* @param end
* @param format "yyyy-MM-dd HH:mm:ss"
* @param type 间隔类型(d、H、m、s)
* @return String
*/
public static Long timeInterval(String start,String end,String format,String type)throws ParseException{
SimpleDateFormat df = new SimpleDateFormat(format);
long startL =df.parse(start).getTime();
long endL =df.parse(end).getTime();
int num = 1;
switch (type){
case "d":
num = 1000*60*60*24;
break;
case "H":
num = 1000*60*60;
break;
case "m":
num = 1000*60;
break;
case "s":
num = 1000;
break;
}
return (endL - startL) / num;
}
/**
* 获取上n个小时整点小时时间
* @param date
* @return String
*/
public static String getLastHourTime(Date date, int n,String type){
Calendar ca = Calendar.getInstance();
ca.set(Calendar.MINUTE, 0);
ca.set(Calendar.SECOND, 0);
SimpleDateFormat sdf = new SimpleDateFormat(type);
ca.set(Calendar.HOUR_OF_DAY, ca.get(Calendar.HOUR_OF_DAY)-n);
date = ca.getTime();
return sdf.format(date);
}
/**
* 获取当前时间的整点小时时间
* @param date
* @return String
*/
public static String getCurrHourTime(Date date,String type){
Calendar ca = Calendar.getInstance();
ca.set(Calendar.MINUTE, 0);
ca.set(Calendar.SECOND, 0);
date = ca.getTime();
SimpleDateFormat sdf = new SimpleDateFormat(type);
return sdf.format(date);
}
/**
* 获取当前时间的整点小时时间
* @param sdate
* @return String
*/
public static String getCurrHourTimeString(String sdate,String type){
try {
SimpleDateFormat format = new SimpleDateFormat(type);
Calendar ca = Calendar.getInstance();
ca.set(Calendar.MINUTE, 0);
ca.set(Calendar.SECOND, 0);
Date strtodate = format.parse(sdate);
strtodate = ca.getTime();
return format.format(strtodate);
}catch (Exception e){
return "";
}
}
/**
* 获取上n个小时整点小时时间
* @param sdate
* @return String
*/
public static String getLastHourTimeString(String sdate, int n,String type){
try {
SimpleDateFormat sdf = new SimpleDateFormat(type);
Date date=sdf.parse(sdate);
Calendar ca = Calendar.getInstance();
ca.set(Calendar.MINUTE, 0);
ca.set(Calendar.SECOND, 0);
ca.set(Calendar.HOUR_OF_DAY, ca.get(Calendar.HOUR_OF_DAY)-n);
date = ca.getTime();
return sdf.format(date);
}catch (Exception e){
return "";
}
}
/**
* 根据指定格式,获取当前时间
* @param format "yyyy-MM-dd HH:mm:ss"
* @return String
*/
public static String getNowDateByType(String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date());
}
/**
* 根据指定格式,格式化指定时间
* @param format "yyyy-MM-dd HH:mm:ss"
* @param date
* @return String
*/
public static String formatDate(String format,Date date){
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
/**
* 计算2个日期相差多少年
* @param fromDate
* @param toDate
* @return String
*/
public static String yearCompare(Date fromDate,Date toDate){
DayCompare result = dayComparePrecise(fromDate, toDate);
double month = result.getMonth();
double year = result.getYear();
//返回2位小数,并且四舍五入
DecimalFormat df = new DecimalFormat("######0.0");
return df.format(year + month / 12);
}
/**
* 在指定时间基础上增加时间
* @param oldTime
* @param format "yyyy-MM-dd HH:mm:ss"
* @param add(毫秒数)
* @return String
* @throws ParseException
*/
public static String TimeAdd(String oldTime,String format ,int add) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat(type);
ParsePosition pos = new ParsePosition(0);
Date createtime = sdf.parse(oldTime,pos);
createtime.setTime(createtime.getTime()+add);
return sdf.format(createtime);
}
未完,待续………………