java常用util工具类
时间
package com.lcdzzz.utils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TimeUtils {
//获取当前年份
public static int getCurrentYear() {
return Calendar.getInstance().get(Calendar.YEAR);
}
//获取当前月份
public static int getCurrentMonth() {
return Calendar.getInstance().get(Calendar.MONTH) + 1;
}
//获取年月日
public static String getYMD() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateNowStr = sdf.format(new Date());
System.out.println("格式化后的日期:" + dateNowStr);
return dateNowStr;
}
/*
* 获取当月第一天的时间
* */
public static Date getFisrtDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
//设置年份
cal.set(Calendar.YEAR, year);
//设置月份
cal.set(Calendar.MONTH, month - 1);
//获取某月最小天数
int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
//设置日历中月份的最小天数
cal.set(Calendar.DAY_OF_MONTH, firstDay);
//格式化日期
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// String firstDayOfMonth = sdf.format(cal.getTime());
return cal.getTime();
}
/*
* 获取当月最后一天的时间
* */
public static Date getLastDayOfMonth(int year, int month) {
Calendar cal = Calendar.getInstance();
//设置年份
cal.set(Calendar.YEAR, year);
//设置月份
cal.set(Calendar.MONTH, month - 1);
//获取某月最大天数
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//设置日历中月份的最大天数
cal.set(Calendar.DAY_OF_MONTH, lastDay);
//格式化日期
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// String lastDayOfMonth = sdf.format(cal.getTime());
return cal.getTime();
}
/*
* date转string
* */
public static String dateToString(Date value) {
Date sqlDate = new java.sql.Date(value.getTime());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter.format(sqlDate);
}
}
ip工具类
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IpUtils {
/**
* 获取客户端真实 IP 地址
*
* @param request HttpServletRequest 对象
* @return 客户端 IP 地址
*/
public static String getClientIp(HttpServletRequest request) {
String ipAddress = request.getHeader("X-Forwarded-For");
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
}
// 多个代理服务器的情况,取第一个非unknown的IP地址
String[] ipArray = ipAddress.split(",");
for (String ip : ipArray) {
if (!"unknown".equalsIgnoreCase(ip)) {
ipAddress = ip.trim();
break;
}
}
return ipAddress;
}
/**
* 判断是否为内部 IP 地址
*
* @param ipAddress IP 地址字符串
* @return 是否为内部 IP 地址
*/
public static boolean isInternalIp(String ipAddress) {
if (ipAddress == null || ipAddress.isEmpty()) {
return false;
}
try {
InetAddress inetAddress = InetAddress.getByName(ipAddress);
byte[] addressBytes = inetAddress.getAddress();
// 10.0.0.0/8
if (addressBytes[0] == 10) {
return true;
}
// 172.16.0.0/12
if (addressBytes[0] == 172 && (addressBytes[1] >= 16 && addressBytes[1] <= 31)) {
return true;
}
// 192.168.0.0/16
if (addressBytes[0] == 192 && addressBytes[1] == 168) {
return true;
}
} catch (UnknownHostException e) {
// 处理异常
}
return false;
}
/**
* 验证 IP 地址格式是否正确
*
* @param ipAddress IP 地址字符串
* @return 是否为有效的 IP 地址
*/
public static boolean isValidIpAddress(String ipAddress) {
try {
InetAddress.getByName(ipAddress);
return true;
} catch (UnknownHostException e) {
return false;
}
}
// 可以添加更多的方法,例如获取本机 IP 地址、计算 IP 地址之间的距离等
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 lcdzzz的博客!