I'm parsing a date in with dd-MM-yyyy
format and returning in seconds (dividing it by 1000). The problem comes when I convert it to Unix Time Stamp, because it converts this seconds to the previous day. I will explain with my code and an example:
private fun String.toTimestamp(): String {
val dateFormat = SimpleDateFormat("dd-MM-yyyy", Locale.getDefault())
return (dateFormat.parse(this).time / 1000).toString
}
If the date is 01/02/2019
(2nd February 2019), this method returns 1548975600
. If you convert it to a date (I'm using this page) it returns 01/31/2019 @ 11:00pm (UTC)
. I've tried adding hour, minutes and seconds, even adding the time zone, but it always returns the day before.
Another example:
13-02-2019
> 1550012400
> 02/12/2019 @ 11:00pm (UTC)
The date comes from a DatePicker
, but if I create it in the next way it returns the correct day:
(Date().time / 1000).toString()
I've tried with the system's language in Spanish and in English, and changing the Locale
to Locale.ENGLISH
and Locale("es", "ES")
and the results are the same.
Any suggestions?