網際網路金融專案——工作日誌(三)之工具類

ZeroWM發表於2016-06-26

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());

相關文章