
目录
时间类
1.1 获取当前时间,以特定格式化形式输出
1.2 自定义时间,以特定格式化输出
1.3 获取当前时间,自定义格式化
1.4 自定义时间,自定义格式化
设备类
根据请求头信息,获取用户发起请求的设备
请求IP类
根据请求头信息,获取用户发起请求的IP地址
文件容量类
在使用IO流时,有需要对文件大小格式化的场景
时间类
1.1 获取当前时间,以特定格式化形式输出
public static String getTime(){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String newDateFormat = dateFormat.format(new Date());
public static void main(String[] args) {
System.out.println(getTime());

1.2 自定义时间,以特定格式化输出
public static String getTime2(String time) {
String formattedTime =null;
date = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US).parse(time);
formattedTime = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss",Locale.CHINA).format(date);
formattedTime = "错误的时间格式";
public static void main(String[] args) {
System.out.println(getTime2("Thu Mar 14 16:24:28 CST 2024"));

1.3 获取当前时间,自定义格式化
public static String getTime(String toFormat){
SimpleDateFormat dateFormat = new SimpleDateFormat(toFormat);
String newDateFormat = dateFormat.format(date);
public static void main(String[] args) {
System.out.println(getTime("yyyy-MM-dd"));

1.4 自定义时间,自定义格式化
public static String getTime2(String time,String toFormat){
String formattedTime =null;
date = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US).parse(time);
formattedTime = new SimpleDateFormat(toFormat,Locale.CHINA).format(date);
formattedTime = "错误的时间格式";
public static void main(String[] args) {
System.out.println(getTime2("Thu Mar 14 16:00:30 CST 2024","yyyy/MM/dd HH:mm:ss"));

设备类
根据请求头信息,获取用户发起请求的设备;
public static String getClientDevice(HttpServletRequest request) {
String userAgent = request.getHeader("User-Agent");
if (userAgent.contains("Windows")) {
} else if (userAgent.contains("Mac")) {
} else if (userAgent.contains("Linux")) {
} else if (userAgent.contains("Android")) {
} else if (userAgent.contains("iPhone") || userAgent.contains("iPad"))
请求IP类
根据请求头信息,获取用户发起请求的IP地址;
public static String getClientIP(HttpServletRequest request) {
String ipAddress = request.getHeader("X-Forwarded-For");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_CLIENT_IP");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
文件容量类
在使用IO流时,有需要对文件大小格式化的场景;
private static String formatFileSize(long size){
}else if(size < 1024* 1024){
return String.format("%.2f KB",(double) size/ 1024);
}else if(size <1024 * 1024 *1024){
return String.format("%.2f MB",(double) size / 1024 /1024);
return String.format("%.2f GB",(double) size /1024 /1024 /1024);