• java开发常用工具类


      1 package com.rui.util;
      2 
      3 import java.text.DateFormat;
      4 import java.text.DecimalFormat;
      5 import java.text.SimpleDateFormat;
      6 import java.util.Date;
      7 import java.util.Random;
      8 
      9 /**
     10  * 
     11  * @ClassName: StrUtils
     12  * @Description: 工具类
     13  * @author poseidon
     14  * @date 2015年10月23日 下午8:13:45
     15  * @version V1.0.0
     16  */
     17 public class StrUtils {
     18 
     19 
     20     /**
     21      * 
     22      * @Title: isEmpty
     23      * @Description: 空判断 
     24      * @param content
     25      * @return boolean
     26      */
     27     public static boolean isEmpty(String content){
     28         return (content==null || content.equals(""))?true:false;
     29     }
     30     
     31     /**
     32      * 
     33      * @Title: isNotEmpty
     34      * @Description: 非空判断
     35      * @param content
     36      * @return boolean
     37      */
     38     public static boolean isNotEmpty(String content){
     39         return !isEmpty(content);
     40     }
     41     
     42     
     43     /**
     44      * 
     45      * @Title: formatDate
     46      * @Description: 格式化日期类
     47      * @param date
     48      * @param pattern
     49      * @return String
     50      */
     51     public static String formatDate(Date date,String pattern){
     52         if(date!=null){
     53             String dateString = new SimpleDateFormat(pattern).format(date);
     54             return dateString;
     55         }else{
     56             return "";
     57         }
     58     }
     59     
     60     /**
     61      * 
     62      * @Title: getExt
     63      * @Description: 获取文件的后缀
     64      * @param name 文件名称
     65      * @param flag true有点false没点
     66      * @return String
     67      */
     68     public static String getExt(String name,boolean flag){
     69         if(isNotEmpty(name)){
     70             String ext  = null;
     71             if(flag){
     72                 ext = name.substring(name.lastIndexOf("."), name.length());
     73             }else{
     74                 ext = name.substring(name.lastIndexOf(".")+1, name.length());
     75             }
     76             return ext;
     77         }else{
     78             return "";
     79         }
     80     }
     81     
     82     /**
     83      * 
     84      * @Title: generateFileName
     85      * @Description: 为上传文件自动分配文件名称,避免重复
     86      * @param fileName
     87      * @param randomNum
     88      * @param dataPattern
     89      * @return String
     90      */
     91     public static String generateFileName(String fileName,int randomNum,String dataPattern) {
     92         // 获得当前时间
     93         DateFormat format = new SimpleDateFormat(dataPattern);
     94         // 转换为字符串
     95         String formatDate = format.format(new Date());
     96         // 随机生成文件编号
     97         int random = new Random().nextInt(randomNum);
     98         // 获得文件后缀名称
     99         int position = fileName.lastIndexOf(".");
    100         String extension = fileName.substring(position);
    101         // 组成一个新的文件名称
    102         return formatDate + random + extension;
    103     }
    104     
    105     
    106     /**
    107      * 
    108      * @Title: countFileSize
    109      * @Description: 根据File文件的长度统计文件的大小
    110      * @param fileSize
    111      * @return String
    112      */
    113     public static String countFileSize(long fileSize) {
    114         String fileSizeString = "";
    115         try {
    116             DecimalFormat df = new DecimalFormat("#.00");
    117             long fileS = fileSize;
    118             if (fileS == 0) {
    119                 fileSizeString = "0KB";
    120             } else if (fileS < 1024) {
    121                 fileSizeString = df.format((double) fileS) + "B";
    122             } else if (fileS < 1048576) {
    123                 fileSizeString = df.format((double) fileS / 1024) + "KB";
    124             } else if (fileS < 1073741824) {
    125                 fileSizeString = df
    126                         .format(((double) fileS / 1024 / 1024) - 0.01)
    127                         + "MB";
    128             } else {
    129                 fileSizeString = df.format((double) fileS / 1024 / 1024 / 1024)
    130                         + "G";
    131             }
    132         } catch (Exception e) {
    133             e.printStackTrace();
    134         }
    135         return fileSizeString;
    136     }
    137     
    138     /**
    139      * 
    140      * @Title: conversionSpecialCharacters
    141      * @Description: 把两个反斜线转换成正斜线
    142      * @param string
    143      * @return String
    144      */
    145     public static String conversionSpecialCharacters(String string) {
    146         return string.replaceAll("\\", "/");
    147     }
    148 
    149     public static void main(String[] args) {
    150         
    151         
    152     }
    153 }
  • 相关阅读:
    性能测试目的
    什么是 JavaConfig?
    Spring Boot 有哪些优点?
    .什么是 Spring Boot?
    简述什么是静态测试、动态测试、黑盒测试、白盒测试、α测试 β测试
    mvc不知道参数名,获取所有参数和值
    18.核心
    Kotlin基础-集合类型 数组
    list可以存放的数据大小
    SpringBoot项目集成Flyway配置执行顺序问题
  • 原文地址:https://www.cnblogs.com/sun-rain/p/4905597.html
Copyright © 2020-2023  润新知