1.版本号工具
之前在网上看到版本号工具,觉得还行,就一直用这个。代码如下,写的也挺简洁的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public static int compareAppVersion(String version1, String version2) { if (version1 == null || version2 == null) { throw new RuntimeException("版本号不能为空"); } String[] versionArray1 = version1.split("\\."); String[] versionArray2 = version2.split("\\."); int idx = 0; int minLength = Math.min(versionArray1.length, versionArray2.length); int diff = 0; while (idx < minLength && (diff = versionArray1[idx].length() - versionArray2[idx].length()) == 0 && (diff = versionArray1[idx].compareTo(versionArray2[idx])) == 0) { ++idx; } diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length; return diff; }
|