1 2 Previous Next 17 Replies Latest reply: Apr 6, 2011 2:20 PM by jschellSomeoneStoleMyAlias RSS

    Converting "minutes.seconds.milliseconds" to "seconds.milliseconds"

    677337
      Hello

      I have string (string a = 1.15.67) which represents time (minutes. seconds. milliseconds).

      Which is the easiest way to get a Double which holds the above mentioned time, where minutes are converted to seconds (double b = 75.67)?

      Should i convert the above mentioned String to a Date object first or is this not neccesary?

      Edited by: user10496629 on 17.03.2011 14:03
        • 1. Re: Converting "minutes.seconds.milliseconds" to "seconds.milliseconds"
          YoungWinston
          user10496629 wrote:
          I have string (string a = 1.15.67) which represents time (minutes. seconds. millseconds).
          Possible problem. Is that 67 milliseconds? Because if so, you have a bit of conversion to do, and it's a terrible format.

          Winston
          • 3. Re: Converting "minutes.seconds.milliseconds" to "seconds.milliseconds"
            796440
            user10496629 wrote:
            Hello

            I have string (string a = 1.15.67) which represents time (minutes. seconds. millseconds).

            Which is the easiest way to get a Double which holds the above mentioned time, where minutes are converted to seconds (double b = 75.67)?

            Should i convert the above mentioned String to a Date object first or is this not neccesary?
            No, a Date object is definitely not the way to go if you're representing elapsed time. Date is for representing a point in time. If you want to use date/time classes to do the work for you, look into Joda time. It may have something like that.

            If this is a one-off and you just want a quick conversion, however, I'd simply use String.split() to get the components, turn each one into a double, and then just do the math.
            • 4. Re: Converting "minutes.seconds.milliseconds" to "seconds.milliseconds"
              jduprez
              If you want to use date/time classes to do the work for you, look into Joda time. It may have something like that.

              If this is a one-off and you just want a quick conversion, however, I'd simply use String.split() to get the components, turn each one into a double, and then just do the math.
              ++
              Two good suggestions :o)
              Except myself I would use integral types (int, or long to be conservative) to hold the number of seconds/milliseconds. I don't see what double would bring except inaccurracy that will build up when you add/combine multiple durations.
              • 5. Re: Converting "minutes.seconds.milliseconds" to "seconds.milliseconds"
                796440
                jduprez wrote:
                If you want to use date/time classes to do the work for you, look into Joda time. It may have something like that.

                If this is a one-off and you just want a quick conversion, however, I'd simply use String.split() to get the components, turn each one into a double, and then just do the math.
                ++
                Two good suggestions :o)
                Except myself I would use integral types (int, or long to be conservative) to hold the number of seconds/milliseconds. I don't see what double would bring except inaccurracy that will build up when you add/combine multiple durations.
                Yeah, but given that he wants the end result to include a fractional piece, he's going to have inaccuracies anyway. And it's highly likely that the integer numbers of minutes, seconds, and millis that are represented individually will be exactly representable by double. In fact, if he keeps millis and seconds in their expected ranges, it's guaranteed that they are, and the only possible parsing inaccuracy is in minutes, and even then, the entire range of int can be exactly represented, so it'd be an awful lot of minutes before he started losing precision there.
                • 6. Re: Converting "minutes.seconds.milliseconds" to "seconds.milliseconds"
                  YoungWinston
                  jverd wrote:
                  In fact, if he keeps millis and seconds in their expected ranges, it's guaranteed that they are, and the only possible parsing inaccuracy is in minutes, and even then, the entire range of int can be exactly represented, so it'd be an awful lot of minutes before he started losing precision there.
                  Oddly enough, one of the very few MS things I remember liking was the way Access stored its dates:
                  basically [day offset].[fraction of day] stored as a double. Saves a lot of faffing about with fractions of seconds and worrying about granularity (it allows sub-millisecond precision for a long time yet).

                  Winston
                  • 7. Re: Converting "minutes.seconds.milliseconds" to "seconds.milliseconds"
                    jduprez
                    To be honest, my knowledge of FP arithmetic comes mostly from reading this forum, and I'm sure you contributed to such discussions in the past - so I'm surprised to read your arguments.
                    Except myself I would use integral types (int, or long to be conservative) to hold the number of seconds/milliseconds. I don't see what double would bring except inaccurracy that will build up when you add/combine multiple durations.
                    Yeah, but given that he wants the end result to include a fractional piece, he's going to have inaccuracies anyway
                    ?
                    Not if he stores the number of milliseconds, and only formats it at display time.
                    And it's highly likely that the integer numbers of minutes, seconds, and millis that are represented individually will be exactly representable by double.
                    In fact, if he keeps millis and seconds in their expected ranges, it's guaranteed that they are, and the only possible parsing inaccuracy is in minutes, and even then, the entire range of int can be exactly represented, so it'd be an awful lot of minutes before he started losing precision there.
                    Either I misunderstand you, or what you say is not correct:
                    System.out.println(0.1D+0.2D+0.3D);
                    Results in:
                    0.6000000000000001
                    • 8. Re: Converting "minutes.seconds.milliseconds" to "seconds.milliseconds"
                      796440
                      jduprez wrote:
                      To be honest, my knowledge of FP arithmetic comes mostly from reading this forum, and I'm sure you contributed to such discussions in the past - so I'm surprised to read your arguments.
                      Except myself I would use integral types (int, or long to be conservative) to hold the number of seconds/milliseconds. I don't see what double would bring except inaccurracy that will build up when you add/combine multiple durations.
                      Yeah, but given that he wants the end result to include a fractional piece, he's going to have inaccuracies anyway
                      ?
                      Not if he stores the number of milliseconds, and only formats it at display time.
                      Oh, I see what you're saying now.

                      Still, unless he's dealing with very large scales, he can store as a double, format for display, and any inaccuracies will wash out in the format. That is, even though 12345.67 cannot be stored in a double, most likely he will be able to ignore that, store 12345.67000000001, and format it so that it displays as 12345.67.

                      Whether that or a separate integer piece representation is more appropriate depends on other requirements and use cases that the OP hasn't shared with us. :-)


                      >
                      And it's highly likely that the integer numbers of minutes, seconds, and millis that are represented individually will be exactly representable by double.
                      In fact, if he keeps millis and seconds in their expected ranges, it's guaranteed that they are, and the only possible parsing inaccuracy is in minutes, and even then, the entire range of int can be exactly represented, so it'd be an awful lot of minutes before he started losing precision there.
                      Either I misunderstand you, or what you say is not correct:
                      System.out.println(0.1D+0.2D+0.3D);
                      Results in:
                      0.6000000000000001
                      But when you format for display, you'll get 0.600.
                      • 9. Re: Converting "minutes.seconds.milliseconds" to "seconds.milliseconds"
                        jduprez
                        Still, unless he's dealing with very large scales, he can store as a double, format for display, and any inaccuracies will wash out in the format.
                        That is, even though 12345.67 cannot be stored in a double, most likely he will be able to ignore that, store 12345.67000000001, and format it so that it displays as 12345.67.
                        Yes, but if he starts using the doubles for other purposes, and especially if he combines them (arithmetic with several durations), innacurracies will stack up.
                        Whether that or a separate integer piece representation is more appropriate depends on other requirements and use cases that the OP hasn't shared with us. :-)
                        You're right, nothing in the OP's description suggest that is a problem yet, but my remark was to point out that double was maybe not a thought-of choice.

                        And to clarify, although my reply was to you (and started with a ++, remember), the pointing out of double was merely directed to the OP, which stated double in his OP.

                        J.
                        • 10. Re: Converting "minutes.seconds.milliseconds" to "seconds.milliseconds"
                          802316
                          I would suggest considering long in millis-seconds rather than double in seconds. If you use double, be sure to use appropriate rounding.
                          String text = "1.15.67";
                          String[] parts = text.split("\\.", 2);
                          double time = Double.parseDouble(parts[0]) * 60 + Double.parseDouble(parts[1]);
                          System.out.printf("%s => %.3f%n", text, time);
                          prints
                          1.15.67 => 75.670
                          • 11. Re: Converting "minutes.seconds.milliseconds" to "seconds.milliseconds"
                            TPD-Opitz
                            following this discussion I'd say that this is a case of primitive obsession which is a code smell to be avoided.

                            In my view the appropriate solution was to have a Duration class like
                            class Duration extends Number /* or Date if you prefer */{
                              private final long seconds;
                              private final long fractions;
                            
                              Duration(long pSeconds, long pFractions){
                                seconds=pSeconds;
                                fractions=pFractions;
                              }
                              public Duration  add(Duration pOther){
                                return new Duration (seconds+pOther.seconds, fractions+pOther.fractions);
                              }
                              public String toString(){
                                return  String.valueOf(seconds)+"."+ String.valueOf(fractions);
                              }
                             // implementation of abstract Number / Date methods
                            }
                            And yes: adding fractions is more complex than shown.

                            Edited by: T.PD on 18.03.2011 13:12
                            Complexity of adding fractions is what maks the double approach so attractive, I think...

                            Edited by: T.PD on 18.03.2011 13:14
                            final keywords added to mak it immutable

                            bye
                            TPD
                            • 12. Re: Converting "minutes.seconds.milliseconds" to "seconds.milliseconds"
                              aksarben
                              I agree. For years I have used a custom class called TimeSpan that wraps a start Date & end Date. Its toString() method computes the difference between the 2 & formats the output string appropriately. Simple & (almost) idiot proof.
                              • 13. Re: Converting "minutes.seconds.milliseconds" to "seconds.milliseconds"
                                796440
                                jduprez wrote:
                                Still, unless he's dealing with very large scales, he can store as a double, format for display, and any inaccuracies will wash out in the format.
                                That is, even though 12345.67 cannot be stored in a double, most likely he will be able to ignore that, store 12345.67000000001, and format it so that it displays as 12345.67.
                                Yes, but if he starts using the doubles for other purposes, and especially if he combines them (arithmetic with several durations), innacurracies will stack up.
                                Yes, cumulative errors are always a potential issue. My hunch in this case however is that this won't be a problem. I could be wrong, of course. Just a gut feel based on what little the OP has said.
                                Whether that or a separate integer piece representation is more appropriate depends on other requirements and use cases that the OP hasn't shared with us. :-)
                                You're right, nothing in the OP's description suggest that is a problem yet, but my remark was to point out that double was maybe not a thought-of choice.
                                Fair enough.

                                In this case I was just willing to take the lazy route and assume the OP actually has a valid requirement for double.
                                • 14. Re: Converting "minutes.seconds.milliseconds" to "seconds.milliseconds"
                                  supratim
                                  Try the TimeUnit Enum in the java.util.concurrent package. This class provides very effective methods to convert time units from one form to another you will ever need.

                                  convert() method in TimeUnit will save your day very easily...
                                  1 2 Previous Next