【问题标题】:Date to local time kotlin日期为当地时间 kotlin
【发布时间】:2020-04-29 07:23:26
【问题描述】:

我有一个带有

的日期对象

“2020 年 4 月 26 日星期日 11:44:00 GMT+03:00”

我尝试对其进行格式化,但它总是会忽略时区“+3:00”并显示小时而不更改它

我尝试过的:

   val sdf = SimpleDateFormat("dd MM yyyy HH:mm:ss", Locale.US)
    return sdf.format(this)

【问题讨论】:

  • 你想要什么结果?
  • 第一个问题一定是你为什么使用旧的 Java 日期 API?尤其是在 Kotlin 中。接下来要指出的是,Date自 UTC 纪元以来的毫秒数 - 没有时区!!您看到的时区是调用toString 的结果,它将使用系统默认时区。基本上,这个问题没有多大意义……
  • @sweeper 我想格式化时间,但不知何故,如果我得到 +2GMT 或 +3GMT,我似乎不会在几个小时内影响结果
  • 您真的只想以不同的方式格式化该日期时间,还是要将其转换为另一个区域或偏移量?
  • 那么你真的应该使用java.time(或者如果你支持低于26的API级别,则使用反向端口)并使用@kaustubhpatange建议的OffsetDateTime甚至ZonedDateTime

标签: java android date kotlin


【解决方案1】:

我不完全理解您的问题,但我猜您的意思是您需要与特定区域相关的 DateTime。在 kotlin 中,我们有 ZonedDateTime

import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle

val londonZone = ZoneId.of("Europe/London")
val londonCurrentDateTime = ZonedDateTime.now(londonZone)
println(londonCurrentDateTime) // Output:  2018-01-25T07:41:02.296Z[Europe/London]
val londonDateAndTime = londonCurrentDateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.MEDIUM))
println(londonDateAndTime) // Output: Thursday, January 25, 2018 7:40:34 AM

您可能还需要查看LocalDateTime 类。

【讨论】:

    【解决方案2】:

    如果您只想使用给定模式解析日期时间String,然后将结果转换为不同的偏移量,您可以这样:

    fun main() {
        // provide the source String
        val datetime = "Sun Apr 26 11:44:00 GMT+03:00 2020"
        // provide a pattern for parsing
        val parsePattern = "E MMM dd HH:mm:ss O yyyy";
        // parse the String to an OffsetDateTime
        val parsedOffsetDateTime = java.time.OffsetDateTime.parse(
                                    datetime,
                                    java.time.format.DateTimeFormatter.ofPattern(parsePattern))
        // print the result using the default format
        println(parsedOffsetDateTime)
    
        // then get the same moment in time at a different offset
        val adjustedOffsetDateTime = parsedOffsetDateTime.withOffsetSameInstant(
                                        java.time.ZoneOffset.of("+02:00"))
        // and print that, too, in order to see the difference
        println(adjustedOffsetDateTime)
    }
    

    产生输出

    2020-04-26T11:44+03:00
    2020-04-26T10:44+02:00
    

    【讨论】:

      【解决方案3】:

      格式字符串取决于您使用的SimpleDateFormat

      演示(纯 Java)

      TimeZone.setDefault(TimeZone.getTimeZone("Asia/Jerusalem"));
      
      Date date = new Date(1587890640000L); // date object with "Sun Apr 26 11:44:00 GMT+03:00 2020"
      
      SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM d HH:mm:ss 'GMT'XXX yyyy", Locale.US);
      String dateString = sdf.format(date);
      System.out.println("Formatted  : " + dateString);
      
      Date parsed = sdf.parse(dateString);
      System.out.println("Parsed back: " + parsed);
      System.out.println("Millis since Epoch: " + parsed.getTime());
      

      输出

      Formatted  : Sun Apr 26 11:44:00 GMT+03:00 2020
      Parsed back: Sun Apr 26 11:44:00 IDT 2020
      Millis since Epoch: 1587890640000
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-25
        • 2018-04-10
        • 1970-01-01
        • 2020-12-24
        • 1970-01-01
        • 2012-10-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多