Java时间到正常格式

我有Java时间1380822000000 。 我想转换成我能读到的东西:

 import java.util.Date object Ws1 { val a = new Date("1380822000000").toString() } 

导致例外

 warning: there were 1 deprecation warning(s); re-run with -deprecation for detai ls java.lang.IllegalArgumentException at java.util.Date.parse(Date.java:615) at java.util.Date.(Date.java:272) at .(:9) at .() at .(:7) at .() at $print() at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57 ) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl .java:43) at java.lang.reflect.Method.invoke(Method.java:606) at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:734) at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983) at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573) at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604) at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568) at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:745) at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:790) at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:702) at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:566) at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:573) at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:576) at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scal a:867) at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822) at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:822) at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader. scala:135) at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:822) at org.jetbrains.plugins.scala.worksheet.WorksheetRunner.main(WorksheetRunner.j ava:66) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57 ) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl .java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) 

如何将其转换为正常的人类表示?

或做更好的事情并使用org.jodatime ;

 import org.joda.time.DateTime; val timestamp = new DateTime(1380822000000L); 

正如你在这里看到的那样,EPOCH已经实施了几毫秒。

您应该将该数字作为长整数传递以获得所需的结果,可以传递字符串但应该是可解析的。

 val a = new Date(1380822000000L).toString(); 

查看参考资料以获取更多信息。

你收到一个警告:

警告:有1个弃用警告;

因为不推荐使用new Date(String)

其次, new Date("1380822000000")无效转换为date String ,因为它以milli seconds ,这是一个long值。

你必须这样做

  vav a= new Date(1380822000000L).toString(); 

TL;博士

Java语法中的示例。

在UTC中:

 Instant.ofEpochMilli( Long.parseLong( "1380822000000" ) ) .toString() 

2013-10-03T17:40:00Z

在特定时区:

 Instant.ofEpochMilli( Long.parseLong( "1380822000000" ) ) .atZone( ZoneId.of( "America/Montreal" ) ) .toString() 

2013-10-03T13:40:00-04:00 [美国/蒙特利尔]

本地化:

 Instant.ofEpochMilli( Long.parseLong( "1380822000000" ) ) .atZone( ZoneId.of( "America/Montreal" ) .format( DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ) .withLocale( Locale.CANADA_FRENCH ) ) 

>

java.time

与旧版本Java捆绑在一起的臭名昭着的java.util.Date/.Calendar类现在已被Java 8及更高版本中内置的新java.time框架所取代。

Instant代表时间轴上的一个时刻。 ZonedDateTime为Instant分配时区。

请注意,java.time的分辨率为纳秒,而早期的框架的粒度为毫秒。 为方便起见,您将在下面看到提供的ofEpochMilli方法,但java.time不限于毫秒。

 Instant instant = Instant.ofEpochMilli( 1380822000000L ) ; // Note the "L" on the long integer literal. 

如果输入是String,则解析为long整数值。 请参阅Long.parseLong静态方法。

 Instant instant = Instant.ofEpochMilli( Long.parseLong( "1380822000000" ) ) ; 

您可以调整到特定时区。

 ZoneId zone = ZoneId.of( "America/Montreal" ) ; ZonedDateTime zdt = instant.atZone( zone ); 

格式

ZonedDateTime的toString方法将以ISO 8601标准的格式生成日期时间值的字符串表示。 在“formatter”中搜索StackOverflow,了解如何生成其他字符串。