Converting Timestamp string “HH:mm:ss” to Duration

前端 未结 3 1078
囚心锁ツ
囚心锁ツ 2021-01-20 03:28

Just like the title says, I\'ve been able to isolate from a text file a string containing a duration in the format of \"HH:mm:ss.\" How would I go about converting it to a d

相关标签:
3条回答
  • 2021-01-20 03:39

    tl;dr

    Duration.between ( 
        LocalTime.MIN , 
        LocalTime.parse ( "08:30:00" ) 
    ).toString()
    

    PT8H30M

    Duration vs Timestamp

    If by the string 08:30:00 you mean "eight and a half hours" span of time, then do not use the term "timestamp". The word “duration” is correct and commonly used. And avoid that format of HH:MM:SS because it is so ambiguous, appearing to be a time-of-day. Instead use the standard format discussed below.

    If by the string 08:30:00 you mean “half-past eight in the morning”, then use the word 'timestamp' and avoid the term 'duration'.

    These are two very different concepts. You must get clear on them, each should be distinct in your mind. Using the ambiguous format of HH:MM:SS makes that distinction all the more difficult (so avoid that format!).

    java.time

    The modern way is with the java.time classes.

    LocalTime

    As mentioned in the comment by Sharma, first parse your string as a LocalTime. This class represents a time-of-day without a date and without a time zone. Having no time zone means these objects are based on a generic 24-hour clock without regard for anomalies such as Daylight Saving Time (DST).

    We do not really want a LocalTime as your input string represents a span of time rather than a time-of-day. But this is just the first step.

    LocalTime lt = LocalTime.parse ( "08:30:00" );
    

    Duration

    To represent the desired span-of-time, we want the Duration class. This class is for spans of time not attached to the timeline. We can create one by converting that LocalTime via getting the amount of time from the beginning of the time-of-day clock, 00:00:00.0 or LocalTime.MIN, and the lt we just instantiated.

    Duration d = Duration.between ( LocalTime.MIN , lt );
    

    ISO 8601

    We can see the result by generating a String in standard ISO 8601 format for durations by simply calling Duration::toString. The java.time classes use ISO 8601 by default when parsing/generating strings. For durations, the standard format is PnYnMnDTnHnMnS where the P marks the beginning and the T separates the years-months-days portion from the hours-minutes-seconds portion. So, our eight-and-a-half hours will appear as PT8H30M.

    System.out.println ( "d.toString(): " + d );

    d.toString(): PT8H30M

    Avoid decimal numbers for date-time values

    For handling such duration values, I strongly suggest using the Duration object in your Java code and the ISO 8601 format when serializing to text.

    • Using objects in your code provides type-safety, makes your code more self-documenting, and ensures valid values.
    • Using ISO 8601 format for text makes for values that can easily be parsed, is a format easily read by humans, and is unambiguous in its meaning unlike 08:30:00 which can be misread as a time-of-day.

    Your desired format of a decimal number like 8.5 is ambiguous (easy to lose track of its meaning). Also a decimal number is awkward and often incorrect when using a floating-point type (float, Float, double, Double) as these types generate extraneous incorrect digits at the far end of the decimal fraction. Floating-point technology purposely trades away accuracy to gain speed of execution.

    BigDecimal

    If you must use a fractional number, and want accuracy, use the BigDecimal class.

    In this example, I assume you want hours and minutes while truncating any fractional minute. So I call toMinutes on the Duration.

    BigDecimal minutesPerHour = new BigDecimal ( 60L ); // Use var/constant for clarity of your intent.
    BigDecimal minutes = new BigDecimal ( d.toMinutes () );
    BigDecimal fractionalHours = minutes.divide ( minutesPerHour );
    

    frationalHours.toString(): 8.5

    See this example code live in IdeOne.com.

    You might want to use BigDecimal facility for rounding if you insist on using these decimals rather than Duration objects and ISO 8601 text.


    About java.time

    The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

    The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

    To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

    Where to obtain the java.time classes?

    • Java SE 8 and SE 9 and later
      • Built-in.
      • Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
    • Java SE 6 and SE 7
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
      • See How to use ThreeTenABP….

    The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

    0 讨论(0)
  • 2021-01-20 03:51

    You can split the expression into parts then parse and calculate duration:

        String[] a = "08:30:00".split(":");
        double d = Integer.parseInt(a[0]) + Integer.parseInt(a[1]) / 60.0;
    
    0 讨论(0)
  • 2021-01-20 03:57

    I would use String.split() to accomplish this. That will split it into an array based on the regex/separators that you specify in the parameters. Something like this,

    String time = "08:30:00";
    String[] splitValues = time.split(":");
    double hour = splitValue[0];
    double minute = splitValue[1];
    double seconds = splitValue[2];
    

    You could then add up the values. You know that there are 60 minutes in an hour, and 60 seconds in a minute. You could convert these to hours by dividing by 60 and 3600, respectively. Then, it's just a simple matter of addition.

    0 讨论(0)
提交回复
热议问题