Groovy web console

subscribe to the feed Subscribe
to this
site

Lib fix

Published 1 month ago by S
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
public String getVersionCodeOld(millis){
    def date = new Date(millis) ;
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);     
    def year = calendar.get(Calendar.YEAR) - 2000;
    def day = calendar.get(Calendar.DAY_OF_YEAR);
    def hour = calendar.get(Calendar.HOUR_OF_DAY);
    
    return "" + year + day + hour;
}

public String getVersionCodeFixed(millis){
    def date = new Date(millis) ;
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);     
    def year = calendar.get(Calendar.YEAR) - 2000;
    def day = calendar.get(Calendar.DAY_OF_YEAR).toString().padLeft(3, "0");
    def hour = calendar.get(Calendar.HOUR_OF_DAY).toString().padLeft(2, "0");
    
    return "" + year + day + hour;
}

Long millis = 1641112200000L

println(getVersionCodeOld(millis));
println(getVersionCodeFixed(millis));