Joda-Time: Period to string

I’m using the Joda-Time library with Java. I’m having some difficulty trying to turn a Period object to a string in the format of “x days, x hours, x minutes”.

These Period objects are first created by adding an amount of seconds to them (they are serialized to XML as seconds and then recreated from them). If I simply use the getHours() etc. methods in them, all I get is zero and the total amount of seconds with getSeconds.

How can I make Joda calculate the seconds into the respective fields, like days, hours, etc…?

Incorrect jodatime period – years months days

I created a Utility method using jodatime to calculate date difference and display it such as 3y 2m 4d. Here is my method: public static String dateBetween(Object start, Object end) { Period period =

Bug in jodatime Period?

How come this test I wrote fails in jodatime 1.6.2? Is it a bug? @Test public void testIfJodaTimePeriodsHandlesPeriodTypesOtherThanMinutesAndHours() { long twentyDaysInMillis = TimeUnit.MILLISECONDS.c

JodaTime: Print Period with Days as Hours?

Is there any way to create a PeriodFormatter in JodaTime which is able to print a Period of lets say 2 days, 4 hours and 4 minutes as 52 hours, 4 minutes?

Clean toString from JodaTime Period

I’m calling toString() (or difference.normalizedStandard().toString()) on a Period object, and getting PT-10H0.317S Why is the default toString so inconvenient? What does this string even mean?

Convert duration PT01H00M to a Period with Jodatime

I’ve got a whole bunch of duration string I want to convert to a Period with Jodatime I tried the ISOPeriodFormat: PeriodFormatter pf = new PeriodFormatterBuilder().append(ISOPeriodFormat.standard()).

jodatime days period type problem/error

I have a problem with jodatime api. I can’t understand why this test doesn’t works. I need resolve how many days, hours, mins and seconds I have in X milliseconds. But the days value aren’t resolved..

How calculate negative period using JodaTime

I’m building an Android app and I need to calculate a sum of a period (hours and minutes). Pratically, I build a method that make this, but I’ve a problem with negative period. In example: First case

Error in Printing Period with JodaTime?

I’ve have a problem trying to print a given period between two dates. Let me show you the details, and then i’ll put the code : Date a = May, 20th , Date b = June, 19th Period in between should be 30

Any way to convert a JodaTime Period to a decimal number of hours?

I have a JodaTime Period that I’ve created from two DateTime instants. Is there a good way to convert that Period into a decimal number of hours? For instance, I have a Period that goes from 1pm to 1:

Serialization of JodaTime Period with Jaxb (BasePeriod does not have a default constructor)

I have written an adapter to handle serialization of a Period http://joda-time.sourceforge.net/api-release/org/joda/time/Period.html in JodaTime with JAXB as it says in http://blog.bdoughan.com/2011/0

Answers

    Period period = new Period();
    // prints 00:00:00
    System.out.println(String.format("%02d:%02d:%02d", period.getHours(), period.getMinutes(), period.getSeconds()));
    period = period.plusSeconds(60 * 60 * 12);
    // prints 00:00:43200
    System.out.println(String.format("%02d:%02d:%02d", period.getHours(), period.getMinutes(), period.getSeconds()));
    period = period.normalizedStandard();
    // prints 12:00:00
    System.out.println(String.format("%02d:%02d:%02d", period.getHours(), period.getMinutes(), period.getSeconds()));

You need to normalize the period because if you construct it with the total number of seconds, then that’s the only value it has. Normalizing it will break it down into the total number of days, minutes, seconds, etc.

Edit by ripper234 – Adding a TL;DR version: PeriodFormat.getDefault().print(period)

For example:

public static void main(String[] args) {
  PeriodFormatter daysHoursMinutes = new PeriodFormatterBuilder()
    .appendDays()
    .appendSuffix(" day", " days")
    .appendSeparator(" and ")
    .appendMinutes()
    .appendSuffix(" minute", " minutes")
    .appendSeparator(" and ")
    .appendSeconds()
    .appendSuffix(" second", " seconds")
    .toFormatter();

  Period period = new Period(72, 24, 12, 0);

  System.out.println(daysHoursMinutes.print(period));
  System.out.println(daysHoursMinutes.print(period.normalizedStandard()));
}

Will print:

24 minutes and 12 seconds
3 days and 24 minutes and 12 seconds

So you can see the output for the non-normalized period simply ignores the number of hours (it didn’t convert the 72 hours to 3 days).

You can also use the Default formatter, which is good for most cases:

Period period = new Period(startDate, endDate;
System.out.println(PeriodFormat.getDefault().print(period))
PeriodFormatter daysHoursMinutes = new PeriodFormatterBuilder()
    .appendDays()
    **.appendSuffix(" day", " days")
    .appendSeparator(" and ")
    .appendMinutes()
    .appendSuffix(" minute", " minutes")**
    .appendSeparator(" and ")
    .appendSeconds()
    .appendSuffix(" second", " seconds")
    .toFormatter();

you’re missing the hours, that’s why. Append hours after days and problem solved.