wordpress 中文建站,北京网站开发公司哪家好,吸引客人的产品宣传句子,网站建设kaodezhu目录
时间类
1.1 获取当前时间#xff0c;以特定格式化形式输出
1.2 自定义时间#xff0c;以特定格式化输出
1.3 获取当前时间#xff0c;自定义格式化
1.4 自定义时间#xff0c;自定义格式化
设备类
根据请求头信息#xff0c;获取用户发起请求的设备 请求IP类 … 目录
时间类
1.1 获取当前时间以特定格式化形式输出
1.2 自定义时间以特定格式化输出
1.3 获取当前时间自定义格式化
1.4 自定义时间自定义格式化
设备类
根据请求头信息获取用户发起请求的设备 请求IP类
根据请求头信息获取用户发起请求的IP地址
文件容量类
在使用IO流时有需要对文件大小格式化的场景 时间类
1.1 获取当前时间以特定格式化形式输出 /**** return 无参的获取当前时间*/public static String getTime(){SimpleDateFormat dateFormat new SimpleDateFormat(yyyy年MM月dd日 HH:mm:ss);String newDateFormat dateFormat.format(new Date());return newDateFormat;}
测试案例
public static void main(String[] args) {//获取当前时间以特定格式化形式输出System.out.println(getTime());
}
测试结果 1.2 自定义时间以特定格式化输出 /**** param time 需要格式化的时间* return 以“yyyy年MM月dd日 HH:mm:ss”格式化后的时间*/public static String getTime2(String time) {Date date null;String formattedTime null;try{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);return formattedTime;}catch (Exception e) {formattedTime 错误的时间格式;}return formattedTime;}
测试案例 public static void main(String[] args) {//自定义时间以特定格式化输出System.out.println(getTime2(Thu Mar 14 16:24:28 CST 2024));}
测试结果 1.3 获取当前时间自定义格式化 /*** 无参的获取当前时间* param toFormat 需要格式化的时间类型* return 格式化后的时间*/public static String getTime(String toFormat){Date date new Date();SimpleDateFormat dateFormat new SimpleDateFormat(toFormat);String newDateFormat dateFormat.format(date);return newDateFormat;}
测试案例 public static void main(String[] args) {//获取当前时间自定义格式化System.out.println(getTime(yyyy-MM-dd));}
测试结果 1.4 自定义时间自定义格式化 /**** param time 需要格式化的时间* param toFormat 需要格式化的类型* return 格式化后的时间*/public static String getTime2(String time,String toFormat){Date date null;String formattedTime null;try{date new SimpleDateFormat(EEE MMM dd HH:mm:ss zzz yyyy, Locale.US).parse(time);formattedTime new SimpleDateFormat(toFormat,Locale.CHINA).format(date);return formattedTime;}catch (Exception e) {formattedTime 错误的时间格式;}return 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)) {return Windows PC;} else if (userAgent.contains(Mac)) {return Mac;} else if (userAgent.contains(Linux)) {return Linux PC;} else if (userAgent.contains(Android)) {return Android;} else if (userAgent.contains(iPhone) || userAgent.contains(iPad)) {return iOS;} else {return Other;}} 请求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();}return ipAddress;} 文件容量类
在使用IO流时有需要对文件大小格式化的场景
private static String formatFileSize(long size){if(size 1024){//如果获取到的文件大小不足1KB则输出的单位为Breturn size B;}else if(size 1024* 1024){//如果文件大小不足1MB则输出的单位为KBreturn String.format(%.2f KB,(double) size/ 1024);}else if(size 1024 * 1024 *1024){//如果文件大小不足1GB则输出的单位为MBreturn String.format(%.2f MB,(double) size / 1024 /1024);}else{//如果文件大小不足1TB则输出的单位为GBreturn String.format(%.2f GB,(double) size /1024 /1024 /1024);}//TB单位在日常获取文件大小时使用频率较少故没有纳入计算
}