Convert microseconds to milliseconds without loss of precision and format as date-time in Java











up vote
1
down vote

favorite












How can I convert incoming long time value in microseconds into milliseconds and then format that to data time stamp as below:




yyyyMMdd-HH:mm:ss.SSS




I am using Java SimpleDateFormat to format the long value to the timestamp. But, converting the microseconds to milliseconds seems problem as I am loosing the value.



SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HH:mm:ss.SSS");


To convert microsecond to millisecond, I am using the TimeUnit:



long micro //microseconds
long milli = TimeUnit.MILLISECONDS.convert(micro, TimeUnit.MICROSECONDS);


What is the right way to convert the value without loosing any data?










share|improve this question
























  • What's you current input value and what's your expected output value?
    – MadProgrammer
    Nov 5 at 3:56










  • What does you mean with "I am loosing the value"? Can you provide a runnable Minimal, Complete, and Verifiable example, with appropriate sampel values for the microseconds, that shows what you wanted to happen and what happened instead?
    – Erwin Bolwidt
    Nov 5 at 3:59






  • 1




    Convert microseconds string to date in Java or Scala might help
    – MadProgrammer
    Nov 5 at 4:00






  • 1




    You should include sample data, indicating given inputs and expected outputs.
    – Basil Bourque
    Nov 5 at 4:48






  • 1




    uh, you are getting microseconds and want milliseconds without loss of precision. You are aware that 1000 microseconds go into a single millisecond and you're treating the number of milliseconds as an integer value. You are ALWAYS going to lose precision.
    – jwenting
    Nov 5 at 9:51















up vote
1
down vote

favorite












How can I convert incoming long time value in microseconds into milliseconds and then format that to data time stamp as below:




yyyyMMdd-HH:mm:ss.SSS




I am using Java SimpleDateFormat to format the long value to the timestamp. But, converting the microseconds to milliseconds seems problem as I am loosing the value.



SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HH:mm:ss.SSS");


To convert microsecond to millisecond, I am using the TimeUnit:



long micro //microseconds
long milli = TimeUnit.MILLISECONDS.convert(micro, TimeUnit.MICROSECONDS);


What is the right way to convert the value without loosing any data?










share|improve this question
























  • What's you current input value and what's your expected output value?
    – MadProgrammer
    Nov 5 at 3:56










  • What does you mean with "I am loosing the value"? Can you provide a runnable Minimal, Complete, and Verifiable example, with appropriate sampel values for the microseconds, that shows what you wanted to happen and what happened instead?
    – Erwin Bolwidt
    Nov 5 at 3:59






  • 1




    Convert microseconds string to date in Java or Scala might help
    – MadProgrammer
    Nov 5 at 4:00






  • 1




    You should include sample data, indicating given inputs and expected outputs.
    – Basil Bourque
    Nov 5 at 4:48






  • 1




    uh, you are getting microseconds and want milliseconds without loss of precision. You are aware that 1000 microseconds go into a single millisecond and you're treating the number of milliseconds as an integer value. You are ALWAYS going to lose precision.
    – jwenting
    Nov 5 at 9:51













up vote
1
down vote

favorite









up vote
1
down vote

favorite











How can I convert incoming long time value in microseconds into milliseconds and then format that to data time stamp as below:




yyyyMMdd-HH:mm:ss.SSS




I am using Java SimpleDateFormat to format the long value to the timestamp. But, converting the microseconds to milliseconds seems problem as I am loosing the value.



SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HH:mm:ss.SSS");


To convert microsecond to millisecond, I am using the TimeUnit:



long micro //microseconds
long milli = TimeUnit.MILLISECONDS.convert(micro, TimeUnit.MICROSECONDS);


What is the right way to convert the value without loosing any data?










share|improve this question















How can I convert incoming long time value in microseconds into milliseconds and then format that to data time stamp as below:




yyyyMMdd-HH:mm:ss.SSS




I am using Java SimpleDateFormat to format the long value to the timestamp. But, converting the microseconds to milliseconds seems problem as I am loosing the value.



SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HH:mm:ss.SSS");


To convert microsecond to millisecond, I am using the TimeUnit:



long micro //microseconds
long milli = TimeUnit.MILLISECONDS.convert(micro, TimeUnit.MICROSECONDS);


What is the right way to convert the value without loosing any data?







java timeunit






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 5 at 9:45









Ole V.V.

24.7k62349




24.7k62349










asked Nov 5 at 3:53









Jparak

91




91












  • What's you current input value and what's your expected output value?
    – MadProgrammer
    Nov 5 at 3:56










  • What does you mean with "I am loosing the value"? Can you provide a runnable Minimal, Complete, and Verifiable example, with appropriate sampel values for the microseconds, that shows what you wanted to happen and what happened instead?
    – Erwin Bolwidt
    Nov 5 at 3:59






  • 1




    Convert microseconds string to date in Java or Scala might help
    – MadProgrammer
    Nov 5 at 4:00






  • 1




    You should include sample data, indicating given inputs and expected outputs.
    – Basil Bourque
    Nov 5 at 4:48






  • 1




    uh, you are getting microseconds and want milliseconds without loss of precision. You are aware that 1000 microseconds go into a single millisecond and you're treating the number of milliseconds as an integer value. You are ALWAYS going to lose precision.
    – jwenting
    Nov 5 at 9:51


















  • What's you current input value and what's your expected output value?
    – MadProgrammer
    Nov 5 at 3:56










  • What does you mean with "I am loosing the value"? Can you provide a runnable Minimal, Complete, and Verifiable example, with appropriate sampel values for the microseconds, that shows what you wanted to happen and what happened instead?
    – Erwin Bolwidt
    Nov 5 at 3:59






  • 1




    Convert microseconds string to date in Java or Scala might help
    – MadProgrammer
    Nov 5 at 4:00






  • 1




    You should include sample data, indicating given inputs and expected outputs.
    – Basil Bourque
    Nov 5 at 4:48






  • 1




    uh, you are getting microseconds and want milliseconds without loss of precision. You are aware that 1000 microseconds go into a single millisecond and you're treating the number of milliseconds as an integer value. You are ALWAYS going to lose precision.
    – jwenting
    Nov 5 at 9:51
















What's you current input value and what's your expected output value?
– MadProgrammer
Nov 5 at 3:56




What's you current input value and what's your expected output value?
– MadProgrammer
Nov 5 at 3:56












What does you mean with "I am loosing the value"? Can you provide a runnable Minimal, Complete, and Verifiable example, with appropriate sampel values for the microseconds, that shows what you wanted to happen and what happened instead?
– Erwin Bolwidt
Nov 5 at 3:59




What does you mean with "I am loosing the value"? Can you provide a runnable Minimal, Complete, and Verifiable example, with appropriate sampel values for the microseconds, that shows what you wanted to happen and what happened instead?
– Erwin Bolwidt
Nov 5 at 3:59




1




1




Convert microseconds string to date in Java or Scala might help
– MadProgrammer
Nov 5 at 4:00




Convert microseconds string to date in Java or Scala might help
– MadProgrammer
Nov 5 at 4:00




1




1




You should include sample data, indicating given inputs and expected outputs.
– Basil Bourque
Nov 5 at 4:48




You should include sample data, indicating given inputs and expected outputs.
– Basil Bourque
Nov 5 at 4:48




1




1




uh, you are getting microseconds and want milliseconds without loss of precision. You are aware that 1000 microseconds go into a single millisecond and you're treating the number of milliseconds as an integer value. You are ALWAYS going to lose precision.
– jwenting
Nov 5 at 9:51




uh, you are getting microseconds and want milliseconds without loss of precision. You are aware that 1000 microseconds go into a single millisecond and you're treating the number of milliseconds as an integer value. You are ALWAYS going to lose precision.
– jwenting
Nov 5 at 9:51












1 Answer
1






active

oldest

votes

















up vote
5
down vote














I am using java SimpleDateFormat




Don’t.



Avoid the treacherous old date-time classes bundled with the earliest versions of Java. They were supplanted years ago with the java.time classes when JSR 310 was adopted.




convert incoming long time value in microseconds into milliseconds




Don’t.



The java.time classes use a resolution of nanoseconds, finer than your microseconds. So no need to throw away data.




Instant = ( whole seconds + fractional second as nanos )



Extract the number of whole seconds.



long seconds = TimeUnit.MICROSECONDS.toSeconds( micros ) ;


Get the fractional second, the amount of micros left over after subtracting for the whole seconds.



long microsRemaining = 
micros
-
TimeUnit.SECONDS.toMicros( seconds )
;


Convert the remaining micros to nanos, because the Instant class we use next represents a count of whole seconds plus a count of nanoseconds (for the fractional second).



long nanos = TimeUnit.MICROSECONDS.toNanos( microsRemaining ) ;


Combine the whole seconds with the fractional second in nanos. The Instant class represents a moment in UTC as a count since the epoch reference of first moment of 1970 in UTC, 1970-01-01T00:00Z.



Instant instant = Instant.ofEpochSecond( seconds ).plusNanos( nanos ) ;


To generate a string in standard ISO 8601 format, simply call toString.



String output = instant.toString() ;


The Z on the end of the string means UTC (an offset-from-UTC of zero hours-minutes-seconds), and is pronounced Zulu.



You can produce strings in your own custom formats using the DateTimeFormatter class. Search Stack Overflow as this has been covered many many many times already.



Tip: Your desired format is so close to the ISO 8601 format, YYYY-MM-DDTHH:MM:SSZ, that I strongly suggest using the standard format rather than make up your own cousin of that.



If you wish to see this same moment through the lens of the wall-clock time used by people of a certain region (a time zone), apply a ZoneId to get a ZonedDateTime.



ZoneId z = ZoneId.of( "Africa/Tunisia" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;




About java.time



The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.



The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.



To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.



You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.



Where to obtain the java.time classes?





  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.


    • Java 9 adds some minor features and fixes.




  • Java SE 6 and Java SE 7


    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.




  • Android


    • Later versions of Android bundle implementations of the java.time classes.

    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….




The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.






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',
    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%2f53148094%2fconvert-microseconds-to-milliseconds-without-loss-of-precision-and-format-as-dat%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    5
    down vote














    I am using java SimpleDateFormat




    Don’t.



    Avoid the treacherous old date-time classes bundled with the earliest versions of Java. They were supplanted years ago with the java.time classes when JSR 310 was adopted.




    convert incoming long time value in microseconds into milliseconds




    Don’t.



    The java.time classes use a resolution of nanoseconds, finer than your microseconds. So no need to throw away data.




    Instant = ( whole seconds + fractional second as nanos )



    Extract the number of whole seconds.



    long seconds = TimeUnit.MICROSECONDS.toSeconds( micros ) ;


    Get the fractional second, the amount of micros left over after subtracting for the whole seconds.



    long microsRemaining = 
    micros
    -
    TimeUnit.SECONDS.toMicros( seconds )
    ;


    Convert the remaining micros to nanos, because the Instant class we use next represents a count of whole seconds plus a count of nanoseconds (for the fractional second).



    long nanos = TimeUnit.MICROSECONDS.toNanos( microsRemaining ) ;


    Combine the whole seconds with the fractional second in nanos. The Instant class represents a moment in UTC as a count since the epoch reference of first moment of 1970 in UTC, 1970-01-01T00:00Z.



    Instant instant = Instant.ofEpochSecond( seconds ).plusNanos( nanos ) ;


    To generate a string in standard ISO 8601 format, simply call toString.



    String output = instant.toString() ;


    The Z on the end of the string means UTC (an offset-from-UTC of zero hours-minutes-seconds), and is pronounced Zulu.



    You can produce strings in your own custom formats using the DateTimeFormatter class. Search Stack Overflow as this has been covered many many many times already.



    Tip: Your desired format is so close to the ISO 8601 format, YYYY-MM-DDTHH:MM:SSZ, that I strongly suggest using the standard format rather than make up your own cousin of that.



    If you wish to see this same moment through the lens of the wall-clock time used by people of a certain region (a time zone), apply a ZoneId to get a ZonedDateTime.



    ZoneId z = ZoneId.of( "Africa/Tunisia" ) ;
    ZonedDateTime zdt = instant.atZone( z ) ;




    About java.time



    The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.



    The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.



    To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.



    You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.



    Where to obtain the java.time classes?





    • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.


      • Java 9 adds some minor features and fixes.




    • Java SE 6 and Java SE 7


      • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.




    • Android


      • Later versions of Android bundle implementations of the java.time classes.

      • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….




    The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.






    share|improve this answer



























      up vote
      5
      down vote














      I am using java SimpleDateFormat




      Don’t.



      Avoid the treacherous old date-time classes bundled with the earliest versions of Java. They were supplanted years ago with the java.time classes when JSR 310 was adopted.




      convert incoming long time value in microseconds into milliseconds




      Don’t.



      The java.time classes use a resolution of nanoseconds, finer than your microseconds. So no need to throw away data.




      Instant = ( whole seconds + fractional second as nanos )



      Extract the number of whole seconds.



      long seconds = TimeUnit.MICROSECONDS.toSeconds( micros ) ;


      Get the fractional second, the amount of micros left over after subtracting for the whole seconds.



      long microsRemaining = 
      micros
      -
      TimeUnit.SECONDS.toMicros( seconds )
      ;


      Convert the remaining micros to nanos, because the Instant class we use next represents a count of whole seconds plus a count of nanoseconds (for the fractional second).



      long nanos = TimeUnit.MICROSECONDS.toNanos( microsRemaining ) ;


      Combine the whole seconds with the fractional second in nanos. The Instant class represents a moment in UTC as a count since the epoch reference of first moment of 1970 in UTC, 1970-01-01T00:00Z.



      Instant instant = Instant.ofEpochSecond( seconds ).plusNanos( nanos ) ;


      To generate a string in standard ISO 8601 format, simply call toString.



      String output = instant.toString() ;


      The Z on the end of the string means UTC (an offset-from-UTC of zero hours-minutes-seconds), and is pronounced Zulu.



      You can produce strings in your own custom formats using the DateTimeFormatter class. Search Stack Overflow as this has been covered many many many times already.



      Tip: Your desired format is so close to the ISO 8601 format, YYYY-MM-DDTHH:MM:SSZ, that I strongly suggest using the standard format rather than make up your own cousin of that.



      If you wish to see this same moment through the lens of the wall-clock time used by people of a certain region (a time zone), apply a ZoneId to get a ZonedDateTime.



      ZoneId z = ZoneId.of( "Africa/Tunisia" ) ;
      ZonedDateTime zdt = instant.atZone( z ) ;




      About java.time



      The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.



      The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.



      To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.



      You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.



      Where to obtain the java.time classes?





      • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.


        • Java 9 adds some minor features and fixes.




      • Java SE 6 and Java SE 7


        • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.




      • Android


        • Later versions of Android bundle implementations of the java.time classes.

        • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….




      The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.






      share|improve this answer

























        up vote
        5
        down vote










        up vote
        5
        down vote










        I am using java SimpleDateFormat




        Don’t.



        Avoid the treacherous old date-time classes bundled with the earliest versions of Java. They were supplanted years ago with the java.time classes when JSR 310 was adopted.




        convert incoming long time value in microseconds into milliseconds




        Don’t.



        The java.time classes use a resolution of nanoseconds, finer than your microseconds. So no need to throw away data.




        Instant = ( whole seconds + fractional second as nanos )



        Extract the number of whole seconds.



        long seconds = TimeUnit.MICROSECONDS.toSeconds( micros ) ;


        Get the fractional second, the amount of micros left over after subtracting for the whole seconds.



        long microsRemaining = 
        micros
        -
        TimeUnit.SECONDS.toMicros( seconds )
        ;


        Convert the remaining micros to nanos, because the Instant class we use next represents a count of whole seconds plus a count of nanoseconds (for the fractional second).



        long nanos = TimeUnit.MICROSECONDS.toNanos( microsRemaining ) ;


        Combine the whole seconds with the fractional second in nanos. The Instant class represents a moment in UTC as a count since the epoch reference of first moment of 1970 in UTC, 1970-01-01T00:00Z.



        Instant instant = Instant.ofEpochSecond( seconds ).plusNanos( nanos ) ;


        To generate a string in standard ISO 8601 format, simply call toString.



        String output = instant.toString() ;


        The Z on the end of the string means UTC (an offset-from-UTC of zero hours-minutes-seconds), and is pronounced Zulu.



        You can produce strings in your own custom formats using the DateTimeFormatter class. Search Stack Overflow as this has been covered many many many times already.



        Tip: Your desired format is so close to the ISO 8601 format, YYYY-MM-DDTHH:MM:SSZ, that I strongly suggest using the standard format rather than make up your own cousin of that.



        If you wish to see this same moment through the lens of the wall-clock time used by people of a certain region (a time zone), apply a ZoneId to get a ZonedDateTime.



        ZoneId z = ZoneId.of( "Africa/Tunisia" ) ;
        ZonedDateTime zdt = instant.atZone( z ) ;




        About java.time



        The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.



        The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.



        To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.



        You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.



        Where to obtain the java.time classes?





        • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.


          • Java 9 adds some minor features and fixes.




        • Java SE 6 and Java SE 7


          • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.




        • Android


          • Later versions of Android bundle implementations of the java.time classes.

          • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….




        The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.






        share|improve this answer















        I am using java SimpleDateFormat




        Don’t.



        Avoid the treacherous old date-time classes bundled with the earliest versions of Java. They were supplanted years ago with the java.time classes when JSR 310 was adopted.




        convert incoming long time value in microseconds into milliseconds




        Don’t.



        The java.time classes use a resolution of nanoseconds, finer than your microseconds. So no need to throw away data.




        Instant = ( whole seconds + fractional second as nanos )



        Extract the number of whole seconds.



        long seconds = TimeUnit.MICROSECONDS.toSeconds( micros ) ;


        Get the fractional second, the amount of micros left over after subtracting for the whole seconds.



        long microsRemaining = 
        micros
        -
        TimeUnit.SECONDS.toMicros( seconds )
        ;


        Convert the remaining micros to nanos, because the Instant class we use next represents a count of whole seconds plus a count of nanoseconds (for the fractional second).



        long nanos = TimeUnit.MICROSECONDS.toNanos( microsRemaining ) ;


        Combine the whole seconds with the fractional second in nanos. The Instant class represents a moment in UTC as a count since the epoch reference of first moment of 1970 in UTC, 1970-01-01T00:00Z.



        Instant instant = Instant.ofEpochSecond( seconds ).plusNanos( nanos ) ;


        To generate a string in standard ISO 8601 format, simply call toString.



        String output = instant.toString() ;


        The Z on the end of the string means UTC (an offset-from-UTC of zero hours-minutes-seconds), and is pronounced Zulu.



        You can produce strings in your own custom formats using the DateTimeFormatter class. Search Stack Overflow as this has been covered many many many times already.



        Tip: Your desired format is so close to the ISO 8601 format, YYYY-MM-DDTHH:MM:SSZ, that I strongly suggest using the standard format rather than make up your own cousin of that.



        If you wish to see this same moment through the lens of the wall-clock time used by people of a certain region (a time zone), apply a ZoneId to get a ZonedDateTime.



        ZoneId z = ZoneId.of( "Africa/Tunisia" ) ;
        ZonedDateTime zdt = instant.atZone( z ) ;




        About java.time



        The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.



        The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.



        To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.



        You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.



        Where to obtain the java.time classes?





        • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.


          • Java 9 adds some minor features and fixes.




        • Java SE 6 and Java SE 7


          • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.




        • Android


          • Later versions of Android bundle implementations of the java.time classes.

          • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….




        The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 5 at 4:57

























        answered Nov 5 at 4:48









        Basil Bourque

        101k24350506




        101k24350506






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53148094%2fconvert-microseconds-to-milliseconds-without-loss-of-precision-and-format-as-dat%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()