Sea*_*ull 16 java date java-8 java-time
我一直在尝试使用Java 8中内置的java.time包将"DateTime"转换为毫秒.
但我无法正确地做到这一点.我想将"29/Jan/2015:18:00:00"转换为毫秒.以下是我尝试过的
Instant instant = Instant.parse("2015-01-29T18:00:00.0z");
Long instantMilliSeconds = Long.parseLong(instant.getEpochSecond() + "" + instant.get(ChronoField.MILLI_OF_SECOND));
System.out.println(new Date(instantMilliSeconds)); // prints Sun Jun 14 05:06:00 PDT 1970
Run Code Online (Sandbox Code Playgroud)
我尝试过使用LocalDateTime,但找不到有效转换为毫秒的方法.我并不是说这是最好的方法,如果你知道更好的东西,我会非常感谢一些指示.
Jef*_*rey 26
System.out.println(instant.toEpochMilli());
System.out.println(instant.getEpochSecond());
System.out.println(instant.get(ChronoField.MILLI_OF_SECOND));
Run Code Online (Sandbox Code Playgroud)
版画
1422554400000
1422554400
0
Run Code Online (Sandbox Code Playgroud)
您的版本无法正常工作,因为您忘记instant.get(ChronoField.MILLI_OF_SECOND)使用额外的零填充以将其填充到3个位置.
| 归档时间: |
|
| 查看次数: |
23583 次 |
| 最近记录: |