Java Runtime.Version parse String

Description

Java Runtime.Version parse String

import java.lang.Runtime.Version;
import java.util.stream.Collectors;

public class Main {
  public static void main(String[] args) {
    // Have some version strings
    String[] versionStrings = { //from  www. j  a v  a  2s  .c  o m
        "11", 
        "12.1", 
        "13.0.1.2", 
        "14.0.2.3.4", 
        "15.0.0", 
        "16.1.2-ea+153", 
        "17+132",
        "18-ea+24-2020-01-23", 
        "19+-123", 
        "20.0.1-ea+132-2020-01-31.10.24.56am" };

    for (String vstr : versionStrings) {
      try {
        Version version = Version.parse(vstr);
        String vnumAdditionalInfo = version.version().stream().skip(4).map(n -> n.toString())
            .collect(Collectors.joining("."));

        System.out.printf("Version String=%s%n", vstr);
        System.out.printf(
            "feature=%d, interim=%d, update=%d, patch=%d," + " additional info=%s,"
                + " pre=%s, build=%s, optional=%s %n%n",
            version.feature(), version.interim(), version.update(), version.patch(), vnumAdditionalInfo,
            version.pre().orElse(""), version.build().isPresent() ? version.build().get().toString() : "",
            version.optional().orElse(""));
      } catch (Exception e) {
        System.out.printf("%s%n%n", e.getMessage());
      }
    }
  }
}



PreviousNext

Related