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

猜你喜欢

转载自blog.csdn.net/lxyoucan/article/details/108357765