Java Instant to LocalDateTime trailing zero











up vote
1
down vote

favorite












I converted an Instant to LocalDateTime in Java with Spring Boot as seen below



LocalDateTime.ofInstant(timeInUtc, zoneId);


In my test I got a Regex to check whether my resource returns a Json with a LocalDateTime. The Regex expects a JSON value in the format:



 2018-11-15T08:38:49.382


But it looks like the trailing zero is removed, meaning instead of



2018-11-15T08:38:49.380


which would comply to the regex, I get



 2018-11-15T08:38:49.38


How can I make sure that the trailing zero is not removed?



Thanks in advance!










share|improve this question




















  • 1




    How are you converting your localdatetime object to string?
    – ernest_k
    Nov 15 at 7:46










  • How do you print/format it? If I run System.out.println(LocalDateTime.parse("2018-11-15T08:38:49.380")) I get 2018-11-15T08:38:49.380 (trailing zero's there).
    – Tomasz Linkowski
    Nov 15 at 7:47










  • I use Jackson to serialize and deserialize my object to JSON.
    – Tror1935
    Nov 15 at 7:54










  • For the sake of precision, the trailing zero is not being removed. Neither Instant nor LocalDateTime have any textual representation in them, but generate one when you call toString. And doesn’t generate trailing zeroes in this case. They do generate ISO 8601 format, which for most purposes is fine, so please check once more whether the absence of zeroes is really a problem in your situation.
    – Ole V.V.
    Nov 15 at 8:30






  • 1




    Why don’t you fix the regex instead?
    – Holger
    Nov 15 at 10:32















up vote
1
down vote

favorite












I converted an Instant to LocalDateTime in Java with Spring Boot as seen below



LocalDateTime.ofInstant(timeInUtc, zoneId);


In my test I got a Regex to check whether my resource returns a Json with a LocalDateTime. The Regex expects a JSON value in the format:



 2018-11-15T08:38:49.382


But it looks like the trailing zero is removed, meaning instead of



2018-11-15T08:38:49.380


which would comply to the regex, I get



 2018-11-15T08:38:49.38


How can I make sure that the trailing zero is not removed?



Thanks in advance!










share|improve this question




















  • 1




    How are you converting your localdatetime object to string?
    – ernest_k
    Nov 15 at 7:46










  • How do you print/format it? If I run System.out.println(LocalDateTime.parse("2018-11-15T08:38:49.380")) I get 2018-11-15T08:38:49.380 (trailing zero's there).
    – Tomasz Linkowski
    Nov 15 at 7:47










  • I use Jackson to serialize and deserialize my object to JSON.
    – Tror1935
    Nov 15 at 7:54










  • For the sake of precision, the trailing zero is not being removed. Neither Instant nor LocalDateTime have any textual representation in them, but generate one when you call toString. And doesn’t generate trailing zeroes in this case. They do generate ISO 8601 format, which for most purposes is fine, so please check once more whether the absence of zeroes is really a problem in your situation.
    – Ole V.V.
    Nov 15 at 8:30






  • 1




    Why don’t you fix the regex instead?
    – Holger
    Nov 15 at 10:32













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I converted an Instant to LocalDateTime in Java with Spring Boot as seen below



LocalDateTime.ofInstant(timeInUtc, zoneId);


In my test I got a Regex to check whether my resource returns a Json with a LocalDateTime. The Regex expects a JSON value in the format:



 2018-11-15T08:38:49.382


But it looks like the trailing zero is removed, meaning instead of



2018-11-15T08:38:49.380


which would comply to the regex, I get



 2018-11-15T08:38:49.38


How can I make sure that the trailing zero is not removed?



Thanks in advance!










share|improve this question















I converted an Instant to LocalDateTime in Java with Spring Boot as seen below



LocalDateTime.ofInstant(timeInUtc, zoneId);


In my test I got a Regex to check whether my resource returns a Json with a LocalDateTime. The Regex expects a JSON value in the format:



 2018-11-15T08:38:49.382


But it looks like the trailing zero is removed, meaning instead of



2018-11-15T08:38:49.380


which would comply to the regex, I get



 2018-11-15T08:38:49.38


How can I make sure that the trailing zero is not removed?



Thanks in advance!







java spring-boot java-8 instant






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 8:01

























asked Nov 15 at 7:43









Tror1935

214




214








  • 1




    How are you converting your localdatetime object to string?
    – ernest_k
    Nov 15 at 7:46










  • How do you print/format it? If I run System.out.println(LocalDateTime.parse("2018-11-15T08:38:49.380")) I get 2018-11-15T08:38:49.380 (trailing zero's there).
    – Tomasz Linkowski
    Nov 15 at 7:47










  • I use Jackson to serialize and deserialize my object to JSON.
    – Tror1935
    Nov 15 at 7:54










  • For the sake of precision, the trailing zero is not being removed. Neither Instant nor LocalDateTime have any textual representation in them, but generate one when you call toString. And doesn’t generate trailing zeroes in this case. They do generate ISO 8601 format, which for most purposes is fine, so please check once more whether the absence of zeroes is really a problem in your situation.
    – Ole V.V.
    Nov 15 at 8:30






  • 1




    Why don’t you fix the regex instead?
    – Holger
    Nov 15 at 10:32














  • 1




    How are you converting your localdatetime object to string?
    – ernest_k
    Nov 15 at 7:46










  • How do you print/format it? If I run System.out.println(LocalDateTime.parse("2018-11-15T08:38:49.380")) I get 2018-11-15T08:38:49.380 (trailing zero's there).
    – Tomasz Linkowski
    Nov 15 at 7:47










  • I use Jackson to serialize and deserialize my object to JSON.
    – Tror1935
    Nov 15 at 7:54










  • For the sake of precision, the trailing zero is not being removed. Neither Instant nor LocalDateTime have any textual representation in them, but generate one when you call toString. And doesn’t generate trailing zeroes in this case. They do generate ISO 8601 format, which for most purposes is fine, so please check once more whether the absence of zeroes is really a problem in your situation.
    – Ole V.V.
    Nov 15 at 8:30






  • 1




    Why don’t you fix the regex instead?
    – Holger
    Nov 15 at 10:32








1




1




How are you converting your localdatetime object to string?
– ernest_k
Nov 15 at 7:46




How are you converting your localdatetime object to string?
– ernest_k
Nov 15 at 7:46












How do you print/format it? If I run System.out.println(LocalDateTime.parse("2018-11-15T08:38:49.380")) I get 2018-11-15T08:38:49.380 (trailing zero's there).
– Tomasz Linkowski
Nov 15 at 7:47




How do you print/format it? If I run System.out.println(LocalDateTime.parse("2018-11-15T08:38:49.380")) I get 2018-11-15T08:38:49.380 (trailing zero's there).
– Tomasz Linkowski
Nov 15 at 7:47












I use Jackson to serialize and deserialize my object to JSON.
– Tror1935
Nov 15 at 7:54




I use Jackson to serialize and deserialize my object to JSON.
– Tror1935
Nov 15 at 7:54












For the sake of precision, the trailing zero is not being removed. Neither Instant nor LocalDateTime have any textual representation in them, but generate one when you call toString. And doesn’t generate trailing zeroes in this case. They do generate ISO 8601 format, which for most purposes is fine, so please check once more whether the absence of zeroes is really a problem in your situation.
– Ole V.V.
Nov 15 at 8:30




For the sake of precision, the trailing zero is not being removed. Neither Instant nor LocalDateTime have any textual representation in them, but generate one when you call toString. And doesn’t generate trailing zeroes in this case. They do generate ISO 8601 format, which for most purposes is fine, so please check once more whether the absence of zeroes is really a problem in your situation.
– Ole V.V.
Nov 15 at 8:30




1




1




Why don’t you fix the regex instead?
– Holger
Nov 15 at 10:32




Why don’t you fix the regex instead?
– Holger
Nov 15 at 10:32












1 Answer
1






active

oldest

votes

















up vote
5
down vote













Formatting the date would help to retain the trailing zero



DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")


Output looks like below:




2018-11-15T08:03:45.580




The code below:



public class Post2 {

public static void main(String args) {

String date = LocalDateTime.ofInstant(Instant.now(), ZoneId.of("UTC"))
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"));
System.out.println(date);

}
}


EDIT
Adding regex matching to match date time with and without milli seconds.



        String regex = "^\d\d\d\d-(0?[1-9]|1[0-2])-(0?[1-9]|[12][0-9]|3[01]) (00|[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9])(\.{0,1}[0-9]{1,3})$";

String str1 = "2015-1-11 13:57:24";

String str2 = "2015-1-11 13:57:24.0";
String str3 = "2015-1-11 13:57:24.00";
String str4 = "2015-1-11 13:57:24.000";
String str5 = "2015-1-11 13:57:24.1";
String str6 = "2015-1-11 13:57:24.12";
String str7 = "2015-1-11 13:57:24.1222";
String str8 = "2015-1-11 13:57:24.02";

System.out.println( str1.matches(regex));
System.out.println(str2.matches(regex));
System.out.println(str3.matches(regex));
System.out.println(str4.matches(regex));
System.out.println(str5.matches(regex));
System.out.println(str6.matches(regex));
System.out.println(str7.matches(regex));
System.out.println(str8.matches(regex));


output:



true
true
true
true
true
true
false
true





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%2f53314552%2fjava-instant-to-localdatetime-trailing-zero%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    5
    down vote













    Formatting the date would help to retain the trailing zero



    DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")


    Output looks like below:




    2018-11-15T08:03:45.580




    The code below:



    public class Post2 {

    public static void main(String args) {

    String date = LocalDateTime.ofInstant(Instant.now(), ZoneId.of("UTC"))
    .format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"));
    System.out.println(date);

    }
    }


    EDIT
    Adding regex matching to match date time with and without milli seconds.



            String regex = "^\d\d\d\d-(0?[1-9]|1[0-2])-(0?[1-9]|[12][0-9]|3[01]) (00|[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9])(\.{0,1}[0-9]{1,3})$";

    String str1 = "2015-1-11 13:57:24";

    String str2 = "2015-1-11 13:57:24.0";
    String str3 = "2015-1-11 13:57:24.00";
    String str4 = "2015-1-11 13:57:24.000";
    String str5 = "2015-1-11 13:57:24.1";
    String str6 = "2015-1-11 13:57:24.12";
    String str7 = "2015-1-11 13:57:24.1222";
    String str8 = "2015-1-11 13:57:24.02";

    System.out.println( str1.matches(regex));
    System.out.println(str2.matches(regex));
    System.out.println(str3.matches(regex));
    System.out.println(str4.matches(regex));
    System.out.println(str5.matches(regex));
    System.out.println(str6.matches(regex));
    System.out.println(str7.matches(regex));
    System.out.println(str8.matches(regex));


    output:



    true
    true
    true
    true
    true
    true
    false
    true





    share|improve this answer



























      up vote
      5
      down vote













      Formatting the date would help to retain the trailing zero



      DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")


      Output looks like below:




      2018-11-15T08:03:45.580




      The code below:



      public class Post2 {

      public static void main(String args) {

      String date = LocalDateTime.ofInstant(Instant.now(), ZoneId.of("UTC"))
      .format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"));
      System.out.println(date);

      }
      }


      EDIT
      Adding regex matching to match date time with and without milli seconds.



              String regex = "^\d\d\d\d-(0?[1-9]|1[0-2])-(0?[1-9]|[12][0-9]|3[01]) (00|[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9])(\.{0,1}[0-9]{1,3})$";

      String str1 = "2015-1-11 13:57:24";

      String str2 = "2015-1-11 13:57:24.0";
      String str3 = "2015-1-11 13:57:24.00";
      String str4 = "2015-1-11 13:57:24.000";
      String str5 = "2015-1-11 13:57:24.1";
      String str6 = "2015-1-11 13:57:24.12";
      String str7 = "2015-1-11 13:57:24.1222";
      String str8 = "2015-1-11 13:57:24.02";

      System.out.println( str1.matches(regex));
      System.out.println(str2.matches(regex));
      System.out.println(str3.matches(regex));
      System.out.println(str4.matches(regex));
      System.out.println(str5.matches(regex));
      System.out.println(str6.matches(regex));
      System.out.println(str7.matches(regex));
      System.out.println(str8.matches(regex));


      output:



      true
      true
      true
      true
      true
      true
      false
      true





      share|improve this answer

























        up vote
        5
        down vote










        up vote
        5
        down vote









        Formatting the date would help to retain the trailing zero



        DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")


        Output looks like below:




        2018-11-15T08:03:45.580




        The code below:



        public class Post2 {

        public static void main(String args) {

        String date = LocalDateTime.ofInstant(Instant.now(), ZoneId.of("UTC"))
        .format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"));
        System.out.println(date);

        }
        }


        EDIT
        Adding regex matching to match date time with and without milli seconds.



                String regex = "^\d\d\d\d-(0?[1-9]|1[0-2])-(0?[1-9]|[12][0-9]|3[01]) (00|[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9])(\.{0,1}[0-9]{1,3})$";

        String str1 = "2015-1-11 13:57:24";

        String str2 = "2015-1-11 13:57:24.0";
        String str3 = "2015-1-11 13:57:24.00";
        String str4 = "2015-1-11 13:57:24.000";
        String str5 = "2015-1-11 13:57:24.1";
        String str6 = "2015-1-11 13:57:24.12";
        String str7 = "2015-1-11 13:57:24.1222";
        String str8 = "2015-1-11 13:57:24.02";

        System.out.println( str1.matches(regex));
        System.out.println(str2.matches(regex));
        System.out.println(str3.matches(regex));
        System.out.println(str4.matches(regex));
        System.out.println(str5.matches(regex));
        System.out.println(str6.matches(regex));
        System.out.println(str7.matches(regex));
        System.out.println(str8.matches(regex));


        output:



        true
        true
        true
        true
        true
        true
        false
        true





        share|improve this answer














        Formatting the date would help to retain the trailing zero



        DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")


        Output looks like below:




        2018-11-15T08:03:45.580




        The code below:



        public class Post2 {

        public static void main(String args) {

        String date = LocalDateTime.ofInstant(Instant.now(), ZoneId.of("UTC"))
        .format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"));
        System.out.println(date);

        }
        }


        EDIT
        Adding regex matching to match date time with and without milli seconds.



                String regex = "^\d\d\d\d-(0?[1-9]|1[0-2])-(0?[1-9]|[12][0-9]|3[01]) (00|[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9])(\.{0,1}[0-9]{1,3})$";

        String str1 = "2015-1-11 13:57:24";

        String str2 = "2015-1-11 13:57:24.0";
        String str3 = "2015-1-11 13:57:24.00";
        String str4 = "2015-1-11 13:57:24.000";
        String str5 = "2015-1-11 13:57:24.1";
        String str6 = "2015-1-11 13:57:24.12";
        String str7 = "2015-1-11 13:57:24.1222";
        String str8 = "2015-1-11 13:57:24.02";

        System.out.println( str1.matches(regex));
        System.out.println(str2.matches(regex));
        System.out.println(str3.matches(regex));
        System.out.println(str4.matches(regex));
        System.out.println(str5.matches(regex));
        System.out.println(str6.matches(regex));
        System.out.println(str7.matches(regex));
        System.out.println(str8.matches(regex));


        output:



        true
        true
        true
        true
        true
        true
        false
        true






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 18 at 16:52

























        answered Nov 15 at 8:06









        secret super star

        59011




        59011






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314552%2fjava-instant-to-localdatetime-trailing-zero%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

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            RAC Tourist Trophy