How to add a time tag to the packaged apk name in gradle, but the international time is eight hours later than Beijing time, how to use Beijing time?

 The background is: when gradle generates the apk package, a specific package name needs to be generated, as follows; however, the UTC logo is used to print the international time, and the international time is eight time zones behind Beijing time, that is, eight hours behind. How to use Beijing time?

    android.applicationVariants.all { variant ->
        variant.outputs.all { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith(".apk")) {
                if (variant.buildType.name == "release") {
                    outputFileName = "xxx_v" + defaultConfig.versionName + "_" + releaseTime() + "_" + variant.productFlavors[0].name + ".apk"
                } else {
                    outputFileName = "xxx_v" + defaultConfig.versionName + "_" + releaseTime() + "_" + variant.productFlavors[0].name + "_" + variant.buildType.name + ".apk"
                }
            }
        }
    }
}

static def releaseTime() {
    // 国际时间,比北京时间晚八小时
    // TimeZone beijingTimeZone = TimeZone.getTimeZone("UTC")
    // 获取代表北京时区的TimeZone对象
    TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai")
    // 创建一个SimpleDateFormat实例,并将其时区设置为北京
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm")
    dateFormat.setTimeZone(timeZone)
    // 获取当前时间并格式化为北京时间
    return dateFormat.format(new Date())
}

Solution: Obtain the TimeZone object representing the Beijing time zone, the identifier is "Asia/Shanghai", and then format the date of the current time. The above method is defined and used in the gradle file. The following is an example of usage in a java file:

    public static void main(String[] args) {
        System.out.println(releaseTime());
    }

    public static String releaseTime() {
        // 国际时间,比北京时间晚八小时
        // TimeZone beijingTimeZone = TimeZone.getTimeZone("UTC");
        // 获取代表北京时区的TimeZone对象
        TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai");
        // 创建一个SimpleDateFormat实例,并将其时区设置为北京
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");
        dateFormat.setTimeZone(timeZone);
        // 获取当前时间并格式化为北京时间
        return dateFormat.format(new Date());
    }

Create value, happy to share! 

Guess you like

Origin blog.csdn.net/ly_xiamu/article/details/131260309