您当前的位置: 首页 >  Java

ITKEY_

暂无认证

  • 2浏览

    0关注

    732博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Java开发中经常用到的工具方法

ITKEY_ 发布时间:2020-09-02 10:48:36 ,浏览量:2

Java开发中经常用到的工具方法,常用方法。

今天日期转成Long
public static Long todayLong(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dayStr = sdf.format(new Date());
        return Long.parseLong(dayStr);
    }
获取今年的年份
 public static Long nianLong(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String dayStr = sdf.format(new Date());
        return Long.parseLong(dayStr);
    }
获取今年的年份月份
 public static Long nianYueLong(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String dayStr = sdf.format(new Date());
        return Long.parseLong(dayStr);
    }
 把yyyyMMdd 格式的Long 日期对象格式化
    public static String formatDate(Long day,String dateFormat){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date d  = null;
//这里会有一个异常,所以要用try catch捕获异常
        try {
            d  = sdf.parse(day+"");
        }catch (Exception e){
            e.printStackTrace();
        }
        SimpleDateFormat result = new SimpleDateFormat(dateFormat);
        return result.format(d);
    }
获取临时目录路径
public static String staticPath(){
        //获取跟目录
        File path = null;
        try {
            path = new File(ResourceUtils.getURL("classpath:").getPath());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if(!path.exists()) path = new File("");
        File upload = new File(path.getAbsolutePath(),"static/");
        if(!upload.exists()) upload.mkdirs();
        return  upload.getAbsolutePath();
    }
四舍五入
    /**
     * 进行准确位数的四舍五入的处理操作
     * @param num 表示要进行处理的数字
     * @param scale 表示要保留的小数位数
     * @return 四舍五入处理后的结果
     */
    public static double round(double num, int scale) {
        return Math.round(num * Math.pow(10.0, scale)) / Math.pow(10.0, scale);
    }
    public static double round2(double num, int scale) {
        return new BigDecimal(num).divide(new BigDecimal(1), scale, RoundingMode.HALF_UP).doubleValue() ;
    }

 

关注
打赏
1665243900
查看更多评论
立即登录/注册

微信扫码登录

0.0371s