fun componi_data( x: String): String
{
//---- permette di ricostruire una data valida, come stringa, anche a partire //---- da valori del tipo: 190122 o 19012022
var y = ""
var controllo = true
if (!x.contains("/") && !x.contains("-") && x.length >= 6) {
if (x.substring(0, 2) >= "01" && x.substring(0, 2) <= "31")
y = x.substring(0, 2) + "/"
else
controllo = false
if (x.substring(2, 4) >= "01" && x.substring(2, 4) <= "12")
y += x.substring(2, 4) + "/"
else
controllo = false
if (x.length == 6) {
if (x.substring(4, 6) >= "00" && x.substring(4, 6) <= "50")
y += "20" + x.substring(4, 6)
else
if (x.substring(4, 6) >= "51" && x.substring(4, 6) <= "99")
y += "19" + x.substring(4, 6)
else
controllo = false
}
if (x.length == 8) {
if (x.substring(4, 8) >= "1900" && x.substring(4, 6) <= "2099")
y += x.substring(4, 8)
else
controllo = false
}
} else {
if (x.contains("/") || x.contains("-") ) {
lateinit var v: List<String>
//--- stringhe di formato utilizzate per la formattazione di numeri --
var df2: DecimalFormat = DecimalFormat(" 00")
var df4: DecimalFormat = DecimalFormat(" 0000")
var k = 0
if (x.contains("/")) v = x.split("/")
if (x.contains("-")) v = x.split("-")
//--- y = df2.format(k) : il valore k viene formattato secondo il formato df2
if (v.size == 3) {
k = v[0].toInt()
if (k in 1..31) y = df2.format(k).trim() + "/"
else
controllo = false
k = v[1].toInt()
if (k in 1..12) y += df2.format(k).trim() + "/"
else
controllo = false
k = v[2].toInt()
if (k in 1..90) {
y += "20" + df2.format(k).trim()
} else {
if (k in 2000..2090)
y += df4.format(k).trim()
else
controllo = false
}
} else
controllo = false
} else
controllo = false
}
if (!controllo) y = "Errore in " + x
return y
}