Parsing date and time now manually as Java's DateFormat is not thread safe causing a lot of curious errors when executed in parallel
This commit is contained in:
parent
1216267fec
commit
d35a420c29
|
@ -1,6 +1,8 @@
|
||||||
package net.dankito.utils.multiplatform
|
package net.dankito.utils.multiplatform
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Be aware that Java DateFormat is not thread safe!
|
||||||
|
*/
|
||||||
expect class DateFormatter constructor(pattern: String) {
|
expect class DateFormatter constructor(pattern: String) {
|
||||||
|
|
||||||
constructor(dateStyle: DateFormatStyle)
|
constructor(dateStyle: DateFormatStyle)
|
||||||
|
|
|
@ -4,6 +4,7 @@ import net.dankito.banking.fints.messages.Existenzstatus
|
||||||
import net.dankito.banking.fints.messages.datenelemente.basisformate.NumerischesDatenelement
|
import net.dankito.banking.fints.messages.datenelemente.basisformate.NumerischesDatenelement
|
||||||
import net.dankito.utils.multiplatform.Date
|
import net.dankito.utils.multiplatform.Date
|
||||||
import net.dankito.utils.multiplatform.DateFormatter
|
import net.dankito.utils.multiplatform.DateFormatter
|
||||||
|
import net.dankito.utils.multiplatform.log.LoggerFactory
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -19,12 +20,29 @@ open class Datum(date: Int?, existenzstatus: Existenzstatus) : NumerischesDatene
|
||||||
val HbciDateFormat = DateFormatter(HbciDateFormatString)
|
val HbciDateFormat = DateFormatter(HbciDateFormatString)
|
||||||
|
|
||||||
|
|
||||||
|
private val log = LoggerFactory.getLogger(Datum::class)
|
||||||
|
|
||||||
|
|
||||||
fun format(date: Date): String {
|
fun format(date: Date): String {
|
||||||
return HbciDateFormat.format(date) // TODO: is this correct?
|
return HbciDateFormat.format(date) // TODO: is this correct?
|
||||||
}
|
}
|
||||||
|
|
||||||
fun parse(dateString: String): Date {
|
fun parse(dateString: String): Date {
|
||||||
return HbciDateFormat.parse(dateString) !!
|
// do not use DateFormatter as Java DateFormat is not thread safe, resulting in a lot of curious errors in parallel execution
|
||||||
|
|
||||||
|
if (dateString.length == 8) {
|
||||||
|
try {
|
||||||
|
val year = dateString.substring(0, 4)
|
||||||
|
val month = dateString.substring(4, 6)
|
||||||
|
val day = dateString.substring(6, 8)
|
||||||
|
|
||||||
|
return Date(year.toInt(), month.toInt(), day.toInt())
|
||||||
|
} catch (e: Exception) {
|
||||||
|
log.error(e) { "Could not parse date string '$dateString' to HBCI date" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw IllegalArgumentException("Cannot parse '$dateString' to HBCI Date. Only dates in format '$HbciDateFormatString' are allowed in HBCI / FinTS.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import net.dankito.banking.fints.messages.Existenzstatus
|
||||||
import net.dankito.banking.fints.messages.datenelemente.basisformate.ZiffernDatenelement
|
import net.dankito.banking.fints.messages.datenelemente.basisformate.ZiffernDatenelement
|
||||||
import net.dankito.utils.multiplatform.Date
|
import net.dankito.utils.multiplatform.Date
|
||||||
import net.dankito.utils.multiplatform.DateFormatter
|
import net.dankito.utils.multiplatform.DateFormatter
|
||||||
|
import net.dankito.utils.multiplatform.log.LoggerFactory
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -20,12 +21,29 @@ open class Uhrzeit(time: Int?, existenzstatus: Existenzstatus) : ZiffernDatenele
|
||||||
val HbciTimeFormat = DateFormatter(HbciTimeFormatString)
|
val HbciTimeFormat = DateFormatter(HbciTimeFormatString)
|
||||||
|
|
||||||
|
|
||||||
|
private val log = LoggerFactory.getLogger(Uhrzeit::class)
|
||||||
|
|
||||||
|
|
||||||
fun format(time: Date): String {
|
fun format(time: Date): String {
|
||||||
return HbciTimeFormat.format(time) // TODO: is this correct?
|
return HbciTimeFormat.format(time) // TODO: is this correct?
|
||||||
}
|
}
|
||||||
|
|
||||||
fun parse(dateString: String): Date {
|
fun parse(timeString: String): Date {
|
||||||
return HbciTimeFormat.parse(dateString) !!
|
// do not use DateFormatter as Java DateFormat is not thread safe, resulting in a lot of curious errors in parallel execution
|
||||||
|
|
||||||
|
if (timeString.length == 6) {
|
||||||
|
try {
|
||||||
|
val hour = timeString.substring(0, 2)
|
||||||
|
val minute = timeString.substring(2, 4)
|
||||||
|
val second = timeString.substring(4, 6)
|
||||||
|
|
||||||
|
return Date(0, 0, 0, hour.toInt(), minute.toInt(), second.toInt())
|
||||||
|
} catch (e: Exception) {
|
||||||
|
log.error(e) { "Could not parse time string '$timeString' to HBCI time" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw IllegalArgumentException("Cannot parse '$timeString' to HBCI Time. Only times in format '${Uhrzeit.HbciTimeFormatString}' are allowed in HBCI / FinTS.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue