9.工具类
日期差:
privateint daysBetween(Date startDate, Date endDate) {
SimpleDateFormatsdf = new SimpleDateFormat("yyyy-MM-dd");
try {
startDate= sdf.parse(sdf.format(startDate));
endDate= sdf.parse(sdf.format(endDate));
}catch (Exception e) {
e.printStackTrace();
}
Calendarcal = Calendar.getInstance();
cal.setTime(startDate);
longtime1 = cal.getTimeInMillis();
cal.setTime(endDate);
longtime2 = cal.getTimeInMillis();
longbetween_days = (time2 - time1) / (1000 * 3600 * 24);
returnInteger.parseInt(String.valueOf(between_days));
}
获取本地IP:
Stringip = IpUtil.getIpAddress(request);//获得本机IP
/**
*获取用户真实IP地址,不使用request.getRemoteAddr();的原因是有可能用户使用了代理软件方式避免真实IP地址,
*
*可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值,究竟哪个才是真正的用户端的真实IP呢?
* 答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。
*
* 如:X-Forwarded-For:192.168.1.110,192.168.1.120, 192.168.1.130,
* 192.168.1.100
*
* 用户真实IP为: 192.168.1.110
*
* @param request
* @return
*/
public static StringgetIpAddress(HttpServletRequest request) {
String ip =request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 ||"unknown".equalsIgnoreCase(ip)) {
ip =request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 ||"unknown".equalsIgnoreCase(ip)) {
ip =request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 ||"unknown".equalsIgnoreCase(ip)) {
ip =request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 ||"unknown".equalsIgnoreCase(ip)) {
ip =request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 ||"unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
10.日志
概念:
日志是应用软件中不可缺少的部分,Apache的开源项目Log4j是一个功能强大的日志组件,提供方便的日志记录
应用:
privatestatic final Log logger = LogFactory.getLog(AccountSetController.class);
logger.info(e.getMessage());