Kotlin convert TimeStamp to DateTime












3















I'm trying to find out how I can convert timestamp to datetime in Kotlin, this is very simple in Java but I cant find any equivalent of it in Kotlin.



For example: epoch timestamp (seconds since 1070-01-01) 1510500494 ==> DateTime object 2017-11-12 03:28:14.



Is there any solution for this in Kotlin or do I have to use Java syntax in Kotlin? Please give me a simple sample to show how I can resolve this problem. Thanks in advance.



this link is not an answer to my question










share|improve this question

























  • sorry for that I've remove the flag .

    – moath naji
    Nov 12 '17 at 15:08













  • @moathnaji i'm trying to convert timestamp not UTC

    – DolDurma
    Nov 12 '17 at 15:10











  • You should provide the Java code you would use that is "very simple" to make it easier for someone to show you the Kotlin equivalent.

    – dominicoder
    Nov 12 '17 at 15:15






  • 2





    That's not what I'm suggesting. I'm just suggesting you ask IntelliJ to convert the Java code to Kotlin. But if you're not familiar with Kotlin, why don't you become familiar with it before using Kotlin? And you still haven't posted the very simple Java code.

    – JB Nizet
    Nov 12 '17 at 16:00






  • 2





    That is irrelevant. You still haven't posted the very simple Java code.

    – JB Nizet
    Nov 12 '17 at 16:08
















3















I'm trying to find out how I can convert timestamp to datetime in Kotlin, this is very simple in Java but I cant find any equivalent of it in Kotlin.



For example: epoch timestamp (seconds since 1070-01-01) 1510500494 ==> DateTime object 2017-11-12 03:28:14.



Is there any solution for this in Kotlin or do I have to use Java syntax in Kotlin? Please give me a simple sample to show how I can resolve this problem. Thanks in advance.



this link is not an answer to my question










share|improve this question

























  • sorry for that I've remove the flag .

    – moath naji
    Nov 12 '17 at 15:08













  • @moathnaji i'm trying to convert timestamp not UTC

    – DolDurma
    Nov 12 '17 at 15:10











  • You should provide the Java code you would use that is "very simple" to make it easier for someone to show you the Kotlin equivalent.

    – dominicoder
    Nov 12 '17 at 15:15






  • 2





    That's not what I'm suggesting. I'm just suggesting you ask IntelliJ to convert the Java code to Kotlin. But if you're not familiar with Kotlin, why don't you become familiar with it before using Kotlin? And you still haven't posted the very simple Java code.

    – JB Nizet
    Nov 12 '17 at 16:00






  • 2





    That is irrelevant. You still haven't posted the very simple Java code.

    – JB Nizet
    Nov 12 '17 at 16:08














3












3








3


0






I'm trying to find out how I can convert timestamp to datetime in Kotlin, this is very simple in Java but I cant find any equivalent of it in Kotlin.



For example: epoch timestamp (seconds since 1070-01-01) 1510500494 ==> DateTime object 2017-11-12 03:28:14.



Is there any solution for this in Kotlin or do I have to use Java syntax in Kotlin? Please give me a simple sample to show how I can resolve this problem. Thanks in advance.



this link is not an answer to my question










share|improve this question
















I'm trying to find out how I can convert timestamp to datetime in Kotlin, this is very simple in Java but I cant find any equivalent of it in Kotlin.



For example: epoch timestamp (seconds since 1070-01-01) 1510500494 ==> DateTime object 2017-11-12 03:28:14.



Is there any solution for this in Kotlin or do I have to use Java syntax in Kotlin? Please give me a simple sample to show how I can resolve this problem. Thanks in advance.



this link is not an answer to my question







java kotlin timestamp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 13 '18 at 18:14









t0r0X

1,8721620




1,8721620










asked Nov 12 '17 at 15:01









DolDurmaDolDurma

3,3841266136




3,3841266136













  • sorry for that I've remove the flag .

    – moath naji
    Nov 12 '17 at 15:08













  • @moathnaji i'm trying to convert timestamp not UTC

    – DolDurma
    Nov 12 '17 at 15:10











  • You should provide the Java code you would use that is "very simple" to make it easier for someone to show you the Kotlin equivalent.

    – dominicoder
    Nov 12 '17 at 15:15






  • 2





    That's not what I'm suggesting. I'm just suggesting you ask IntelliJ to convert the Java code to Kotlin. But if you're not familiar with Kotlin, why don't you become familiar with it before using Kotlin? And you still haven't posted the very simple Java code.

    – JB Nizet
    Nov 12 '17 at 16:00






  • 2





    That is irrelevant. You still haven't posted the very simple Java code.

    – JB Nizet
    Nov 12 '17 at 16:08



















  • sorry for that I've remove the flag .

    – moath naji
    Nov 12 '17 at 15:08













  • @moathnaji i'm trying to convert timestamp not UTC

    – DolDurma
    Nov 12 '17 at 15:10











  • You should provide the Java code you would use that is "very simple" to make it easier for someone to show you the Kotlin equivalent.

    – dominicoder
    Nov 12 '17 at 15:15






  • 2





    That's not what I'm suggesting. I'm just suggesting you ask IntelliJ to convert the Java code to Kotlin. But if you're not familiar with Kotlin, why don't you become familiar with it before using Kotlin? And you still haven't posted the very simple Java code.

    – JB Nizet
    Nov 12 '17 at 16:00






  • 2





    That is irrelevant. You still haven't posted the very simple Java code.

    – JB Nizet
    Nov 12 '17 at 16:08

















sorry for that I've remove the flag .

– moath naji
Nov 12 '17 at 15:08







sorry for that I've remove the flag .

– moath naji
Nov 12 '17 at 15:08















@moathnaji i'm trying to convert timestamp not UTC

– DolDurma
Nov 12 '17 at 15:10





@moathnaji i'm trying to convert timestamp not UTC

– DolDurma
Nov 12 '17 at 15:10













You should provide the Java code you would use that is "very simple" to make it easier for someone to show you the Kotlin equivalent.

– dominicoder
Nov 12 '17 at 15:15





You should provide the Java code you would use that is "very simple" to make it easier for someone to show you the Kotlin equivalent.

– dominicoder
Nov 12 '17 at 15:15




2




2





That's not what I'm suggesting. I'm just suggesting you ask IntelliJ to convert the Java code to Kotlin. But if you're not familiar with Kotlin, why don't you become familiar with it before using Kotlin? And you still haven't posted the very simple Java code.

– JB Nizet
Nov 12 '17 at 16:00





That's not what I'm suggesting. I'm just suggesting you ask IntelliJ to convert the Java code to Kotlin. But if you're not familiar with Kotlin, why don't you become familiar with it before using Kotlin? And you still haven't posted the very simple Java code.

– JB Nizet
Nov 12 '17 at 16:00




2




2





That is irrelevant. You still haven't posted the very simple Java code.

– JB Nizet
Nov 12 '17 at 16:08





That is irrelevant. You still haven't posted the very simple Java code.

– JB Nizet
Nov 12 '17 at 16:08












4 Answers
4






active

oldest

votes


















5














private fun getDateTime(s: String): String? {
try {
val sdf = SimpleDateFormat("MM/dd/yyyy")
val netDate = Date(Long.parseLong(s))
return sdf.format(netDate)
} catch (e: Exception) {
return e.toString()
}
}





share|improve this answer
























  • val netDate = Date(s.toLong() * 1000) worked for me.

    – temp_
    Feb 22 '18 at 17:43













  • Is this to current locale?

    – JGuo
    Jul 27 '18 at 14:13



















2














It's actually just like Java. Try this:



val stamp = Timestamp(System.currentTimeMillis())
val date = Date(stamp.getTime())
println(date)





share|improve this answer































    2














    Although it's Kotlin, you still have to use the Java API. An example for Java 8+ APIs converting the value 1510500494 which you mentioned in the question comments:



    import java.time.*

    val dt = Instant.ofEpochSecond(1510500494).atZone(ZoneId.systemDefault()).toLocalDateTime()





    share|improve this answer































      0














        fun stringtoDate(dates: String): Date {
      val sdf = SimpleDateFormat("EEE, MMM dd yyyy",
      Locale.ENGLISH)
      var date: Date? = null
      try {
      date = sdf.parse(dates)
      println(date)
      } catch (e: ParseException) {
      e.printStackTrace()
      }
      return date!!
      }





      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%2f47250263%2fkotlin-convert-timestamp-to-datetime%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        5














        private fun getDateTime(s: String): String? {
        try {
        val sdf = SimpleDateFormat("MM/dd/yyyy")
        val netDate = Date(Long.parseLong(s))
        return sdf.format(netDate)
        } catch (e: Exception) {
        return e.toString()
        }
        }





        share|improve this answer
























        • val netDate = Date(s.toLong() * 1000) worked for me.

          – temp_
          Feb 22 '18 at 17:43













        • Is this to current locale?

          – JGuo
          Jul 27 '18 at 14:13
















        5














        private fun getDateTime(s: String): String? {
        try {
        val sdf = SimpleDateFormat("MM/dd/yyyy")
        val netDate = Date(Long.parseLong(s))
        return sdf.format(netDate)
        } catch (e: Exception) {
        return e.toString()
        }
        }





        share|improve this answer
























        • val netDate = Date(s.toLong() * 1000) worked for me.

          – temp_
          Feb 22 '18 at 17:43













        • Is this to current locale?

          – JGuo
          Jul 27 '18 at 14:13














        5












        5








        5







        private fun getDateTime(s: String): String? {
        try {
        val sdf = SimpleDateFormat("MM/dd/yyyy")
        val netDate = Date(Long.parseLong(s))
        return sdf.format(netDate)
        } catch (e: Exception) {
        return e.toString()
        }
        }





        share|improve this answer













        private fun getDateTime(s: String): String? {
        try {
        val sdf = SimpleDateFormat("MM/dd/yyyy")
        val netDate = Date(Long.parseLong(s))
        return sdf.format(netDate)
        } catch (e: Exception) {
        return e.toString()
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '17 at 9:48









        arjun shresthaarjun shrestha

        461210




        461210













        • val netDate = Date(s.toLong() * 1000) worked for me.

          – temp_
          Feb 22 '18 at 17:43













        • Is this to current locale?

          – JGuo
          Jul 27 '18 at 14:13



















        • val netDate = Date(s.toLong() * 1000) worked for me.

          – temp_
          Feb 22 '18 at 17:43













        • Is this to current locale?

          – JGuo
          Jul 27 '18 at 14:13

















        val netDate = Date(s.toLong() * 1000) worked for me.

        – temp_
        Feb 22 '18 at 17:43







        val netDate = Date(s.toLong() * 1000) worked for me.

        – temp_
        Feb 22 '18 at 17:43















        Is this to current locale?

        – JGuo
        Jul 27 '18 at 14:13





        Is this to current locale?

        – JGuo
        Jul 27 '18 at 14:13













        2














        It's actually just like Java. Try this:



        val stamp = Timestamp(System.currentTimeMillis())
        val date = Date(stamp.getTime())
        println(date)





        share|improve this answer




























          2














          It's actually just like Java. Try this:



          val stamp = Timestamp(System.currentTimeMillis())
          val date = Date(stamp.getTime())
          println(date)





          share|improve this answer


























            2












            2








            2







            It's actually just like Java. Try this:



            val stamp = Timestamp(System.currentTimeMillis())
            val date = Date(stamp.getTime())
            println(date)





            share|improve this answer













            It's actually just like Java. Try this:



            val stamp = Timestamp(System.currentTimeMillis())
            val date = Date(stamp.getTime())
            println(date)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 29 '18 at 20:19









            Javier García ManzanoJavier García Manzano

            253213




            253213























                2














                Although it's Kotlin, you still have to use the Java API. An example for Java 8+ APIs converting the value 1510500494 which you mentioned in the question comments:



                import java.time.*

                val dt = Instant.ofEpochSecond(1510500494).atZone(ZoneId.systemDefault()).toLocalDateTime()





                share|improve this answer




























                  2














                  Although it's Kotlin, you still have to use the Java API. An example for Java 8+ APIs converting the value 1510500494 which you mentioned in the question comments:



                  import java.time.*

                  val dt = Instant.ofEpochSecond(1510500494).atZone(ZoneId.systemDefault()).toLocalDateTime()





                  share|improve this answer


























                    2












                    2








                    2







                    Although it's Kotlin, you still have to use the Java API. An example for Java 8+ APIs converting the value 1510500494 which you mentioned in the question comments:



                    import java.time.*

                    val dt = Instant.ofEpochSecond(1510500494).atZone(ZoneId.systemDefault()).toLocalDateTime()





                    share|improve this answer













                    Although it's Kotlin, you still have to use the Java API. An example for Java 8+ APIs converting the value 1510500494 which you mentioned in the question comments:



                    import java.time.*

                    val dt = Instant.ofEpochSecond(1510500494).atZone(ZoneId.systemDefault()).toLocalDateTime()






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 13 '18 at 15:02









                    t0r0Xt0r0X

                    1,8721620




                    1,8721620























                        0














                          fun stringtoDate(dates: String): Date {
                        val sdf = SimpleDateFormat("EEE, MMM dd yyyy",
                        Locale.ENGLISH)
                        var date: Date? = null
                        try {
                        date = sdf.parse(dates)
                        println(date)
                        } catch (e: ParseException) {
                        e.printStackTrace()
                        }
                        return date!!
                        }





                        share|improve this answer




























                          0














                            fun stringtoDate(dates: String): Date {
                          val sdf = SimpleDateFormat("EEE, MMM dd yyyy",
                          Locale.ENGLISH)
                          var date: Date? = null
                          try {
                          date = sdf.parse(dates)
                          println(date)
                          } catch (e: ParseException) {
                          e.printStackTrace()
                          }
                          return date!!
                          }





                          share|improve this answer


























                            0












                            0








                            0







                              fun stringtoDate(dates: String): Date {
                            val sdf = SimpleDateFormat("EEE, MMM dd yyyy",
                            Locale.ENGLISH)
                            var date: Date? = null
                            try {
                            date = sdf.parse(dates)
                            println(date)
                            } catch (e: ParseException) {
                            e.printStackTrace()
                            }
                            return date!!
                            }





                            share|improve this answer













                              fun stringtoDate(dates: String): Date {
                            val sdf = SimpleDateFormat("EEE, MMM dd yyyy",
                            Locale.ENGLISH)
                            var date: Date? = null
                            try {
                            date = sdf.parse(dates)
                            println(date)
                            } catch (e: ParseException) {
                            e.printStackTrace()
                            }
                            return date!!
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jul 18 '18 at 5:14









                            Umesh MaharjanUmesh Maharjan

                            1763




                            1763






























                                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%2f47250263%2fkotlin-convert-timestamp-to-datetime%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

                                Bressuire

                                Vorschmack

                                Quarantine