数据库查出>= 0.99.0, < 1.25.0 这样的字符串,怎么用Java代码去比较
时间: 2024-03-10 12:43:34 浏览: 188
你可以使用`Version`类来实现该比较,这个类可以将版本号字符串转换为`Version`对象进行比较。
首先,你需要添加以下依赖:
```xml
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.7</version>
</dependency>
```
然后,你可以使用以下代码来比较版本号:
```java
import java.util.Arrays;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
public class VersionCompare {
public static void main(String[] args) {
String version = "1.0.0";
String[] range = { ">= 0.99.0", "< 1.25.0" };
DefaultArtifactVersion currentVersion = new DefaultArtifactVersion(version);
DefaultArtifactVersion lowerBound = new DefaultArtifactVersion(range[0].substring(2));
DefaultArtifactVersion upperBound = new DefaultArtifactVersion(range[1].substring(2));
if (currentVersion.compareTo(lowerBound) >= 0 && currentVersion.compareTo(upperBound) < 0) {
System.out.println("当前版本符合范围");
} else {
System.out.println("当前版本不符合范围");
}
}
}
```
这段代码将会输出"当前版本符合范围"或"当前版本不符合范围"。
阅读全文