• 【方法封装】时间格式化输出,获取请求设备和IP


    目录

    时间类

    1.1 获取当前时间,以特定格式化形式输出

    1.2 自定义时间,以特定格式化输出

    1.3 获取当前时间,自定义格式化

    1.4 自定义时间,自定义格式化

    设备类

    根据请求头信息,获取用户发起请求的设备

     请求IP类

    根据请求头信息,获取用户发起请求的IP地址

    文件容量类

    在使用IO流时,有需要对文件大小格式化的场景


    时间类

    1.1 获取当前时间,以特定格式化形式输出

    1. /**
    2. *
    3. * @return 无参的获取当前时间
    4. */
    5. public static String getTime(){
    6. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    7. String newDateFormat = dateFormat.format(new Date());
    8. return newDateFormat;
    9. }
    • 测试案例
    1. public static void main(String[] args) {
    2. //获取当前时间,以特定格式化形式输出
    3. System.out.println(getTime());
    4. }
    • 测试结果


    1.2 自定义时间,以特定格式化输出

    1. /**
    2. *
    3. * @param time 需要格式化的时间
    4. * @return 以“yyyy年MM月dd日 HH:mm:ss”格式化后的时间
    5. */
    6. public static String getTime2(String time) {
    7. Date date =null;
    8. String formattedTime =null;
    9. try{
    10. date = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US).parse(time);
    11. formattedTime = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss",Locale.CHINA).format(date);
    12. return formattedTime;
    13. }catch (Exception e) {
    14. formattedTime = "错误的时间格式";
    15. }
    16. return formattedTime;
    17. }
    • 测试案例
    1. public static void main(String[] args) {
    2. //自定义时间,以特定格式化输出
    3. System.out.println(getTime2("Thu Mar 14 16:24:28 CST 2024"));
    4. }
    • 测试结果


    1.3 获取当前时间,自定义格式化

    1. /**
    2. * 无参的获取当前时间
    3. * @param toFormat 需要格式化的时间类型
    4. * @return 格式化后的时间
    5. */
    6. public static String getTime(String toFormat){
    7. Date date = new Date();
    8. SimpleDateFormat dateFormat = new SimpleDateFormat(toFormat);
    9. String newDateFormat = dateFormat.format(date);
    10. return newDateFormat;
    11. }
    • 测试案例
    1. public static void main(String[] args) {
    2. //获取当前时间,自定义格式化
    3. System.out.println(getTime("yyyy-MM-dd"));
    4. }
    • 测试结果


    1.4 自定义时间,自定义格式化

    1. /**
    2. *
    3. * @param time 需要格式化的时间
    4. * @param toFormat 需要格式化的类型
    5. * @return 格式化后的时间
    6. */
    7. public static String getTime2(String time,String toFormat){
    8. Date date =null;
    9. String formattedTime =null;
    10. try{
    11. date = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US).parse(time);
    12. formattedTime = new SimpleDateFormat(toFormat,Locale.CHINA).format(date);
    13. return formattedTime;
    14. }catch (Exception e) {
    15. formattedTime = "错误的时间格式";
    16. }
    17. return formattedTime;
    18. }
    • 测试案例
    1. public static void main(String[] args) {
    2. //自定义时间,自定义格式化
    3. System.out.println(getTime2("Thu Mar 14 16:00:30 CST 2024","yyyy/MM/dd HH:mm:ss"));
    4. }
    • 测试结果


    设备类

    根据请求头信息,获取用户发起请求的设备;

    1. public static String getClientDevice(HttpServletRequest request) {
    2. String userAgent = request.getHeader("User-Agent");
    3. if (userAgent.contains("Windows")) {
    4. return "Windows PC";
    5. } else if (userAgent.contains("Mac")) {
    6. return "Mac";
    7. } else if (userAgent.contains("Linux")) {
    8. return "Linux PC";
    9. } else if (userAgent.contains("Android")) {
    10. return "Android";
    11. } else if (userAgent.contains("iPhone") || userAgent.contains("iPad"))
    12. {
    13. return "iOS";
    14. } else {
    15. return "Other";
    16. }
    17. }

     请求IP类

    根据请求头信息,获取用户发起请求的IP地址;

    1. public static String getClientIP(HttpServletRequest request) {
    2. String ipAddress = request.getHeader("X-Forwarded-For");
    3. if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
    4. ipAddress = request.getHeader("Proxy-Client-IP");
    5. }
    6. if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
    7. ipAddress = request.getHeader("WL-Proxy-Client-IP");
    8. }
    9. if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
    10. ipAddress = request.getHeader("HTTP_CLIENT_IP");
    11. }
    12. if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
    13. ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
    14. }
    15. if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
    16. ipAddress = request.getRemoteAddr();
    17. }
    18. return ipAddress;
    19. }

    文件容量类

    在使用IO流时,有需要对文件大小格式化的场景;

    1. private static String formatFileSize(long size){
    2. if(size < 1024){
    3. //如果获取到的文件大小不足1KB,则输出的单位为B
    4. return size +"B";
    5. }else if(size < 1024* 1024){
    6. //如果文件大小不足1MB,则输出的单位为KB
    7. return String.format("%.2f KB",(double) size/ 1024);
    8. }else if(size <1024 * 1024 *1024){
    9. //如果文件大小不足1GB,则输出的单位为MB
    10. return String.format("%.2f MB",(double) size / 1024 /1024);
    11. }else{
    12. //如果文件大小不足1TB,则输出的单位为GB
    13. return String.format("%.2f GB",(double) size /1024 /1024 /1024);
    14. }
    15. //TB单位在日常获取文件大小时使用频率较少,故没有纳入计算
    16. }

  • 相关阅读:
    「零基础从零开始写VO视觉里程计」G2O基础知识讲解(7-5)
    数据结构-Redis(一)
    【精品】JavaScript中获取URL中参数值的方法汇总
    docker清理Overlay2占用磁盘空间
    判断sparse matrix是否是对称矩阵
    含文档+PPT+源码等]精品基于Java的社区团购系统SSM[包运行成功]Java毕业设计SSM项目源码
    定制化精准推送与用户分组策略:数智营销的硬技能
    【Linux】多路IO复用技术②——poll详解&如何使用poll模型实现简易的一对多服务器(附图解与代码实现)
    老司机带你用python从另外一个角度看市场需求~
    计算机网络——计算机网络体系结构
  • 原文地址:https://blog.csdn.net/LikewaterwithLi/article/details/136716857