Quantcast
Channel: How can I pad an integer with zeros on the left? - Stack Overflow
Viewing all articles
Browse latest Browse all 23

Answer by Haseeb Hassan Asif for How can I pad an integer with zeros on the left?

$
0
0

For Kotlin

fun Calendar.getFullDate(): String {    val mYear = "${this.get(Calendar.YEAR)}-"    val mMonth = if (this.get(Calendar.MONTH) + 1 < 10) {"0${this.get(Calendar.MONTH) + 1}-"    } else {"${this.get(Calendar.MONTH)+ 1}-"    }    val mDate = if (this.get(Calendar.DAY_OF_MONTH)  < 10) {"0${this.get(Calendar.DAY_OF_MONTH)}"    } else {"${this.get(Calendar.DAY_OF_MONTH)}"    }    return mYear + mMonth + mDate}

and use it as

val date: String = calendar.getFullDate()


Viewing all articles
Browse latest Browse all 23

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>