Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java time to the normal format

Tags:

java

scala

I have Java time 1380822000000. And I want to convert to something I can read:

import java.util.Date

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

causes an exception

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.<init>(Date.java:272)
    at .<init>(<console>:9)
    at .<clinit>(<console>)
    at .<init>(<console>:7)
    at .<clinit>(<console>)
    at $print(<console>)
    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)

How do I convert it to the normal human representation?

like image 844
Incerteza Avatar asked Feb 26 '26 20:02

Incerteza


2 Answers

You should pass that number as a long integer to get desired result, string can be passed but that should be parse-able.

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

Look at the reference for more info.

like image 91
Iván Pérez Avatar answered Mar 01 '26 10:03

Iván Pérez


Or do the better thing and use org.jodatime;

import org.joda.time.DateTime;

val timestamp = new DateTime(1380822000000L);

as you can see here, a miliseconds since EPOCH is already implemented.

like image 22
flavian Avatar answered Mar 01 '26 09:03

flavian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!