最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

kotlin - how to parse date 2025-03-20T12:45:00Z to 2025-03-20T12:45+03:00 kotlinx-datetime android - Stack Overflow

programmeradmin6浏览0评论

i have a timestamp in milliseconds (for example, 1710929100000) (Long). First i convert it to Instant selectedDateTimeMillis.toInstant() and get a output: 2025-03-20T12:45:00Z. Now, I need to format it as a local date-time with a offset (in current location UTC+3) keeping the same time: 2025-03-20T12:45+03:00

How can i do this? I tried different methods, but most often my hours in the date just increased

i have a timestamp in milliseconds (for example, 1710929100000) (Long). First i convert it to Instant selectedDateTimeMillis.toInstant() and get a output: 2025-03-20T12:45:00Z. Now, I need to format it as a local date-time with a offset (in current location UTC+3) keeping the same time: 2025-03-20T12:45+03:00

How can i do this? I tried different methods, but most often my hours in the date just increased

Share Improve this question asked Mar 21 at 15:58 onesectoronesector 4739 silver badges26 bronze badges 3
  • "but most often my hours in the date just increased" - yes, and that's natural, because the instant you're receiving doesn't represent 2025-03-20T12:45+03:00. Do you understand that 2025-03-20T12:45+03:00 is a different instant in time? Are you actually trying to represent an instant in time three hours later? (What you're trying to achieve is not formatting the original instant in time in a specific time zone - it's formatting a different instant.) – Jon Skeet Commented Mar 21 at 16:04
  • @JonSkeet I found an example on the Internet: March 11, 2017 at 11:30 am. in Seoul needs to be converted during Unix it will be an integer type with value 1489199400. According to ISO-8601 it will be a string type whose value is: 2017-03-11T11:30:00+09:00. How is this different from my example? I want to do the same if I am in UTC +3 hours – onesector Commented Mar 21 at 16:34
  • That value would be an instant of 2017-03-11T02:30:00Z - which is equivalent to 2017-03-11T11:30:00+09:00. In other words, 2017-03-11T11:30:00+09:00 is still a representation of the same instant - whereas what you're asking for is a representation of a different instant. Note that your sample of 1710929100000 is 2024-03-20T10:05:00Z, which is nothing like the value you're later saying - it would really help if you could use one consistent sample. – Jon Skeet Commented Mar 21 at 17:05
Add a comment  | 

1 Answer 1

Reset to default 0

I tried to solve your question. So i wrote and checked this code.

@RequiresApi(Build.VERSION_CODES.O)
private fun convertTimestamp(timestamp: Long): String? {
    val instant = Instant.ofEpochMilli(timestamp)
    val zoneOffset = ZoneOffset.ofHours(3)
    val zonedDateTime = instant.atZone(zoneOffset)
    val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mmXXX")
    val formattedDateTime = zonedDateTime.format(formatter)
    return formattedDateTime
}
发布评论

评论列表(0)

  1. 暂无评论