常用Java开发工具代码

1、比较string类型时间的大小

public boolean lg(String s1, String s2) {
        int i1 = Integer.parseInt(s1);
        int i2 = Integer.parseInt(s2);
        if (i1 > i2) {
            return true;
        }
        return false;
}

2、将时间戳格式化为时间

    public String date2TimeStamp(String timestamp) {
        //时间格式,HH是24小时制,hh是AM PM12小时制
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //比如timestamp=1449210225945;
        long date_temp = Long.parseLong(timestamp);
        return sdf.format(new Date(date_temp));
        //至于取10位或取13位,date_temp*1000L就是这种截取作用。如果是和服务器传值的,就和后台商量好就可以了
    }

3、md5加密

    public String md5(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");// 生成一个MD5加密计算摘要
            md.update(str.getBytes());// 计算md5函数
            /**
             * digest()最后确定返回md5 hash值,返回值为8位字符串。
             * 因为md5 hash值是16位的hex值,实际上就是8位的字符
             * BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值
             * 一个byte是八位二进制,也就是2位十六进制字符(2的8次方等于16的2次方)
             */
            String hashedPwd = new BigInteger(1, md.digest()).toString(16);// 16是表示转换为16进制数
            return hashedPwd;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "1234567";
        }
    }

4、判断是否是JSON

    public boolean isJSON(String str) {
        boolean result = false;
        try {
            JSON.parseObject(str);
            result = true;
        } catch (Exception e) {
//            e.printStackTrace();
        }
        return result;
    }

5、比较两个日期的大小

    public boolean lgBoo(String time1, String time2) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if (dateFormat.parse(time1).getTime() > dateFormat.parse(time2).getTime()) {
                return true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

6、Java curl请求

    public static String execCurl(String[] cmds) {
        ProcessBuilder process = new ProcessBuilder(cmds);
        Process p;
        try {
            p = process.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
//                builder.append(System.getProperty("line.separator"));
            }
            return builder.toString();

        } catch (IOException e) {
            System.out.print("error。。。");
            e.printStackTrace();
        }
        return null;
    }

7、两个整数相除得到百分比

String.format("%.2f%%",(Integer.parseInt(v2) - Integer.parseInt(v1)) * 1.00 / Integer.parseInt(v2) * 100)

8、根据经纬度计算距离

public class DistanceUtils {
    /**
     * 地球半径,单位 km
     */
    private static final double EARTH_RADIUS = 6378.137;

    /**
     * 根据经纬度,计算两点间的距离
     *
     * @param longitude1 第一个点的经度
     * @param latitude1  第一个点的纬度
     * @param longitude2 第二个点的经度
     * @param latitude2  第二个点的纬度
     * @return 返回距离 单位千米
     */
    public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
        // 纬度
        double lat1 = Math.toRadians(latitude1);
        double lat2 = Math.toRadians(latitude2);
        // 经度
        double lng1 = Math.toRadians(longitude1);
        double lng2 = Math.toRadians(longitude2);
        // 纬度之差
        double a = lat1 - lat2;
        // 经度之差
        double b = lng1 - lng2;
        // 计算两点距离的公式
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(b / 2), 2)));
        // 弧长乘地球半径, 返回单位: 千米
        s = s * EARTH_RADIUS;
        return s;
    }

    public static void main(String[] args) {
        double d = getDistance(116.481725, 39.990958, 116.472705, 39.995967);
        System.out.println(d);
    }

}

9、Java 定时器

java定时器 - 知乎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值