Skip to content

Commit ee4d389

Browse files
author
吕发强
committed
添加对比版本号方法。
1 parent a5f917c commit ee4d389

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

library/src/main/java/com/lvfq/library/utils/LvAndroidUtil.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,41 @@ public static String getVersionName() {
4545
return "";
4646
}
4747

48+
/**
49+
* 比较两个版本的大小
50+
*
51+
* @param version1
52+
* @param version2
53+
* @return 返回0和-1,-1表示小于后面的版本
54+
*/
55+
public static int compareVersion(String version1, String version2) {
56+
if (version1.equals(version2)) {
57+
return 0;
58+
}
59+
String[] version1Array = version1.split("\\.");
60+
String[] version2Array = version2.split("\\.");
61+
int index = 0;
62+
int minLen = Math.min(version1Array.length, version2Array.length);
63+
int diff = 0;
64+
while (index < minLen && (diff = Integer.parseInt(version1Array[index]) - Integer.parseInt(version2Array[index])) == 0) {
65+
index++;
66+
}
67+
if (diff == 0) {
68+
for (int i = index; i < version1Array.length; i++) {
69+
if (Integer.parseInt(version1Array[i]) > 0) {
70+
return 1;
71+
}
72+
}
73+
for (int i = index; i < version2Array.length; i++) {
74+
if (Integer.parseInt(version2Array[i]) > 0) {
75+
return -1;
76+
}
77+
}
78+
79+
return 0;
80+
} else {
81+
return diff > 0 ? 1 : -1;
82+
}
83+
}
84+
4885
}

0 commit comments

Comments
 (0)