5 Replies Latest reply: Jan 26, 2014 4:44 PM by rp0428 RSS

    Java 8: Pb with new java.time api

    bsar69

      Hello,

       

      I'm trying to use the new java.time api that comes with Java 8 (I already know joda-time).

      I'm using the latest build B124.

       

      I've a String "01/08/2012_00:00:01", I know that the time ref. is UTC, and I want to convert it to an 'Instant';

       

      I tried:

      DateTimeFormatter FORMAT_DT = DateTimeFormatter.ofPattern("dd/MM/yyyy_HH:mm:ss").withZone(ZoneOffset.UTC);
      Instant instant = Instant.from(FORMAT_DT.parse("01/08/2012_00:00:01"));

      But it fails with following error:

      java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {},ISO resolved to 2012-08-01T00:00:01 of type java.time.format.Parsed
           at java.time.Instant.from(Unknown Source)

      I don't understand what's wrong with my code, and I can't figure out how I should proceed to convert the String to an Instant ...

       

      Any suggestion welcome,

       

      Best regards,

      Bernard.

        • 1. Re: Java 8: Pb with new java.time api
          TPD-Opitz

          why don't you try parsing the date as suggested here:

          http://docs.oracle.com/javase/tutorial/datetime/iso/format.html

           

          bye

          TPD

          • 2. Re: Java 8: Pb with new java.time api
            bsar69

            Hello,

             

            Thanks for this reply.

             

            At the end, I need to get an Instant, not a LocalDateTime, so I could of course do something like this:

             

                    LocalDateTime ldt = LocalDateTime.from(FORMAT_DT.parse("01/08/2012_00:00:01"));
                    Instant instant = Instant.from(ldt.atZone(ZoneOffset.UTC));

             

            But this seems quite verbose (especially compared to joda api) ... Why isn't it possible to directly convert a String to a Instant using a parser ?

             

            Best regards,

            Bernard.

            • 3. Re: Java 8: Pb with new java.time api
              rp0428

              I don't understand what's wrong with my code, and I can't figure out how I should proceed to convert the String to an Instant ...

              Your question is equivalent to saying 'when I do steps A, B, C, D, E and F all at once I get an exception; what is wrong?'. What is wrong is that you have a problem in step A, or B, or C, or D, or E or F. So start figuring out which one of those steps is causing the problem.

               

              You should 'proceed' using the traditional standard troubleshooting methods that apply to any multi-step process that produces an error at the end. You will never learn to troubleshoot problems if you just throw your hands up in the air and ask for help just because a complex series of steps doesn't complete successfully.

               

              Break the process into its individual steps and check the results of each step to identify which step is FIRST producing an error.

              DateTimeFormatter FORMAT_DT = DateTimeFormatter.ofPattern("dd/MM/yyyy_HH:mm:ss").withZone(ZoneOffset.UTC);

              That statement consists of multiple steps: the 'ofPattern' method being applied to a String pattern, producing a NEW formatter, then the 'withZone' method applied to that new formatter producing yet ANOTHER new formatter.

               

              1. So execute the 'ofPattern' method SEPARATELY and capture the return value into a new variable.

              2. Did step #1 produce an exception? If not then examine that variable to ensure that it is valid. If it did then find the problem.

              3. Now apply the 'withZone' method to the formatter produced in step #1 and capture the return value into a new variable.

              4. Did step #3 produce an exception? If not then examine that variable to ensure that it is valid. If it did then find the problem.

              Instant instant = Instant.from(FORMAT_DT.parse("01/08/2012_00:00:01"));

              That statement consists of multiple steps: the 'parse' method being applied to a formatter producing a TemporalAccessor, then the 'from' method being applied using that new TemporalAccessor.

               

              5. So execute the 'parse' method separately and capture the return value into a new variable.

              6. Did step #5 produce an exception? If not then examine that variable to ensure that it is valid. If it did then find the problem.

              7. Now apply the 'from' method to that new object.

              8. Did step #7 produce an exception? and so on.

              • 4. Re: Java 8: Pb with new java.time api
                bsar69
                3.  Re: Java 8: Pb with new java.time api

                   rp0428 Guru @rp0428 , FYI, I'm SW engineer since more than 20 years, working with Java since 10 years, using joda-time since 4 years, so your statement about 'I will never learn ...' hmm ... but since I'm not a native English speaker, I will not enter further into this discussion ...

                 

                To come back to the technical topic, at a glance, I would expect that converting a String to an Instant should be a basic/simple need, so it should be possible to do it in two lines of code; for the time being, the only way I found is to use a LocalDateTime intermediate variable, which seems quite verbose, and I'm wondering if someone knows a better way to do it, and would be willing to share it here.

                 

                Regards,

                Bernard.

                • 5. Re: Java 8: Pb with new java.time api
                  rp0428

                  Originally you stated this:

                  I don't understand what's wrong with my code,

                  So naturally we focused on how to "understand what's wrong' with your code.

                  You will never learn to troubleshoot problems if you just throw your hands up in the air and ask for help just because a complex series of steps doesn't complete successfully. 

                   

                  Break the process into its individual steps and check the results of each step to identify which step is FIRST producing an error.

                  When a complex or multi-step process has an error you start checking the individual steps of the process to see how far it gets successfully.

                  I would expect that converting a String to an Instant should be a basic/simple need, so it should be possible to do it in two lines of code; for the time being, the only way I found is to use a LocalDateTime intermediate variable, which seems quite verbose, and I'm wondering if someone knows a better way to do it, and would be willing to share it here.

                  Just a technical distinction: your latest example using a LocalDateTime intermediate variable CAN be done in one line of code by just using dot-notation to avoid creating an explicit intermediate variable. But that variable will still be created implicitly behind the scenes.

                   

                  Converting a String to an Instant might be considered a basic/simple need but the set of functionality related to dealing with times, dates, calendars, etc is extremely complex. It makes much more sense to develop the requisite functionality in modules.

                   

                  Especially when introducing new functionality such as the 'Instant' class are related package elements introduced in 1.8. That functionality builds on what came before and parsers already existing that know how to deal with all of the possible String variants and formatting options and so on.

                   

                  Since that work CAN BE done on-the-fly using dot notation and anonymous implicit intermediate classes there isn't much need to reinvent that functionality in the new classes. If certain uses become standard new methods can always be added (e.g. to the Instant class) that will 'appear' to do things in one step.

                   

                  And, being an early adopter release, you can always file a 'bug' or enhancement request from the 1.8 download page. That page has links for 'Report Bugs' and 'Feedback forum' that you can use.

                   

                  In this current forum no one can give you an 'official' answer as to why something was implemented a particular way and/or whether that implementation is a 'bug' or was designed to work that way. Only Oracle can do that.