0

I'm trying to shift datetime by a Duration which I get in the format "HHmm", for example "0010" meaning 10 minutes.

I've found similar ticket (Parsing time strings like "1h 30min") but I can't make it work properly.

Here's the code:

PeriodFormatter hoursMinutes = new PeriodFormatterBuilder().appendHours().appendMinutes().toFormatter();
Duration duration = hoursMinutes.parsePeriod("0010").toStandardDuration();
duration.getStandardMinutes(); //Returns 600

For some reason, I get 600 minutes instead of 10. So it looks like the minutes were interpreted as hours, but I can't understand why. I've tried adding .maximumParsedDigits(2) for hours, but the result was the same.

Why is my code wrong? Is there some other way to initialize duration parser? Something, where I could just use the standard format like "HHmm"?

Community
  • 1
  • 1
NeplatnyUdaj
  • 6,052
  • 6
  • 43
  • 76

2 Answers2

2

So the issue was really with the maximum parseddigits. I only had to add it before hours, not minutes. So the solution is this:

PeriodFormatter hoursMinutes =
                new PeriodFormatterBuilder().maximumParsedDigits(2).appendHours().appendMinutes().toFormatter();
Duration duration = hoursMinutes.parsePeriod("0010").toStandardDuration();
duration.getStandardMinutes(); //Returns 10
NeplatnyUdaj
  • 6,052
  • 6
  • 43
  • 76
  • Ah okay, you were quicker +1 so I deleted my answer. Personally I prefer a pattern-based approach., for example using my lib Time4J: `Duration d = Duration.formatter(ClockUnit.class, "#hmm").parse("0010"); long minutes = ClockUnit.MINUTES.convert(d); System.out.println(minutes); // 10` – Meno Hochschild Apr 05 '16 at 12:17
  • I also like your solution more, but I don't want to add another library just for this. – NeplatnyUdaj Apr 05 '16 at 12:20
  • too bad that they did not include this in jsr-310 – Woody Apr 13 '18 at 16:45
0

According to the documentation:

getStandardMinutes

public long getStandardMinutes()

Gets the length of this duration in minutes assuming that there are the standard number of milliseconds in a minute. This method assumes that there are 60 seconds in a minute and 1000 milliseconds in a second. All currently supplied chronologies use this definition.

This returns getMillis() / 60000. The result is an integer division, thus excess milliseconds are truncated.

Returns: the length of the duration in standard seconds

Seeing as 600 is actually 10 minutes in seconds (60 * 10), you got the answer you expected.

Ori Lentz
  • 3,668
  • 6
  • 22
  • 28
  • I don't think the documentation says what you are implying. The problem is not in the `getStandardMinutes()`, but in the Duration value. When I call `getStandardHours()`, it prints `10`. When I add this duration to the ReadableInstant like DateTime, it is really shifted by 10 hours, not 10 minutes. – NeplatnyUdaj Apr 05 '16 at 11:15
  • So yes... documentation says what you are implying, but it's lying. `getMillis() / 60000` obviously isn't a value in seconds. – NeplatnyUdaj Apr 05 '16 at 12:21