StringUtil { public static int compareVersion(String version1, String version2) { String[] v1 = version1.split("\\."); String[] v2 = version2.split("\\."); for(int i = 0; i < Math.max(v1.length, v2.length); i++) { int gap = (i < v1.length ? Integer.parseInt(v1[i]) : 0) - (i < v2.length ? Integer.parseInt(v2[i]) : 0); if(gap != 0) return gap > 0 ? 1 : -1; } return 0; }  static String subHead(String str, int maxLen); static String toString(Object obj); static String randomString(int length); static String join(String split, String... args); static String join(String split, Object... args); static String join(String split, Function<T, String> function, Collection<T> args); static String join(String split, Function<T, String> function, T ... args); static boolean isEmpty(String str); static String[] splitRemoveEmpty(String regex, String str); static boolean trimEquals(String str1, String str2, boolean ignoreCase); static boolean trimEquals(String str1, String str2); static String[] splitByLen(String buff, int splitLen); static int compareVersion(String version1, String version2); static String[] splitByLineRemoveEmpty(String str);  }
@Test public void testCompareVersion() { String version = "2.8.19"; String targetVersion = "2.8.22"; Assert.assertEquals(-1, StringUtil.compareVersion(version, targetVersion)); version = "2.8.19.1"; Assert.assertEquals(-1, StringUtil.compareVersion(version, targetVersion)); version = "2.8"; Assert.assertEquals(-1, StringUtil.compareVersion(version, targetVersion)); version = "2.8.22.1"; Assert.assertEquals(1, StringUtil.compareVersion(version, targetVersion)); }