Timestamp convert [duplicate]
My below function doesn't cast the date to the defined format.
val oldFormat= new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSSSSS")
val newFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSSSSS",Locale.ENGLISH)
newFormat.format(oldFormat.parse(s).getTime))
Input date is in format yyyy-MM-dd-HH.mm.ss.SSSSSS, need to convert that into yyyy-MM-dd hh:mm:ss.SSSSSS.
my input is 2019-10-08-03.57.14.694695 but the above code outputs to 2019-10-08 04:15:04.000695 what am I doing wrong here
Using the newer DatetimeFormatter
and LocalDateTime
API introduced in Java 8:
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.*
fun main(args: Array<String>){
val oldFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH.mm.ss.SSSSSS")
val localDateTime = LocalDateTime.parse("2019-10-08-03.57.14.694695", oldFormatter)
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS", Locale.ENGLISH)
val output = formatter.format(localDateTime)
println(output)
}
I created a custom formatter and obtained date object which I reformatted again using a different custom formatter.