Easy way to get a readable date from a long (timestamp) in scala












1















My question is simple



If I do:



var start = System.currentTimeMillis


I get:



start: Long = 1542717303659


How should I do to get a string looking to something readable for a human eye?:



ex: "20/11/2018 13:30:10"










share|improve this question



























    1















    My question is simple



    If I do:



    var start = System.currentTimeMillis


    I get:



    start: Long = 1542717303659


    How should I do to get a string looking to something readable for a human eye?:



    ex: "20/11/2018 13:30:10"










    share|improve this question

























      1












      1








      1








      My question is simple



      If I do:



      var start = System.currentTimeMillis


      I get:



      start: Long = 1542717303659


      How should I do to get a string looking to something readable for a human eye?:



      ex: "20/11/2018 13:30:10"










      share|improve this question














      My question is simple



      If I do:



      var start = System.currentTimeMillis


      I get:



      start: Long = 1542717303659


      How should I do to get a string looking to something readable for a human eye?:



      ex: "20/11/2018 13:30:10"







      scala apache






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 12:46









      AnnesoAnneso

      769




      769
























          3 Answers
          3






          active

          oldest

          votes


















          3














          You can use the java.time library like:



            val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss")
          formatter.format(LocalDateTime.now)


          If you only have the timestamp, this solution gets a bit more complex:



          formatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), ZoneId.of("UTC")))


          Then I would take java.text.SimpleDateFormat :



          new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(System.currentTimeMillis())


          To get back to the Timestamp:



          new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse( "02/12/2012 12:23:44" ).getTime





          share|improve this answer


























          • Thank you!!!! :-).

            – Anneso
            Nov 21 '18 at 9:02













          • Do you have any idea on how to do the opposite? create a timestamp long with a string date?

            – Anneso
            Nov 21 '18 at 13:17











          • @Anneso see my edited answer

            – pme
            Nov 21 '18 at 13:51











          • great thank you again! :-)

            – Anneso
            Nov 21 '18 at 15:57



















          1














          Don't overthink it: nothing wrong with just new Date(start).toString






          share|improve this answer
























          • Thank you!! :-) But this give me a date, I really need a time with it to evaluate how long a scipt take to run. :-)

            – Anneso
            Nov 21 '18 at 9:03











          • Did you try it? :) (it gives you time too). But if all you need is evaluate what the script took, you are probably better off with Long to begin with. Just subtract start from the end, and you get the number of milliseconds.

            – Dima
            Nov 21 '18 at 12:40











          • Hi yes I did the substract but I wanted to print the datetime too. But I try your solution and get only a date...

            – Anneso
            Nov 21 '18 at 13:11











          • scala> var start = System.currentTimeMillis start: Long = 1542805880478 scala> new Date(start).toString res23: String = 2018-11-21

            – Anneso
            Nov 21 '18 at 13:11











          • Hmm ... Wrong Date? Try new java.util.Date(1542805880478l).toString

            – Dima
            Nov 21 '18 at 16:27



















          1














          You can use java.time library and get it in readable format as below one-liner.



          scala>  java.time.LocalDateTime.ofEpochSecond(System.currentTimeMillis/1000,0,java.time.ZoneOffset.UTC)
          res31: java.time.LocalDateTime = 2018-11-21T18:37:49

          scala>


          I'm just diving the Milliseconds by 1000, so that we get EpochSecond.



          For getting it back,



          scala> java.time.LocalDateTime.parse("2018-11-21T18:41:29").toEpochSecond(java.time.ZoneOffset.UTC)
          res40: Long = 1542825689

          scala>





          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393310%2feasy-way-to-get-a-readable-date-from-a-long-timestamp-in-scala%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            You can use the java.time library like:



              val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss")
            formatter.format(LocalDateTime.now)


            If you only have the timestamp, this solution gets a bit more complex:



            formatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), ZoneId.of("UTC")))


            Then I would take java.text.SimpleDateFormat :



            new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(System.currentTimeMillis())


            To get back to the Timestamp:



            new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse( "02/12/2012 12:23:44" ).getTime





            share|improve this answer


























            • Thank you!!!! :-).

              – Anneso
              Nov 21 '18 at 9:02













            • Do you have any idea on how to do the opposite? create a timestamp long with a string date?

              – Anneso
              Nov 21 '18 at 13:17











            • @Anneso see my edited answer

              – pme
              Nov 21 '18 at 13:51











            • great thank you again! :-)

              – Anneso
              Nov 21 '18 at 15:57
















            3














            You can use the java.time library like:



              val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss")
            formatter.format(LocalDateTime.now)


            If you only have the timestamp, this solution gets a bit more complex:



            formatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), ZoneId.of("UTC")))


            Then I would take java.text.SimpleDateFormat :



            new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(System.currentTimeMillis())


            To get back to the Timestamp:



            new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse( "02/12/2012 12:23:44" ).getTime





            share|improve this answer


























            • Thank you!!!! :-).

              – Anneso
              Nov 21 '18 at 9:02













            • Do you have any idea on how to do the opposite? create a timestamp long with a string date?

              – Anneso
              Nov 21 '18 at 13:17











            • @Anneso see my edited answer

              – pme
              Nov 21 '18 at 13:51











            • great thank you again! :-)

              – Anneso
              Nov 21 '18 at 15:57














            3












            3








            3







            You can use the java.time library like:



              val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss")
            formatter.format(LocalDateTime.now)


            If you only have the timestamp, this solution gets a bit more complex:



            formatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), ZoneId.of("UTC")))


            Then I would take java.text.SimpleDateFormat :



            new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(System.currentTimeMillis())


            To get back to the Timestamp:



            new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse( "02/12/2012 12:23:44" ).getTime





            share|improve this answer















            You can use the java.time library like:



              val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss")
            formatter.format(LocalDateTime.now)


            If you only have the timestamp, this solution gets a bit more complex:



            formatter.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), ZoneId.of("UTC")))


            Then I would take java.text.SimpleDateFormat :



            new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(System.currentTimeMillis())


            To get back to the Timestamp:



            new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse( "02/12/2012 12:23:44" ).getTime






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 '18 at 13:51

























            answered Nov 20 '18 at 12:54









            pmepme

            2,45511224




            2,45511224













            • Thank you!!!! :-).

              – Anneso
              Nov 21 '18 at 9:02













            • Do you have any idea on how to do the opposite? create a timestamp long with a string date?

              – Anneso
              Nov 21 '18 at 13:17











            • @Anneso see my edited answer

              – pme
              Nov 21 '18 at 13:51











            • great thank you again! :-)

              – Anneso
              Nov 21 '18 at 15:57



















            • Thank you!!!! :-).

              – Anneso
              Nov 21 '18 at 9:02













            • Do you have any idea on how to do the opposite? create a timestamp long with a string date?

              – Anneso
              Nov 21 '18 at 13:17











            • @Anneso see my edited answer

              – pme
              Nov 21 '18 at 13:51











            • great thank you again! :-)

              – Anneso
              Nov 21 '18 at 15:57

















            Thank you!!!! :-).

            – Anneso
            Nov 21 '18 at 9:02







            Thank you!!!! :-).

            – Anneso
            Nov 21 '18 at 9:02















            Do you have any idea on how to do the opposite? create a timestamp long with a string date?

            – Anneso
            Nov 21 '18 at 13:17





            Do you have any idea on how to do the opposite? create a timestamp long with a string date?

            – Anneso
            Nov 21 '18 at 13:17













            @Anneso see my edited answer

            – pme
            Nov 21 '18 at 13:51





            @Anneso see my edited answer

            – pme
            Nov 21 '18 at 13:51













            great thank you again! :-)

            – Anneso
            Nov 21 '18 at 15:57





            great thank you again! :-)

            – Anneso
            Nov 21 '18 at 15:57













            1














            Don't overthink it: nothing wrong with just new Date(start).toString






            share|improve this answer
























            • Thank you!! :-) But this give me a date, I really need a time with it to evaluate how long a scipt take to run. :-)

              – Anneso
              Nov 21 '18 at 9:03











            • Did you try it? :) (it gives you time too). But if all you need is evaluate what the script took, you are probably better off with Long to begin with. Just subtract start from the end, and you get the number of milliseconds.

              – Dima
              Nov 21 '18 at 12:40











            • Hi yes I did the substract but I wanted to print the datetime too. But I try your solution and get only a date...

              – Anneso
              Nov 21 '18 at 13:11











            • scala> var start = System.currentTimeMillis start: Long = 1542805880478 scala> new Date(start).toString res23: String = 2018-11-21

              – Anneso
              Nov 21 '18 at 13:11











            • Hmm ... Wrong Date? Try new java.util.Date(1542805880478l).toString

              – Dima
              Nov 21 '18 at 16:27
















            1














            Don't overthink it: nothing wrong with just new Date(start).toString






            share|improve this answer
























            • Thank you!! :-) But this give me a date, I really need a time with it to evaluate how long a scipt take to run. :-)

              – Anneso
              Nov 21 '18 at 9:03











            • Did you try it? :) (it gives you time too). But if all you need is evaluate what the script took, you are probably better off with Long to begin with. Just subtract start from the end, and you get the number of milliseconds.

              – Dima
              Nov 21 '18 at 12:40











            • Hi yes I did the substract but I wanted to print the datetime too. But I try your solution and get only a date...

              – Anneso
              Nov 21 '18 at 13:11











            • scala> var start = System.currentTimeMillis start: Long = 1542805880478 scala> new Date(start).toString res23: String = 2018-11-21

              – Anneso
              Nov 21 '18 at 13:11











            • Hmm ... Wrong Date? Try new java.util.Date(1542805880478l).toString

              – Dima
              Nov 21 '18 at 16:27














            1












            1








            1







            Don't overthink it: nothing wrong with just new Date(start).toString






            share|improve this answer













            Don't overthink it: nothing wrong with just new Date(start).toString







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 '18 at 13:13









            DimaDima

            24.5k32235




            24.5k32235













            • Thank you!! :-) But this give me a date, I really need a time with it to evaluate how long a scipt take to run. :-)

              – Anneso
              Nov 21 '18 at 9:03











            • Did you try it? :) (it gives you time too). But if all you need is evaluate what the script took, you are probably better off with Long to begin with. Just subtract start from the end, and you get the number of milliseconds.

              – Dima
              Nov 21 '18 at 12:40











            • Hi yes I did the substract but I wanted to print the datetime too. But I try your solution and get only a date...

              – Anneso
              Nov 21 '18 at 13:11











            • scala> var start = System.currentTimeMillis start: Long = 1542805880478 scala> new Date(start).toString res23: String = 2018-11-21

              – Anneso
              Nov 21 '18 at 13:11











            • Hmm ... Wrong Date? Try new java.util.Date(1542805880478l).toString

              – Dima
              Nov 21 '18 at 16:27



















            • Thank you!! :-) But this give me a date, I really need a time with it to evaluate how long a scipt take to run. :-)

              – Anneso
              Nov 21 '18 at 9:03











            • Did you try it? :) (it gives you time too). But if all you need is evaluate what the script took, you are probably better off with Long to begin with. Just subtract start from the end, and you get the number of milliseconds.

              – Dima
              Nov 21 '18 at 12:40











            • Hi yes I did the substract but I wanted to print the datetime too. But I try your solution and get only a date...

              – Anneso
              Nov 21 '18 at 13:11











            • scala> var start = System.currentTimeMillis start: Long = 1542805880478 scala> new Date(start).toString res23: String = 2018-11-21

              – Anneso
              Nov 21 '18 at 13:11











            • Hmm ... Wrong Date? Try new java.util.Date(1542805880478l).toString

              – Dima
              Nov 21 '18 at 16:27

















            Thank you!! :-) But this give me a date, I really need a time with it to evaluate how long a scipt take to run. :-)

            – Anneso
            Nov 21 '18 at 9:03





            Thank you!! :-) But this give me a date, I really need a time with it to evaluate how long a scipt take to run. :-)

            – Anneso
            Nov 21 '18 at 9:03













            Did you try it? :) (it gives you time too). But if all you need is evaluate what the script took, you are probably better off with Long to begin with. Just subtract start from the end, and you get the number of milliseconds.

            – Dima
            Nov 21 '18 at 12:40





            Did you try it? :) (it gives you time too). But if all you need is evaluate what the script took, you are probably better off with Long to begin with. Just subtract start from the end, and you get the number of milliseconds.

            – Dima
            Nov 21 '18 at 12:40













            Hi yes I did the substract but I wanted to print the datetime too. But I try your solution and get only a date...

            – Anneso
            Nov 21 '18 at 13:11





            Hi yes I did the substract but I wanted to print the datetime too. But I try your solution and get only a date...

            – Anneso
            Nov 21 '18 at 13:11













            scala> var start = System.currentTimeMillis start: Long = 1542805880478 scala> new Date(start).toString res23: String = 2018-11-21

            – Anneso
            Nov 21 '18 at 13:11





            scala> var start = System.currentTimeMillis start: Long = 1542805880478 scala> new Date(start).toString res23: String = 2018-11-21

            – Anneso
            Nov 21 '18 at 13:11













            Hmm ... Wrong Date? Try new java.util.Date(1542805880478l).toString

            – Dima
            Nov 21 '18 at 16:27





            Hmm ... Wrong Date? Try new java.util.Date(1542805880478l).toString

            – Dima
            Nov 21 '18 at 16:27











            1














            You can use java.time library and get it in readable format as below one-liner.



            scala>  java.time.LocalDateTime.ofEpochSecond(System.currentTimeMillis/1000,0,java.time.ZoneOffset.UTC)
            res31: java.time.LocalDateTime = 2018-11-21T18:37:49

            scala>


            I'm just diving the Milliseconds by 1000, so that we get EpochSecond.



            For getting it back,



            scala> java.time.LocalDateTime.parse("2018-11-21T18:41:29").toEpochSecond(java.time.ZoneOffset.UTC)
            res40: Long = 1542825689

            scala>





            share|improve this answer






























              1














              You can use java.time library and get it in readable format as below one-liner.



              scala>  java.time.LocalDateTime.ofEpochSecond(System.currentTimeMillis/1000,0,java.time.ZoneOffset.UTC)
              res31: java.time.LocalDateTime = 2018-11-21T18:37:49

              scala>


              I'm just diving the Milliseconds by 1000, so that we get EpochSecond.



              For getting it back,



              scala> java.time.LocalDateTime.parse("2018-11-21T18:41:29").toEpochSecond(java.time.ZoneOffset.UTC)
              res40: Long = 1542825689

              scala>





              share|improve this answer




























                1












                1








                1







                You can use java.time library and get it in readable format as below one-liner.



                scala>  java.time.LocalDateTime.ofEpochSecond(System.currentTimeMillis/1000,0,java.time.ZoneOffset.UTC)
                res31: java.time.LocalDateTime = 2018-11-21T18:37:49

                scala>


                I'm just diving the Milliseconds by 1000, so that we get EpochSecond.



                For getting it back,



                scala> java.time.LocalDateTime.parse("2018-11-21T18:41:29").toEpochSecond(java.time.ZoneOffset.UTC)
                res40: Long = 1542825689

                scala>





                share|improve this answer















                You can use java.time library and get it in readable format as below one-liner.



                scala>  java.time.LocalDateTime.ofEpochSecond(System.currentTimeMillis/1000,0,java.time.ZoneOffset.UTC)
                res31: java.time.LocalDateTime = 2018-11-21T18:37:49

                scala>


                I'm just diving the Milliseconds by 1000, so that we get EpochSecond.



                For getting it back,



                scala> java.time.LocalDateTime.parse("2018-11-21T18:41:29").toEpochSecond(java.time.ZoneOffset.UTC)
                res40: Long = 1542825689

                scala>






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 21 '18 at 18:45

























                answered Nov 21 '18 at 18:39









                stack0114106stack0114106

                2,9121417




                2,9121417






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393310%2feasy-way-to-get-a-readable-date-from-a-long-timestamp-in-scala%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                    How to fix TextFormField cause rebuild widget in Flutter