Convert java.util.Date to String in yyyy-MM-dd format without creating a lot of objects

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
8
down vote

favorite












I need to convert java.util.Date to String in yyyy-MM-dd format in a big amounts.



I have just moved to java 8 and want to know how to do it properly.
When using java 7 I was using



DateTimeFormatter DATE_FORMATTER = DateTimeFormat.forPattern(DATE_FORMAT_PATTERN)


and then



DATE_FORMATTER.print(value.getTime())


it helped me not to create a lots of redundant objects



So now when I moved to java 8 I want rewrite it properly but



LocalDate.fromDateFields(value).toString())


creates each time new LocalDate object and this gives a lot of work to GC



Are there any ways to solve my problem?
Performance and thread-safety are very important










share|improve this question



















  • 1




    Do you have Date or LocalDate objects?
    – Flown
    1 hour ago










  • Hi, java.util.Date
    – Oleksandr Riznyk
    55 mins ago










  • There is no need converting to LocalDate, just use the SimpleDateFormat.
    – Flown
    53 mins ago














up vote
8
down vote

favorite












I need to convert java.util.Date to String in yyyy-MM-dd format in a big amounts.



I have just moved to java 8 and want to know how to do it properly.
When using java 7 I was using



DateTimeFormatter DATE_FORMATTER = DateTimeFormat.forPattern(DATE_FORMAT_PATTERN)


and then



DATE_FORMATTER.print(value.getTime())


it helped me not to create a lots of redundant objects



So now when I moved to java 8 I want rewrite it properly but



LocalDate.fromDateFields(value).toString())


creates each time new LocalDate object and this gives a lot of work to GC



Are there any ways to solve my problem?
Performance and thread-safety are very important










share|improve this question



















  • 1




    Do you have Date or LocalDate objects?
    – Flown
    1 hour ago










  • Hi, java.util.Date
    – Oleksandr Riznyk
    55 mins ago










  • There is no need converting to LocalDate, just use the SimpleDateFormat.
    – Flown
    53 mins ago












up vote
8
down vote

favorite









up vote
8
down vote

favorite











I need to convert java.util.Date to String in yyyy-MM-dd format in a big amounts.



I have just moved to java 8 and want to know how to do it properly.
When using java 7 I was using



DateTimeFormatter DATE_FORMATTER = DateTimeFormat.forPattern(DATE_FORMAT_PATTERN)


and then



DATE_FORMATTER.print(value.getTime())


it helped me not to create a lots of redundant objects



So now when I moved to java 8 I want rewrite it properly but



LocalDate.fromDateFields(value).toString())


creates each time new LocalDate object and this gives a lot of work to GC



Are there any ways to solve my problem?
Performance and thread-safety are very important










share|improve this question















I need to convert java.util.Date to String in yyyy-MM-dd format in a big amounts.



I have just moved to java 8 and want to know how to do it properly.
When using java 7 I was using



DateTimeFormatter DATE_FORMATTER = DateTimeFormat.forPattern(DATE_FORMAT_PATTERN)


and then



DATE_FORMATTER.print(value.getTime())


it helped me not to create a lots of redundant objects



So now when I moved to java 8 I want rewrite it properly but



LocalDate.fromDateFields(value).toString())


creates each time new LocalDate object and this gives a lot of work to GC



Are there any ways to solve my problem?
Performance and thread-safety are very important







java java-8 java-time






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 38 mins ago

























asked 1 hour ago









Oleksandr Riznyk

21018




21018







  • 1




    Do you have Date or LocalDate objects?
    – Flown
    1 hour ago










  • Hi, java.util.Date
    – Oleksandr Riznyk
    55 mins ago










  • There is no need converting to LocalDate, just use the SimpleDateFormat.
    – Flown
    53 mins ago












  • 1




    Do you have Date or LocalDate objects?
    – Flown
    1 hour ago










  • Hi, java.util.Date
    – Oleksandr Riznyk
    55 mins ago










  • There is no need converting to LocalDate, just use the SimpleDateFormat.
    – Flown
    53 mins ago







1




1




Do you have Date or LocalDate objects?
– Flown
1 hour ago




Do you have Date or LocalDate objects?
– Flown
1 hour ago












Hi, java.util.Date
– Oleksandr Riznyk
55 mins ago




Hi, java.util.Date
– Oleksandr Riznyk
55 mins ago












There is no need converting to LocalDate, just use the SimpleDateFormat.
– Flown
53 mins ago




There is no need converting to LocalDate, just use the SimpleDateFormat.
– Flown
53 mins ago












6 Answers
6






active

oldest

votes

















up vote
3
down vote













The following only has an overhead for the conversion of the old Date to the new LocalDate.



 Date date = new Date();
LocalDate ldate = LocalDate.from(date.toInstant());
String s = DateTimeFormatter.ISO_DATE.format(ldate); // uuuu-MM-dd


It is true however that DateTimeFormatters are thread-safe and hence will have one instantiation more per call.






share|improve this answer




















  • Good idea I didn't notice that I can use date.toInstant() already with the static method LocalDate.from(), this reduces the part of specifing a time zone. +1
    – 0x1C1B
    41 mins ago






  • 1




    This snippet throws an exception on 1.8.0_181 and 11
    – Flown
    36 mins ago










  • @Flown You're right also on JDK10 what's the problem?
    – 0x1C1B
    33 mins ago






  • 1




    Should be LocalDate.from(date.toInstant().atZone(ZoneOffset.UTC))
    – Flown
    31 mins ago










  • @Flown Is LocalDate.from(date.toInstant().atZone(ZoneOffset.UTC)) faster than date.toInstant().atZone(ZoneOffset.UTC).toLocalDate();?
    – 0x1C1B
    23 mins ago

















up vote
1
down vote













I don't have exact numbers from point of view of the performance but I would use the Java 8 Time API to solve this issue. In your special case I would use the following statement:



LocalDate.now().format(DateTimeFormatter.ISO_DATE);


EDIT:
For this solution is a conversion from java.util.Date to java.time.LocalDate required!



new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();





share|improve this answer





























    up vote
    1
    down vote













    Use SimpleDateFormat to format Date.



    watch out, SDF is NOT THREAD-SAFE, it might not be important but keep that in mind.



    For Example:



    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    System.out.println((sdf.format(new Date())).toString());


    LINK with more information.






    share|improve this answer






















    • The method parse(String) in the type DateFormat is not applicable for the arguments (Date)
      – Selaron
      47 mins ago










    • yeah, i meant format :) nice catch
      – Emil Hotkowski
      41 mins ago

















    up vote
    0
    down vote













    I would suggest you try out Joda Time for DateTime-related operations.
    Joda Time Maven






    share|improve this answer




















    • thanks but my solution from Java 7 is already uses Joda Time. I would like do know if Java 8 provides any better solutions?
      – Oleksandr Riznyk
      1 hour ago


















    up vote
    0
    down vote













    Without creating lots of objects, meaning you want the performance version?



    public static String getIsoDate(java.util.Date time) 
    java.util.Calendar cal = java.util.Calendar.getInstance();
    cal.setTime(time);
    StringBuilder sb = new StringBuilder();
    int year = cal.get(java.util.Calendar.YEAR);
    int month = cal.get(java.util.Calendar.MONTH);
    int day = cal.get(java.util.Calendar.DAY_OF_MONTH);
    sb.append(year);
    sb.append('-');
    if (month < 10)
    sb.append('0');

    sb.append(month);
    sb.append('-');
    if (day < 10)
    sb.append('0');

    sb.append(day);
    return sb.toString();



    This version is thread-safe and avoids most hidden object creations, and unless you have years under 1000 or over 9999, it will print just fine.






    share|improve this answer




















    • This isn't offensive it's actually interest, are you sure that this code is faster than converting it to LocalDate and using the Java 8 Time API? I mean, I'm definetly not a performance freak, but it seems that this is a lot of code with memory allocation... Seems to be interesting...
      – 0x1C1B
      25 mins ago

















    up vote
    -1
    down vote













    Use SimpleDateFormat to format Date in your expected format.



    For Example:



    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    System.out.println((sdf.parse("2019-08-31")).toString());





    share|improve this answer




















    • Hi, i need to convert java.util.Date object to String, but your code is used for parsing Date from String which I do not have at this moment
      – Oleksandr Riznyk
      55 mins ago










    • how do you want to do it then?
      – Busy Bee
      49 mins ago






    • 1




      I don't understand your question but in your solution you parsing a date from string but my target is to parse string it is the question
      – Oleksandr Riznyk
      44 mins ago










    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: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    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%2f52870774%2fconvert-java-util-date-to-string-in-yyyy-mm-dd-format-without-creating-a-lot-of%23new-answer', 'question_page');

    );

    Post as a guest






























    6 Answers
    6






    active

    oldest

    votes








    6 Answers
    6






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    The following only has an overhead for the conversion of the old Date to the new LocalDate.



     Date date = new Date();
    LocalDate ldate = LocalDate.from(date.toInstant());
    String s = DateTimeFormatter.ISO_DATE.format(ldate); // uuuu-MM-dd


    It is true however that DateTimeFormatters are thread-safe and hence will have one instantiation more per call.






    share|improve this answer




















    • Good idea I didn't notice that I can use date.toInstant() already with the static method LocalDate.from(), this reduces the part of specifing a time zone. +1
      – 0x1C1B
      41 mins ago






    • 1




      This snippet throws an exception on 1.8.0_181 and 11
      – Flown
      36 mins ago










    • @Flown You're right also on JDK10 what's the problem?
      – 0x1C1B
      33 mins ago






    • 1




      Should be LocalDate.from(date.toInstant().atZone(ZoneOffset.UTC))
      – Flown
      31 mins ago










    • @Flown Is LocalDate.from(date.toInstant().atZone(ZoneOffset.UTC)) faster than date.toInstant().atZone(ZoneOffset.UTC).toLocalDate();?
      – 0x1C1B
      23 mins ago














    up vote
    3
    down vote













    The following only has an overhead for the conversion of the old Date to the new LocalDate.



     Date date = new Date();
    LocalDate ldate = LocalDate.from(date.toInstant());
    String s = DateTimeFormatter.ISO_DATE.format(ldate); // uuuu-MM-dd


    It is true however that DateTimeFormatters are thread-safe and hence will have one instantiation more per call.






    share|improve this answer




















    • Good idea I didn't notice that I can use date.toInstant() already with the static method LocalDate.from(), this reduces the part of specifing a time zone. +1
      – 0x1C1B
      41 mins ago






    • 1




      This snippet throws an exception on 1.8.0_181 and 11
      – Flown
      36 mins ago










    • @Flown You're right also on JDK10 what's the problem?
      – 0x1C1B
      33 mins ago






    • 1




      Should be LocalDate.from(date.toInstant().atZone(ZoneOffset.UTC))
      – Flown
      31 mins ago










    • @Flown Is LocalDate.from(date.toInstant().atZone(ZoneOffset.UTC)) faster than date.toInstant().atZone(ZoneOffset.UTC).toLocalDate();?
      – 0x1C1B
      23 mins ago












    up vote
    3
    down vote










    up vote
    3
    down vote









    The following only has an overhead for the conversion of the old Date to the new LocalDate.



     Date date = new Date();
    LocalDate ldate = LocalDate.from(date.toInstant());
    String s = DateTimeFormatter.ISO_DATE.format(ldate); // uuuu-MM-dd


    It is true however that DateTimeFormatters are thread-safe and hence will have one instantiation more per call.






    share|improve this answer












    The following only has an overhead for the conversion of the old Date to the new LocalDate.



     Date date = new Date();
    LocalDate ldate = LocalDate.from(date.toInstant());
    String s = DateTimeFormatter.ISO_DATE.format(ldate); // uuuu-MM-dd


    It is true however that DateTimeFormatters are thread-safe and hence will have one instantiation more per call.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 51 mins ago









    Joop Eggen

    74.4k754100




    74.4k754100











    • Good idea I didn't notice that I can use date.toInstant() already with the static method LocalDate.from(), this reduces the part of specifing a time zone. +1
      – 0x1C1B
      41 mins ago






    • 1




      This snippet throws an exception on 1.8.0_181 and 11
      – Flown
      36 mins ago










    • @Flown You're right also on JDK10 what's the problem?
      – 0x1C1B
      33 mins ago






    • 1




      Should be LocalDate.from(date.toInstant().atZone(ZoneOffset.UTC))
      – Flown
      31 mins ago










    • @Flown Is LocalDate.from(date.toInstant().atZone(ZoneOffset.UTC)) faster than date.toInstant().atZone(ZoneOffset.UTC).toLocalDate();?
      – 0x1C1B
      23 mins ago
















    • Good idea I didn't notice that I can use date.toInstant() already with the static method LocalDate.from(), this reduces the part of specifing a time zone. +1
      – 0x1C1B
      41 mins ago






    • 1




      This snippet throws an exception on 1.8.0_181 and 11
      – Flown
      36 mins ago










    • @Flown You're right also on JDK10 what's the problem?
      – 0x1C1B
      33 mins ago






    • 1




      Should be LocalDate.from(date.toInstant().atZone(ZoneOffset.UTC))
      – Flown
      31 mins ago










    • @Flown Is LocalDate.from(date.toInstant().atZone(ZoneOffset.UTC)) faster than date.toInstant().atZone(ZoneOffset.UTC).toLocalDate();?
      – 0x1C1B
      23 mins ago















    Good idea I didn't notice that I can use date.toInstant() already with the static method LocalDate.from(), this reduces the part of specifing a time zone. +1
    – 0x1C1B
    41 mins ago




    Good idea I didn't notice that I can use date.toInstant() already with the static method LocalDate.from(), this reduces the part of specifing a time zone. +1
    – 0x1C1B
    41 mins ago




    1




    1




    This snippet throws an exception on 1.8.0_181 and 11
    – Flown
    36 mins ago




    This snippet throws an exception on 1.8.0_181 and 11
    – Flown
    36 mins ago












    @Flown You're right also on JDK10 what's the problem?
    – 0x1C1B
    33 mins ago




    @Flown You're right also on JDK10 what's the problem?
    – 0x1C1B
    33 mins ago




    1




    1




    Should be LocalDate.from(date.toInstant().atZone(ZoneOffset.UTC))
    – Flown
    31 mins ago




    Should be LocalDate.from(date.toInstant().atZone(ZoneOffset.UTC))
    – Flown
    31 mins ago












    @Flown Is LocalDate.from(date.toInstant().atZone(ZoneOffset.UTC)) faster than date.toInstant().atZone(ZoneOffset.UTC).toLocalDate();?
    – 0x1C1B
    23 mins ago




    @Flown Is LocalDate.from(date.toInstant().atZone(ZoneOffset.UTC)) faster than date.toInstant().atZone(ZoneOffset.UTC).toLocalDate();?
    – 0x1C1B
    23 mins ago












    up vote
    1
    down vote













    I don't have exact numbers from point of view of the performance but I would use the Java 8 Time API to solve this issue. In your special case I would use the following statement:



    LocalDate.now().format(DateTimeFormatter.ISO_DATE);


    EDIT:
    For this solution is a conversion from java.util.Date to java.time.LocalDate required!



    new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();





    share|improve this answer


























      up vote
      1
      down vote













      I don't have exact numbers from point of view of the performance but I would use the Java 8 Time API to solve this issue. In your special case I would use the following statement:



      LocalDate.now().format(DateTimeFormatter.ISO_DATE);


      EDIT:
      For this solution is a conversion from java.util.Date to java.time.LocalDate required!



      new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();





      share|improve this answer
























        up vote
        1
        down vote










        up vote
        1
        down vote









        I don't have exact numbers from point of view of the performance but I would use the Java 8 Time API to solve this issue. In your special case I would use the following statement:



        LocalDate.now().format(DateTimeFormatter.ISO_DATE);


        EDIT:
        For this solution is a conversion from java.util.Date to java.time.LocalDate required!



        new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();





        share|improve this answer














        I don't have exact numbers from point of view of the performance but I would use the Java 8 Time API to solve this issue. In your special case I would use the following statement:



        LocalDate.now().format(DateTimeFormatter.ISO_DATE);


        EDIT:
        For this solution is a conversion from java.util.Date to java.time.LocalDate required!



        new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 48 mins ago

























        answered 53 mins ago









        0x1C1B

        47012




        47012




















            up vote
            1
            down vote













            Use SimpleDateFormat to format Date.



            watch out, SDF is NOT THREAD-SAFE, it might not be important but keep that in mind.



            For Example:



            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            System.out.println((sdf.format(new Date())).toString());


            LINK with more information.






            share|improve this answer






















            • The method parse(String) in the type DateFormat is not applicable for the arguments (Date)
              – Selaron
              47 mins ago










            • yeah, i meant format :) nice catch
              – Emil Hotkowski
              41 mins ago














            up vote
            1
            down vote













            Use SimpleDateFormat to format Date.



            watch out, SDF is NOT THREAD-SAFE, it might not be important but keep that in mind.



            For Example:



            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            System.out.println((sdf.format(new Date())).toString());


            LINK with more information.






            share|improve this answer






















            • The method parse(String) in the type DateFormat is not applicable for the arguments (Date)
              – Selaron
              47 mins ago










            • yeah, i meant format :) nice catch
              – Emil Hotkowski
              41 mins ago












            up vote
            1
            down vote










            up vote
            1
            down vote









            Use SimpleDateFormat to format Date.



            watch out, SDF is NOT THREAD-SAFE, it might not be important but keep that in mind.



            For Example:



            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            System.out.println((sdf.format(new Date())).toString());


            LINK with more information.






            share|improve this answer














            Use SimpleDateFormat to format Date.



            watch out, SDF is NOT THREAD-SAFE, it might not be important but keep that in mind.



            For Example:



            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            System.out.println((sdf.format(new Date())).toString());


            LINK with more information.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 42 mins ago

























            answered 52 mins ago









            Emil Hotkowski

            758312




            758312











            • The method parse(String) in the type DateFormat is not applicable for the arguments (Date)
              – Selaron
              47 mins ago










            • yeah, i meant format :) nice catch
              – Emil Hotkowski
              41 mins ago
















            • The method parse(String) in the type DateFormat is not applicable for the arguments (Date)
              – Selaron
              47 mins ago










            • yeah, i meant format :) nice catch
              – Emil Hotkowski
              41 mins ago















            The method parse(String) in the type DateFormat is not applicable for the arguments (Date)
            – Selaron
            47 mins ago




            The method parse(String) in the type DateFormat is not applicable for the arguments (Date)
            – Selaron
            47 mins ago












            yeah, i meant format :) nice catch
            – Emil Hotkowski
            41 mins ago




            yeah, i meant format :) nice catch
            – Emil Hotkowski
            41 mins ago










            up vote
            0
            down vote













            I would suggest you try out Joda Time for DateTime-related operations.
            Joda Time Maven






            share|improve this answer




















            • thanks but my solution from Java 7 is already uses Joda Time. I would like do know if Java 8 provides any better solutions?
              – Oleksandr Riznyk
              1 hour ago















            up vote
            0
            down vote













            I would suggest you try out Joda Time for DateTime-related operations.
            Joda Time Maven






            share|improve this answer




















            • thanks but my solution from Java 7 is already uses Joda Time. I would like do know if Java 8 provides any better solutions?
              – Oleksandr Riznyk
              1 hour ago













            up vote
            0
            down vote










            up vote
            0
            down vote









            I would suggest you try out Joda Time for DateTime-related operations.
            Joda Time Maven






            share|improve this answer












            I would suggest you try out Joda Time for DateTime-related operations.
            Joda Time Maven







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 1 hour ago









            Nagaraja R

            43




            43











            • thanks but my solution from Java 7 is already uses Joda Time. I would like do know if Java 8 provides any better solutions?
              – Oleksandr Riznyk
              1 hour ago

















            • thanks but my solution from Java 7 is already uses Joda Time. I would like do know if Java 8 provides any better solutions?
              – Oleksandr Riznyk
              1 hour ago
















            thanks but my solution from Java 7 is already uses Joda Time. I would like do know if Java 8 provides any better solutions?
            – Oleksandr Riznyk
            1 hour ago





            thanks but my solution from Java 7 is already uses Joda Time. I would like do know if Java 8 provides any better solutions?
            – Oleksandr Riznyk
            1 hour ago











            up vote
            0
            down vote













            Without creating lots of objects, meaning you want the performance version?



            public static String getIsoDate(java.util.Date time) 
            java.util.Calendar cal = java.util.Calendar.getInstance();
            cal.setTime(time);
            StringBuilder sb = new StringBuilder();
            int year = cal.get(java.util.Calendar.YEAR);
            int month = cal.get(java.util.Calendar.MONTH);
            int day = cal.get(java.util.Calendar.DAY_OF_MONTH);
            sb.append(year);
            sb.append('-');
            if (month < 10)
            sb.append('0');

            sb.append(month);
            sb.append('-');
            if (day < 10)
            sb.append('0');

            sb.append(day);
            return sb.toString();



            This version is thread-safe and avoids most hidden object creations, and unless you have years under 1000 or over 9999, it will print just fine.






            share|improve this answer




















            • This isn't offensive it's actually interest, are you sure that this code is faster than converting it to LocalDate and using the Java 8 Time API? I mean, I'm definetly not a performance freak, but it seems that this is a lot of code with memory allocation... Seems to be interesting...
              – 0x1C1B
              25 mins ago














            up vote
            0
            down vote













            Without creating lots of objects, meaning you want the performance version?



            public static String getIsoDate(java.util.Date time) 
            java.util.Calendar cal = java.util.Calendar.getInstance();
            cal.setTime(time);
            StringBuilder sb = new StringBuilder();
            int year = cal.get(java.util.Calendar.YEAR);
            int month = cal.get(java.util.Calendar.MONTH);
            int day = cal.get(java.util.Calendar.DAY_OF_MONTH);
            sb.append(year);
            sb.append('-');
            if (month < 10)
            sb.append('0');

            sb.append(month);
            sb.append('-');
            if (day < 10)
            sb.append('0');

            sb.append(day);
            return sb.toString();



            This version is thread-safe and avoids most hidden object creations, and unless you have years under 1000 or over 9999, it will print just fine.






            share|improve this answer




















            • This isn't offensive it's actually interest, are you sure that this code is faster than converting it to LocalDate and using the Java 8 Time API? I mean, I'm definetly not a performance freak, but it seems that this is a lot of code with memory allocation... Seems to be interesting...
              – 0x1C1B
              25 mins ago












            up vote
            0
            down vote










            up vote
            0
            down vote









            Without creating lots of objects, meaning you want the performance version?



            public static String getIsoDate(java.util.Date time) 
            java.util.Calendar cal = java.util.Calendar.getInstance();
            cal.setTime(time);
            StringBuilder sb = new StringBuilder();
            int year = cal.get(java.util.Calendar.YEAR);
            int month = cal.get(java.util.Calendar.MONTH);
            int day = cal.get(java.util.Calendar.DAY_OF_MONTH);
            sb.append(year);
            sb.append('-');
            if (month < 10)
            sb.append('0');

            sb.append(month);
            sb.append('-');
            if (day < 10)
            sb.append('0');

            sb.append(day);
            return sb.toString();



            This version is thread-safe and avoids most hidden object creations, and unless you have years under 1000 or over 9999, it will print just fine.






            share|improve this answer












            Without creating lots of objects, meaning you want the performance version?



            public static String getIsoDate(java.util.Date time) 
            java.util.Calendar cal = java.util.Calendar.getInstance();
            cal.setTime(time);
            StringBuilder sb = new StringBuilder();
            int year = cal.get(java.util.Calendar.YEAR);
            int month = cal.get(java.util.Calendar.MONTH);
            int day = cal.get(java.util.Calendar.DAY_OF_MONTH);
            sb.append(year);
            sb.append('-');
            if (month < 10)
            sb.append('0');

            sb.append(month);
            sb.append('-');
            if (day < 10)
            sb.append('0');

            sb.append(day);
            return sb.toString();



            This version is thread-safe and avoids most hidden object creations, and unless you have years under 1000 or over 9999, it will print just fine.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 32 mins ago









            coladict

            1,601517




            1,601517











            • This isn't offensive it's actually interest, are you sure that this code is faster than converting it to LocalDate and using the Java 8 Time API? I mean, I'm definetly not a performance freak, but it seems that this is a lot of code with memory allocation... Seems to be interesting...
              – 0x1C1B
              25 mins ago
















            • This isn't offensive it's actually interest, are you sure that this code is faster than converting it to LocalDate and using the Java 8 Time API? I mean, I'm definetly not a performance freak, but it seems that this is a lot of code with memory allocation... Seems to be interesting...
              – 0x1C1B
              25 mins ago















            This isn't offensive it's actually interest, are you sure that this code is faster than converting it to LocalDate and using the Java 8 Time API? I mean, I'm definetly not a performance freak, but it seems that this is a lot of code with memory allocation... Seems to be interesting...
            – 0x1C1B
            25 mins ago




            This isn't offensive it's actually interest, are you sure that this code is faster than converting it to LocalDate and using the Java 8 Time API? I mean, I'm definetly not a performance freak, but it seems that this is a lot of code with memory allocation... Seems to be interesting...
            – 0x1C1B
            25 mins ago










            up vote
            -1
            down vote













            Use SimpleDateFormat to format Date in your expected format.



            For Example:



            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            System.out.println((sdf.parse("2019-08-31")).toString());





            share|improve this answer




















            • Hi, i need to convert java.util.Date object to String, but your code is used for parsing Date from String which I do not have at this moment
              – Oleksandr Riznyk
              55 mins ago










            • how do you want to do it then?
              – Busy Bee
              49 mins ago






            • 1




              I don't understand your question but in your solution you parsing a date from string but my target is to parse string it is the question
              – Oleksandr Riznyk
              44 mins ago














            up vote
            -1
            down vote













            Use SimpleDateFormat to format Date in your expected format.



            For Example:



            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            System.out.println((sdf.parse("2019-08-31")).toString());





            share|improve this answer




















            • Hi, i need to convert java.util.Date object to String, but your code is used for parsing Date from String which I do not have at this moment
              – Oleksandr Riznyk
              55 mins ago










            • how do you want to do it then?
              – Busy Bee
              49 mins ago






            • 1




              I don't understand your question but in your solution you parsing a date from string but my target is to parse string it is the question
              – Oleksandr Riznyk
              44 mins ago












            up vote
            -1
            down vote










            up vote
            -1
            down vote









            Use SimpleDateFormat to format Date in your expected format.



            For Example:



            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            System.out.println((sdf.parse("2019-08-31")).toString());





            share|improve this answer












            Use SimpleDateFormat to format Date in your expected format.



            For Example:



            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            System.out.println((sdf.parse("2019-08-31")).toString());






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 58 mins ago









            Busy Bee

            7251618




            7251618











            • Hi, i need to convert java.util.Date object to String, but your code is used for parsing Date from String which I do not have at this moment
              – Oleksandr Riznyk
              55 mins ago










            • how do you want to do it then?
              – Busy Bee
              49 mins ago






            • 1




              I don't understand your question but in your solution you parsing a date from string but my target is to parse string it is the question
              – Oleksandr Riznyk
              44 mins ago
















            • Hi, i need to convert java.util.Date object to String, but your code is used for parsing Date from String which I do not have at this moment
              – Oleksandr Riznyk
              55 mins ago










            • how do you want to do it then?
              – Busy Bee
              49 mins ago






            • 1




              I don't understand your question but in your solution you parsing a date from string but my target is to parse string it is the question
              – Oleksandr Riznyk
              44 mins ago















            Hi, i need to convert java.util.Date object to String, but your code is used for parsing Date from String which I do not have at this moment
            – Oleksandr Riznyk
            55 mins ago




            Hi, i need to convert java.util.Date object to String, but your code is used for parsing Date from String which I do not have at this moment
            – Oleksandr Riznyk
            55 mins ago












            how do you want to do it then?
            – Busy Bee
            49 mins ago




            how do you want to do it then?
            – Busy Bee
            49 mins ago




            1




            1




            I don't understand your question but in your solution you parsing a date from string but my target is to parse string it is the question
            – Oleksandr Riznyk
            44 mins ago




            I don't understand your question but in your solution you parsing a date from string but my target is to parse string it is the question
            – Oleksandr Riznyk
            44 mins ago

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52870774%2fconvert-java-util-date-to-string-in-yyyy-mm-dd-format-without-creating-a-lot-of%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            List of Gilmore Girls characters

            Confectionery