2

I have a string in format dHHmm that stands for a duration of time, and want to get the equivalent minutes.

Therefore I'm trying to convert it into a org.joda.time.Duration.getMinutes() object. But there seems to be no kind of DurationParser that could be used for this.

How could I best convert this string?

This is what I tried, but did not work:

PeriodFormatter formatter = new PeriodFormatterBuilder()
    .appendDays()
    .appendHours()
    .appendMinutes()
    .toFormatter();

assertTrue(formatter.parsePeriod("00310").getMinutes == 190); //failed with =0
membersound
  • 81,582
  • 193
  • 585
  • 1,120

1 Answers1

0

So, first you want to get org.joda.time.Duration from dHHmm...

import org.joda.time.Duration;

public Duration fromTheFormat( String str ) {
    Duration dayDuration = Duration.standardDays( parseInt( str.substring( 0, 1 ) ) );
    Duration hourDuration = Duration.standardHours( parseInt( str.substring( 1, 3 ) ) );
    Duration minuteDuration = Duration.standardMinutes( parseInt( str.substring( 3, 5 ) ) );

    return dayDuration.plus( hourDuration.getMillis() ).plus( minuteDuration.millis() );
}

or

import org.joda.time.Duration;
import org.joda.time.Period;
import org.joda.time.format.PeriodFormatter;

public Duration fromTheFormat( String str ) {
    Period period = new Period( /* years = */ 0,
                                /* months = */ 0,
                                /* weeks = */ 0,
                                /* days = */ parseInt( str.substring( 0, 1 ) ),
                                /* hours = */ parseInt( str.substring( 1, 3 ) ),
                                /* minutes = */ parseInt( str.substring( 3, 5 ),
                                /* seconds = */ 0,
                                /* millis = */ 0
    );

    // or best one is
    PeriodFormatter pf = new PeriodFormatterBuilder()
                           .minimumPrintedDigits( 1 )
                           .maximumParsedDigits( 1 )
                           .appendDays()
                           .minimumPrintedDigits( 2 )
                           .maximumParsedDigits( 2 )
                           .appendHours()
                           .appendMinutes()
                           .toFormatter();

    period = pf.parsePeriod( str );

    return period.toStandardDuration();
}
sarveshseri
  • 13,738
  • 28
  • 47
  • The second example is not valid Java. Java does not have named parameters, something like `years = 0` as an argument is a syntax error. – Jesper Feb 05 '15 at 14:43
  • There is no `.minimumParsedDigits()` in `PeriodFormatterBuilder`!? – membersound Feb 05 '15 at 14:53
  • @membersound... yes... true. – sarveshseri Feb 05 '15 at 14:57
  • 1
    Sorry, but: why do you post invalid code if you know it is invalid? – membersound Feb 05 '15 at 14:58
  • @membersound Was going to correct it... but forgot and posted. – sarveshseri Feb 05 '15 at 15:00
  • OK but did you try it? For me it gives a result of: 10 minutes, which is obviously wrong. – membersound Feb 05 '15 at 15:01
  • So... I gave 3 options... I think first 2 work fine... if I did not make more typying mistakes... I am still trying to correctly type the third one . – sarveshseri Feb 05 '15 at 15:03
  • I tried your 1st soluation and indeed: `duration.getStandardMinutes() == 190` OK. But: `duration.getPeriod().getMinutes()==10`. Why? Note that it gives the same result as with your 3rd solution, which is strange. Would be nice if we could figure this out, as from the syntax point of view I'd like the 3rd solution most. – membersound Feb 05 '15 at 15:08
  • Actually I tried it in Scala console. That's why I had to rewrite here for Java, which led to typing mistakes. – sarveshseri Feb 05 '15 at 15:09
  • 1
    @membersound that is fine... You are mis-understanding this... Period.getMinutes() returns minutes in this Period.. It does not return the total minutes. So...In current case this period has `0 days`, `3 hours` and `10 minutes`. Can we do anything about downvotes.. ? – sarveshseri Feb 05 '15 at 15:18
  • @Jasper fixed... forgot to comment them out. – sarveshseri Feb 05 '15 at 15:26