code: public static String convertToDebianVersion( String version, boolean apply, String envName, String template, Date timestamp ) {
          Matcher matcher = SNAPSHOT_PATTERN.matcher(version);
          if (matcher.matches()) {
              version = matcher.group(1) + "~";
  
              if (apply) {
                  final String envValue = System.getenv(envName);
                  if(template != null && template.length() > 0) {
                      version += formatSnapshotTemplate(template, timestamp);
                  } else if (envValue != null && envValue.length() > 0) {
                      version += envValue;
                  } else {
                      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                      dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
                      version += dateFormat.format(timestamp);
                  }
              } else {
                  version += "SNAPSHOT";
              }
          } else {
              matcher = BETA_PATTERN.matcher(version);
              if (matcher.matches()) {
                  if (matcher.group(1) != null) {
                      version = matcher.group(1) + "~" + matcher.group(4) + matcher.group(5);
                  } else {
                      version = matcher.group(3) + "~" + matcher.group(4) + matcher.group(5);
                  }
              }
          }
  
          // safest upstream_version should only contain full stop, plus, tilde, and alphanumerics
          // https://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version
          version = version.replaceAll("[^\\.+~A-Za-z0-9]", "+").replaceAll("\\++", "+");
  
          return version;
      }
docs: Convert the project version to a version suitable for a Debian package.
  -SNAPSHOT suffixes are replaced with a timestamp (~yyyyMMddHHmmss).
  The separator before a rc, alpha or beta version is replaced with '~'
  such that the version is always ordered before the final or GA release.
  
  @param version the project version to convert to a Debian package version
  @param template the template used to replace -SNAPSHOT, the timestamp format is in brackets,
  the rest of the string is preserved (prefix[yyMMdd]suffix -> prefix151230suffix)
  @param timestamp the UTC date used as the timestamp to replace the SNAPSHOT suffix.