SimpleDateFormat parse 2018-11-08_21h34m46sZ









up vote
0
down vote

favorite












Hi want to convert UTC times to local time and i am doing this



public class TimeZoneN 
public static void main( String args ) throws ParseException

String timeStr1 = "2018-11-08_21h34m46sZ";

String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss's'Z";

SimpleDateFormat formatter = new SimpleDateFormat( formatStr1 );
formatter.setTimeZone( TimeZone.getTimeZone( "UTC" ) );

Date date1 = formatter.parse( timeStr1 );
System.out.println( date1 );





i am getting a parse exception because od the Z, but when i do



String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss'sZ'";


It passes, is that valid though ? Woudn't that consider sZ as a single constant and not Z as the timezone token ?










share|improve this question



















  • 1




    Yes, that's valid. Z is a literal.
    – ernest_k
    Nov 9 at 18:34










  • In that case what difference would that make if i dont include it at all ? I tried with String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss's'"; and its doing exactly the same job.
    – user2133558
    Nov 9 at 18:39










  • Z is not a literal, it’s a UTC offset of zero, also known as “Zulu time zone”. You need to parse as an offset or your formatter will assume the time zone of your JVM or the time zone set on the formatter and give you an incorrect result (if that didn’t happen to be UTC).
    – Ole V.V.
    Nov 9 at 19:32











  • I recommend you avoid the SimpleDateFormat class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in java.time, the modern Java date and time API. DateTimeFormatter.ofPattern("yyyy-MM-dd_HH'h'mm'm'ss's'X").parse(timeStr1, Instant::from) will give you an Instant of 2018-11-08T21:34:46Z.
    – Ole V.V.
    Nov 9 at 19:34











  • It's not a duplicate i am not asking for the same parsing format Ole.
    – user2133558
    Nov 9 at 21:18














up vote
0
down vote

favorite












Hi want to convert UTC times to local time and i am doing this



public class TimeZoneN 
public static void main( String args ) throws ParseException

String timeStr1 = "2018-11-08_21h34m46sZ";

String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss's'Z";

SimpleDateFormat formatter = new SimpleDateFormat( formatStr1 );
formatter.setTimeZone( TimeZone.getTimeZone( "UTC" ) );

Date date1 = formatter.parse( timeStr1 );
System.out.println( date1 );





i am getting a parse exception because od the Z, but when i do



String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss'sZ'";


It passes, is that valid though ? Woudn't that consider sZ as a single constant and not Z as the timezone token ?










share|improve this question



















  • 1




    Yes, that's valid. Z is a literal.
    – ernest_k
    Nov 9 at 18:34










  • In that case what difference would that make if i dont include it at all ? I tried with String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss's'"; and its doing exactly the same job.
    – user2133558
    Nov 9 at 18:39










  • Z is not a literal, it’s a UTC offset of zero, also known as “Zulu time zone”. You need to parse as an offset or your formatter will assume the time zone of your JVM or the time zone set on the formatter and give you an incorrect result (if that didn’t happen to be UTC).
    – Ole V.V.
    Nov 9 at 19:32











  • I recommend you avoid the SimpleDateFormat class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in java.time, the modern Java date and time API. DateTimeFormatter.ofPattern("yyyy-MM-dd_HH'h'mm'm'ss's'X").parse(timeStr1, Instant::from) will give you an Instant of 2018-11-08T21:34:46Z.
    – Ole V.V.
    Nov 9 at 19:34











  • It's not a duplicate i am not asking for the same parsing format Ole.
    – user2133558
    Nov 9 at 21:18












up vote
0
down vote

favorite









up vote
0
down vote

favorite











Hi want to convert UTC times to local time and i am doing this



public class TimeZoneN 
public static void main( String args ) throws ParseException

String timeStr1 = "2018-11-08_21h34m46sZ";

String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss's'Z";

SimpleDateFormat formatter = new SimpleDateFormat( formatStr1 );
formatter.setTimeZone( TimeZone.getTimeZone( "UTC" ) );

Date date1 = formatter.parse( timeStr1 );
System.out.println( date1 );





i am getting a parse exception because od the Z, but when i do



String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss'sZ'";


It passes, is that valid though ? Woudn't that consider sZ as a single constant and not Z as the timezone token ?










share|improve this question















Hi want to convert UTC times to local time and i am doing this



public class TimeZoneN 
public static void main( String args ) throws ParseException

String timeStr1 = "2018-11-08_21h34m46sZ";

String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss's'Z";

SimpleDateFormat formatter = new SimpleDateFormat( formatStr1 );
formatter.setTimeZone( TimeZone.getTimeZone( "UTC" ) );

Date date1 = formatter.parse( timeStr1 );
System.out.println( date1 );





i am getting a parse exception because od the Z, but when i do



String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss'sZ'";


It passes, is that valid though ? Woudn't that consider sZ as a single constant and not Z as the timezone token ?







java timezone simpledateformat utc datetime-parsing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 4:35









Ole V.V.

25.8k62550




25.8k62550










asked Nov 9 at 18:31









user2133558

1441318




1441318







  • 1




    Yes, that's valid. Z is a literal.
    – ernest_k
    Nov 9 at 18:34










  • In that case what difference would that make if i dont include it at all ? I tried with String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss's'"; and its doing exactly the same job.
    – user2133558
    Nov 9 at 18:39










  • Z is not a literal, it’s a UTC offset of zero, also known as “Zulu time zone”. You need to parse as an offset or your formatter will assume the time zone of your JVM or the time zone set on the formatter and give you an incorrect result (if that didn’t happen to be UTC).
    – Ole V.V.
    Nov 9 at 19:32











  • I recommend you avoid the SimpleDateFormat class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in java.time, the modern Java date and time API. DateTimeFormatter.ofPattern("yyyy-MM-dd_HH'h'mm'm'ss's'X").parse(timeStr1, Instant::from) will give you an Instant of 2018-11-08T21:34:46Z.
    – Ole V.V.
    Nov 9 at 19:34











  • It's not a duplicate i am not asking for the same parsing format Ole.
    – user2133558
    Nov 9 at 21:18












  • 1




    Yes, that's valid. Z is a literal.
    – ernest_k
    Nov 9 at 18:34










  • In that case what difference would that make if i dont include it at all ? I tried with String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss's'"; and its doing exactly the same job.
    – user2133558
    Nov 9 at 18:39










  • Z is not a literal, it’s a UTC offset of zero, also known as “Zulu time zone”. You need to parse as an offset or your formatter will assume the time zone of your JVM or the time zone set on the formatter and give you an incorrect result (if that didn’t happen to be UTC).
    – Ole V.V.
    Nov 9 at 19:32











  • I recommend you avoid the SimpleDateFormat class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in java.time, the modern Java date and time API. DateTimeFormatter.ofPattern("yyyy-MM-dd_HH'h'mm'm'ss's'X").parse(timeStr1, Instant::from) will give you an Instant of 2018-11-08T21:34:46Z.
    – Ole V.V.
    Nov 9 at 19:34











  • It's not a duplicate i am not asking for the same parsing format Ole.
    – user2133558
    Nov 9 at 21:18







1




1




Yes, that's valid. Z is a literal.
– ernest_k
Nov 9 at 18:34




Yes, that's valid. Z is a literal.
– ernest_k
Nov 9 at 18:34












In that case what difference would that make if i dont include it at all ? I tried with String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss's'"; and its doing exactly the same job.
– user2133558
Nov 9 at 18:39




In that case what difference would that make if i dont include it at all ? I tried with String formatStr1 = "yyyy-MM-dd_HH'h'mm'm'ss's'"; and its doing exactly the same job.
– user2133558
Nov 9 at 18:39












Z is not a literal, it’s a UTC offset of zero, also known as “Zulu time zone”. You need to parse as an offset or your formatter will assume the time zone of your JVM or the time zone set on the formatter and give you an incorrect result (if that didn’t happen to be UTC).
– Ole V.V.
Nov 9 at 19:32





Z is not a literal, it’s a UTC offset of zero, also known as “Zulu time zone”. You need to parse as an offset or your formatter will assume the time zone of your JVM or the time zone set on the formatter and give you an incorrect result (if that didn’t happen to be UTC).
– Ole V.V.
Nov 9 at 19:32













I recommend you avoid the SimpleDateFormat class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in java.time, the modern Java date and time API. DateTimeFormatter.ofPattern("yyyy-MM-dd_HH'h'mm'm'ss's'X").parse(timeStr1, Instant::from) will give you an Instant of 2018-11-08T21:34:46Z.
– Ole V.V.
Nov 9 at 19:34





I recommend you avoid the SimpleDateFormat class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in java.time, the modern Java date and time API. DateTimeFormatter.ofPattern("yyyy-MM-dd_HH'h'mm'm'ss's'X").parse(timeStr1, Instant::from) will give you an Instant of 2018-11-08T21:34:46Z.
– Ole V.V.
Nov 9 at 19:34













It's not a duplicate i am not asking for the same parsing format Ole.
– user2133558
Nov 9 at 21:18




It's not a duplicate i am not asking for the same parsing format Ole.
– user2133558
Nov 9 at 21:18












1 Answer
1






active

oldest

votes

















up vote
0
down vote













You are correct that Z denotes Zulu time, another name for UTC. You may also think of it as offset zero from UTC. So you will want to parse Z as an offset to ensure that your time is interpreted correctly.



java.time



However, don’t use SimpleDateFormat. It’s notoriously troublesome and long outdated. Don’t use Date either, it too is long outdated and it too has design problems.



 DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd_H'h'm'm's's'X");
String timeStr1 = "2018-11-08_21h34m46sZ";
Instant instant1 = formatter.parse(timeStr1, Instant::from);
System.out.println(instant1);


Output:




2018-11-08T21:34:46Z




For the sake of completeness:




want to convert … to local time




Taking America/Toronto as a random example:



 ZonedDateTime dateTime = instant1.atZone(ZoneId.of("America/Toronto"));
System.out.println(dateTime);


Output:




2018-11-08T16:34:46-05:00[America/Toronto]




In a comment you asked about Z:




what difference would that make if i dont include it at all ?




Two things:



  • You want to make sure the string includes the required offset and object if not, or you risk that errors pass unnoticed. It’s called input validation.

  • As I mentioned you want to parse Z as an offset or you cannot extract an unambiguous point in time from the parsed values.

Link: Oracle tutorial: Date Time explaining how to use java.time.






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%2f53231484%2fsimpledateformat-parse-2018-11-08-21h34m46sz%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
    0
    down vote













    You are correct that Z denotes Zulu time, another name for UTC. You may also think of it as offset zero from UTC. So you will want to parse Z as an offset to ensure that your time is interpreted correctly.



    java.time



    However, don’t use SimpleDateFormat. It’s notoriously troublesome and long outdated. Don’t use Date either, it too is long outdated and it too has design problems.



     DateTimeFormatter formatter
    = DateTimeFormatter.ofPattern("uuuu-MM-dd_H'h'm'm's's'X");
    String timeStr1 = "2018-11-08_21h34m46sZ";
    Instant instant1 = formatter.parse(timeStr1, Instant::from);
    System.out.println(instant1);


    Output:




    2018-11-08T21:34:46Z




    For the sake of completeness:




    want to convert … to local time




    Taking America/Toronto as a random example:



     ZonedDateTime dateTime = instant1.atZone(ZoneId.of("America/Toronto"));
    System.out.println(dateTime);


    Output:




    2018-11-08T16:34:46-05:00[America/Toronto]




    In a comment you asked about Z:




    what difference would that make if i dont include it at all ?




    Two things:



    • You want to make sure the string includes the required offset and object if not, or you risk that errors pass unnoticed. It’s called input validation.

    • As I mentioned you want to parse Z as an offset or you cannot extract an unambiguous point in time from the parsed values.

    Link: Oracle tutorial: Date Time explaining how to use java.time.






    share|improve this answer


























      up vote
      0
      down vote













      You are correct that Z denotes Zulu time, another name for UTC. You may also think of it as offset zero from UTC. So you will want to parse Z as an offset to ensure that your time is interpreted correctly.



      java.time



      However, don’t use SimpleDateFormat. It’s notoriously troublesome and long outdated. Don’t use Date either, it too is long outdated and it too has design problems.



       DateTimeFormatter formatter
      = DateTimeFormatter.ofPattern("uuuu-MM-dd_H'h'm'm's's'X");
      String timeStr1 = "2018-11-08_21h34m46sZ";
      Instant instant1 = formatter.parse(timeStr1, Instant::from);
      System.out.println(instant1);


      Output:




      2018-11-08T21:34:46Z




      For the sake of completeness:




      want to convert … to local time




      Taking America/Toronto as a random example:



       ZonedDateTime dateTime = instant1.atZone(ZoneId.of("America/Toronto"));
      System.out.println(dateTime);


      Output:




      2018-11-08T16:34:46-05:00[America/Toronto]




      In a comment you asked about Z:




      what difference would that make if i dont include it at all ?




      Two things:



      • You want to make sure the string includes the required offset and object if not, or you risk that errors pass unnoticed. It’s called input validation.

      • As I mentioned you want to parse Z as an offset or you cannot extract an unambiguous point in time from the parsed values.

      Link: Oracle tutorial: Date Time explaining how to use java.time.






      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        You are correct that Z denotes Zulu time, another name for UTC. You may also think of it as offset zero from UTC. So you will want to parse Z as an offset to ensure that your time is interpreted correctly.



        java.time



        However, don’t use SimpleDateFormat. It’s notoriously troublesome and long outdated. Don’t use Date either, it too is long outdated and it too has design problems.



         DateTimeFormatter formatter
        = DateTimeFormatter.ofPattern("uuuu-MM-dd_H'h'm'm's's'X");
        String timeStr1 = "2018-11-08_21h34m46sZ";
        Instant instant1 = formatter.parse(timeStr1, Instant::from);
        System.out.println(instant1);


        Output:




        2018-11-08T21:34:46Z




        For the sake of completeness:




        want to convert … to local time




        Taking America/Toronto as a random example:



         ZonedDateTime dateTime = instant1.atZone(ZoneId.of("America/Toronto"));
        System.out.println(dateTime);


        Output:




        2018-11-08T16:34:46-05:00[America/Toronto]




        In a comment you asked about Z:




        what difference would that make if i dont include it at all ?




        Two things:



        • You want to make sure the string includes the required offset and object if not, or you risk that errors pass unnoticed. It’s called input validation.

        • As I mentioned you want to parse Z as an offset or you cannot extract an unambiguous point in time from the parsed values.

        Link: Oracle tutorial: Date Time explaining how to use java.time.






        share|improve this answer














        You are correct that Z denotes Zulu time, another name for UTC. You may also think of it as offset zero from UTC. So you will want to parse Z as an offset to ensure that your time is interpreted correctly.



        java.time



        However, don’t use SimpleDateFormat. It’s notoriously troublesome and long outdated. Don’t use Date either, it too is long outdated and it too has design problems.



         DateTimeFormatter formatter
        = DateTimeFormatter.ofPattern("uuuu-MM-dd_H'h'm'm's's'X");
        String timeStr1 = "2018-11-08_21h34m46sZ";
        Instant instant1 = formatter.parse(timeStr1, Instant::from);
        System.out.println(instant1);


        Output:




        2018-11-08T21:34:46Z




        For the sake of completeness:




        want to convert … to local time




        Taking America/Toronto as a random example:



         ZonedDateTime dateTime = instant1.atZone(ZoneId.of("America/Toronto"));
        System.out.println(dateTime);


        Output:




        2018-11-08T16:34:46-05:00[America/Toronto]




        In a comment you asked about Z:




        what difference would that make if i dont include it at all ?




        Two things:



        • You want to make sure the string includes the required offset and object if not, or you risk that errors pass unnoticed. It’s called input validation.

        • As I mentioned you want to parse Z as an offset or you cannot extract an unambiguous point in time from the parsed values.

        Link: Oracle tutorial: Date Time explaining how to use java.time.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 4:39

























        answered Nov 10 at 4:25









        Ole V.V.

        25.8k62550




        25.8k62550



























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53231484%2fsimpledateformat-parse-2018-11-08-21h34m46sz%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

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20