tl;dr
This will convert the example String
to the system default time zone:
import java.time.ZonedDateTime
import java.time.ZoneId
fun main() {
// example String
val orderCreationDate = "2020-09-01T13:16:33.114Z"
// parse it to a ZonedDateTime and adjust the zone to the system default
val localZonedDateTime = ZonedDateTime.parse(orderCreationDate)
.withZoneSameInstant(ZoneId.systemDefault())
// print the adjusted values
println(localZonedDateTime)
}
The output depends on the system default time zone, in the Kotlin Playground, it produces the following line:
2020-09-01T13:16:33.114Z[UTC]
which obviously means the Kotlin Playground is playing in UTC.
A little more...
It's strongly recommended to use java.time
nowadays and to stop using the outdated libraries for datetime operations (java.util.Date
, java.util.Calendar
along with java.text.SimpleDateFormat
).
If you do that, you can parse this example String
without specifying an input format because it is formatted in an ISO standard.
You can create an offset-aware (java.time.OffsetDateTime
) object or a zone-aware one
(java.time.ZonedDateTime
), that's up to you. The following example(s) show(s) how to parse your String
, how to adjust a zone or an offset and how to print in a different format:
import java.time.OffsetDateTime
import java.time.ZonedDateTime
import java.time.ZoneId
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
fun main() {
// example String
val orderCreationDate = "2020-09-01T13:16:33.114Z"
// parse it to an OffsetDateTime (Z == UTC == +00:00 offset)
val offsetDateTime = OffsetDateTime.parse(orderCreationDate)
// or parse it to a ZonedDateTime
val zonedDateTime = ZonedDateTime.parse(orderCreationDate)
// print the default output format
println(offsetDateTime)
println(zonedDateTime)
// adjust both to a different offset or zone
val localZonedDateTime = zonedDateTime.withZoneSameInstant(ZoneId.of("Brazil/DeNoronha"))
val localOffsetDateTime = offsetDateTime.withOffsetSameInstant(ZoneOffset.ofHours(-2))
// print the adjusted values
println(localOffsetDateTime)
println(localZonedDateTime)
// and print your desired output format (which doesn't show a zone or offset)
println(localOffsetDateTime.format(
DateTimeFormatter.ofPattern("dd-MM-uuuu hh:mm a")
)
)
println(localZonedDateTime.format(
DateTimeFormatter.ofPattern("dd-MM-uuuu hh:mm a")
)
)
}
The output is
2020-09-01T13:16:33.114Z
2020-09-01T13:16:33.114Z
2020-09-01T11:16:33.114-02:00
2020-09-01T11:16:33.114-02:00[Brazil/DeNoronha]
01-09-2020 11:16 AM
01-09-2020 11:16 AM
For a conversion to the system zone or offset, use ZoneId.systemDefault()
or ZoneOffset.systemDefault()
instead of hard coded ones. Pay attention to the ZoneOffset
since it does not necessarily give the correct one because only a ZoneId
considers daylight saving time.
For more information, see this question and its answer
For further and more accurate information about formats to be defined for parsing or formatting output, you should read the JavaDocs of DateTimeFormatter
.