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

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() ;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值