Convert from Unix Timestamp to date in Groovy












0















I have a date in unix timestamp and I want to convert it to human readable...



 def dateUnix = 1486146877214
Date dateObj = new Date( ((long)dateUnix) * 1000 )
def cleanDate = new SimpleDateFormat('yyyy-MM-dd').format(dateObj)
println "clean date $cleanDate"


This gives me..



  clean date 49064-02-13


where I was expecting "2017-02-03".



What am I doing wrong?



I even casted the timestamp to a long explicitly as suggested in this answer.










share|improve this question























  • Try this answer stackoverflow.com/questions/47403542/…

    – IamVickyAV
    Nov 21 '18 at 6:41
















0















I have a date in unix timestamp and I want to convert it to human readable...



 def dateUnix = 1486146877214
Date dateObj = new Date( ((long)dateUnix) * 1000 )
def cleanDate = new SimpleDateFormat('yyyy-MM-dd').format(dateObj)
println "clean date $cleanDate"


This gives me..



  clean date 49064-02-13


where I was expecting "2017-02-03".



What am I doing wrong?



I even casted the timestamp to a long explicitly as suggested in this answer.










share|improve this question























  • Try this answer stackoverflow.com/questions/47403542/…

    – IamVickyAV
    Nov 21 '18 at 6:41














0












0








0








I have a date in unix timestamp and I want to convert it to human readable...



 def dateUnix = 1486146877214
Date dateObj = new Date( ((long)dateUnix) * 1000 )
def cleanDate = new SimpleDateFormat('yyyy-MM-dd').format(dateObj)
println "clean date $cleanDate"


This gives me..



  clean date 49064-02-13


where I was expecting "2017-02-03".



What am I doing wrong?



I even casted the timestamp to a long explicitly as suggested in this answer.










share|improve this question














I have a date in unix timestamp and I want to convert it to human readable...



 def dateUnix = 1486146877214
Date dateObj = new Date( ((long)dateUnix) * 1000 )
def cleanDate = new SimpleDateFormat('yyyy-MM-dd').format(dateObj)
println "clean date $cleanDate"


This gives me..



  clean date 49064-02-13


where I was expecting "2017-02-03".



What am I doing wrong?



I even casted the timestamp to a long explicitly as suggested in this answer.







date groovy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 1:15









AbuMariamAbuMariam

1,04062340




1,04062340













  • Try this answer stackoverflow.com/questions/47403542/…

    – IamVickyAV
    Nov 21 '18 at 6:41



















  • Try this answer stackoverflow.com/questions/47403542/…

    – IamVickyAV
    Nov 21 '18 at 6:41

















Try this answer stackoverflow.com/questions/47403542/…

– IamVickyAV
Nov 21 '18 at 6:41





Try this answer stackoverflow.com/questions/47403542/…

– IamVickyAV
Nov 21 '18 at 6:41












2 Answers
2






active

oldest

votes


















1














Don't multiply by 1000 - new Date(long) is in millisecs not microsecs






share|improve this answer

































    0














    There is default way to convert Unix time stamp to Java date:



    import java.time.Instant
    import java.util.Date

    def unixTimeStamp = 1486146877214
    Date date = Date.from(Instant.ofEpochSecond(unixTimeStamp))





    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%2f53403959%2fconvert-from-unix-timestamp-to-date-in-groovy%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Don't multiply by 1000 - new Date(long) is in millisecs not microsecs






      share|improve this answer






























        1














        Don't multiply by 1000 - new Date(long) is in millisecs not microsecs






        share|improve this answer




























          1












          1








          1







          Don't multiply by 1000 - new Date(long) is in millisecs not microsecs






          share|improve this answer















          Don't multiply by 1000 - new Date(long) is in millisecs not microsecs







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 22:22

























          answered Nov 21 '18 at 5:01









          Michael RutherfurdMichael Rutherfurd

          10.1k42235




          10.1k42235

























              0














              There is default way to convert Unix time stamp to Java date:



              import java.time.Instant
              import java.util.Date

              def unixTimeStamp = 1486146877214
              Date date = Date.from(Instant.ofEpochSecond(unixTimeStamp))





              share|improve this answer




























                0














                There is default way to convert Unix time stamp to Java date:



                import java.time.Instant
                import java.util.Date

                def unixTimeStamp = 1486146877214
                Date date = Date.from(Instant.ofEpochSecond(unixTimeStamp))





                share|improve this answer


























                  0












                  0








                  0







                  There is default way to convert Unix time stamp to Java date:



                  import java.time.Instant
                  import java.util.Date

                  def unixTimeStamp = 1486146877214
                  Date date = Date.from(Instant.ofEpochSecond(unixTimeStamp))





                  share|improve this answer













                  There is default way to convert Unix time stamp to Java date:



                  import java.time.Instant
                  import java.util.Date

                  def unixTimeStamp = 1486146877214
                  Date date = Date.from(Instant.ofEpochSecond(unixTimeStamp))






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 7:10









                  Evgeny SmirnovEvgeny Smirnov

                  2,0361517




                  2,0361517






























                      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%2f53403959%2fconvert-from-unix-timestamp-to-date-in-groovy%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







                      這個網誌中的熱門文章

                      Xamarin.form Move up view when keyboard appear

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Anylogic : not able to use stopDelay()