English 中文(简体)
小型电池板与UTC之间的距离
原标题:Get time difference in milliseconds between JVM and UTC

我用了极其艰难的时间来说明似乎是一个简单的问题。

我想计算一下UTC即时和JVERS(即操作该代码的机器所特有的)瞬间差幅。

我尝试了以下初步情况(即科特林而不是 Java):

// 1
val difference = TimeZone.getDefault().getOffset(System.currentTimeMillis())

// 2
val difference = ZoneId.systemDefault().rules.getOffset(Instant.EPOCH).totalSeconds * 1000

// 3
val difference = ZonedDateTime.now(ZoneId.systemDefault()).offset.totalSeconds * 1000

更具体地说: 今天,我正在美国西海岸的Mac(Feb 22, 2024——即节日节节节节节节节节节节节节节节期间)上发言。 所有3个初始化都产生了<代码>-28800000<>/code>(即基于时间区),当时我正在寻找一种将产生<代码>-25200000<<<>/code>(即:DST-灵敏度/即时值)。

我在这里做了什么错误?

我希望解决办法是时间区/机械学(即,我可以在世界上任何机器上操作这一守则,而没有像扼杀时间区这样的具体投入。

EDIT:

正如@Louis Wasserman所指出的,我期望初始化将产生<条码>-25200000<> 代码>是不正确的,因为目前的时间差异实际上是<条码>-28800000<>。

我的错误期望是由于我撰写了以下试验:

@Test
fun timeStrToEpochMillis_invariantValidation_happyPaths() {
    // Arrange
    val testTimeStr = "2023-06-13 20:21:46"

    // Act
    val result = timeStrToEpochMillis(testTimeStr)

    // Assert
    assertEquals(1686687706000, result)
}

以下职能:

fun timeStrToEpochMillis(timeStr: String): Long {
    val df = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    return df.parse(timeStr).time
}

当我对一个集装箱进行上述试验时,如果该集装箱的违约时间区为UTC,则该试验将经过。 然而,在对我的当地机器进行上述测试时,这一试验将失败,而当地机器的违约时间区是太平洋。

我忽略了,这种区别应当基于试验扼杀,例如:

@Test
fun timeStrToEpochMillis_invariantValidation_happyPaths() {
    // Arrange
    val testTimeStr = "2023-06-13 20:21:46"

    // Act
    val result = timeStrToEpochMillis(testTimeStr)
    val diff = TimeZone.getDefault().getOffset(result)

    // Assert
    assertEquals(1686687706000 + diff, result)
}

再次感谢@Louis Wasserman指出显而易见之处。

最佳回答

目前,这不是美国西海岸的节省时间。 The time Zone America/Los_Angeles, used by the US West Maritime, is present 28800 000 milliseconds from UTC.

The JVM is correct here.

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签