首页 > 其他分享 >工具类

工具类

时间:2022-09-21 09:55:41浏览次数:52  
标签:return String Calendar static new 工具 public

//导出
public void excel(List<Long> ids, HttpServletResponse response) {
 List<StockPageVo> excel = stockMapper.excel(ids);
        try {
            ExportParams params = new ExportParams();
            params.setSheetName("历史入库导出");//设置sheet名
            Workbook workbook = ExcelExportUtil.exportExcel(params, StockPageVo.class, excel);
            //返回头设置下载,并设置文件名,返回
            DownloadUtils.setExportExcelFormat(response, workbook, "历史入库");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 public static void setExportExcelFormat(HttpServletResponse response, Workbook workbook, String fileName) throws Exception {
        response.reset();
        response.setContentType("application/vnd.ms-excel");//下载
        fileName = fileName + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1) + ".xls");
        try (ServletOutputStream outStream = response.getOutputStream()) {
            workbook.write(outStream);
        }
    }

//图片上传
    public static String upload(MultipartFile file) {
        if (file == null) throw new BusinessException("图片为空");
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        String uuid = Utility.generateShortUuid() + "." + extension;
        try {
                          //图片上传的路径
            String path = IntegerFaceConfig.EXCEP_Path;
            file.transferTo(new File(path + uuid));
            return path + uuid;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return throw new BusinessException("图片上传失败");
    }

//图片的删除
        File file123 = new File(ImgUser);//图片的路径
        file123.delete();


//校验id和端口
    public static boolean checkIp(String ipAddress) {
        String ip = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
        Pattern pattern = Pattern.compile(ip);
        Matcher matcher = pattern.matcher(ipAddress);
        return matcher.matches();
    }

    public static boolean checkPort(String port) {
        //端口号验证 1 ~ 65535
        String regex = "^([1-9]|[1-9]\\d{1,3}|[1-6][0-5][0-5][0-3][0-5])$";
        return Pattern.matches(regex, port);
    }


//校验电话号码
    public static boolean isMobileNo(String mobiles) {
        // ^ 匹配输入字符串开始的位置
        // \d 匹配一个或多个数字,其中 \ 要转义,所以是 \\d
        // $ 匹配输入字符串结尾的位置
        String regExp = "^((13[0-9])|(14[5,7,9])|(15[0-3,5-9])|(166)|(17[3,5,6,7,8])" +
                "|(18[0-9])|(19[8,9]))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }



//对参数格式的校验
    public static Boolean isNull(String str) {
        if (str==null) return false;
        boolean blank = StringUtils.isBlank(str);
        if (!blank) {
            return !str.equals("''");
        } else {
            return false;
        }
    }

//获取当前时间
    public static String getTimestamps() {
        Date date = new Date();
        DateTime dateTime = new DateTime();
        String endOfDay = getEndOfDay(date);
        /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
        String formats = sdf.format(endOfDay);*/
        return endOfDay;
    }


    public static String getEndOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        /*calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);*/
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }


//获取当前年月日
    public static String getDate() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

//获取本年
    public static String getYear() {
        Calendar c = Calendar.getInstance();
        String getCurrentTime = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        return String.valueOf(c.get(Calendar.YEAR));
    }

//计算两个数占得百分比
    public static String getPercent(int x, int y) {
        double d1 = x * 1.0;
        double d2 = y * 1.0;
        NumberFormat percentInstance = NumberFormat.getPercentInstance();
        // 设置保留几位小数,这里设置的是保留两位小数
        percentInstance.setMinimumFractionDigits(2);
        return percentInstance.format(d1 / d2);
    }

标签:return,String,Calendar,static,new,工具,public
From: https://www.cnblogs.com/srjx/p/16714560.html

相关文章

  • qperf 网络测试工具
    qperf是测试延迟以及带宽的工具,一般的用法是一个机器使用不带参数的,另外一个机器使用qperfhostnametests或者其他命令支持的testsMiscellaneousconf......
  • 使用Dolby Encoding Engine及一些三方工具实现在非macOS上制作Dolby Vision 8.4视频
    杜比视界作为一种新兴的、效果颇佳的HDR技术,正随着设备和软件等的更新换代走进千家万户,让越来越多的人体验到了HDR效果的魅力。目前,随着B站等视频平台开始接受杜比视界内容......
  • 画图工具包plotly使用
    (1)越来越流行plotly(2)替换matplotlibdf=px.data.gapminder()df_2007=df.query("year==2007")fig=px.scatter(df_2007,x="gdpPercap",y="lifeExp",color="cont......
  • 开发工具的下载与学习使用
    1、IntelliJIDEA2022.2.2的下载和破解过程:先上IDEA官网下载IDEA  下载安装IDEA后,再去下载激活补丁  在 idea64.exe.vmoptions 配置文件结尾添加如下配置:......
  • Hbase 测试工具
    这篇文章主要介绍两种测试hbase的工具和部分参数wrk压力测试工具    WRK压力测试工具,这种方式可以测试前端的接口一起测试    但是瓶颈也是前端的java接口,没......
  • JavaScript 工具函数:随机取自然数
    functionrandomUint(max){returnMath.floor(Math.random()*max);}Math.random()*max返回的是大于0的浮点数,不能四舍五入取整。用Math.floor()对上一个结果......
  • nxopen vb.net 底打孔工具
    OptionStrictOffImportsSystemImportsNXOpenImportsNXOpen.BlockStylerImportsNXOpen.UFImportsSystem.Collections.Generic'----------------------------......
  • 开发工具:第四章:Java开发必选工具
      更多内容请见原文,原文转载自:https://blog.csdn.net/weixin_44519496/article/details/120323616......
  • MySQL实现备份(3)之xtrabackup 备份工具
    xtrabackup工具:是percona公司开发的一个用于对MySQL进行备份的工具。相对于mysqldump,xtrabackup支持增量备份、差异备份等。使用手册;https://www.percona.com/doc/percon......
  • JAVA工具类-DES加密解密
    importjavax.crypto.*;importjavax.crypto.spec.DESKeySpec;importjava.security.SecureRandom;importlombok.extern.slf4j.Slf4j;importorg.apache.commons.co......