1

I would like to format periods of time into English and other languages, like Joda Time does.

However I want an easy way to just list the most significant field or two, for example "2 years and three months" or "5 days" without me having to write code to handle this. The problem with Joda Time is it gives you output like "2 years, three months and five days" unless you write code.

There are libraries like Pretty Time which do exactly what I want but only for comparisons of times with now, like "3 months ago". I just want "3 months".

Surely there must be a library that acts like Pretty Time but for generic durations?

I'm specifically using Grails / Groovy, but a plain Java solution is equally acceptable.

Community
  • 1
  • 1
Fletch
  • 4,829
  • 2
  • 41
  • 55
  • Sometimes when you have specific requirements, you have to write some code. You should be able to copy something which is almost what you want and extend it. You could even supply the change as a patch so others can take advantage of your improvement. – Peter Lawrey Sep 25 '12 at 08:49
  • @PeterLawrey what do you mean by "specific"? You mean specific to me - i.e. unique? Then you are entirely right; however I just can't believe that that necessity is specific to me??!! – Fletch Sep 25 '12 at 08:55
  • In which case it would be very useful to provide any improvements you make to the open source projects so others can make use of them. – Peter Lawrey Sep 25 '12 at 09:00
  • I've not used PrettyTime (although I really want to), but under its features it says *Generate human-readable timestamps like, “right now”, “2 days ago”, or “3 months from now”* wvh eludes to the fact that it can do "dates" as well – MadProgrammer Sep 25 '12 at 09:14

1 Answers1

1

Why you can't write a little bit of code using Joda Time? I had a similar problematic as you have and I solved it whit an utility method like this:

DateTime dt = new DateTime(); // Now
DateTime plusDuration = dt.plus(new Duration(110376000000L)); // Now plus three years and a half

// Define and calculate the interval of time
Interval interval = new Interval(dt.getMillis(), plusDuration.getMillis());

// Parse the interval to period using the proper PeriodType
Period period = interval.toPeriod(PeriodType.yearMonthDayTime());

// Define the period formatter for pretty printing the period
PeriodFormatter pf = new PeriodFormatterBuilder()
        .appendYears().appendSuffix("y ", "y ")
        .appendMonths().appendSuffix("m", "m ").appendDays()
        .appendSuffix("d ", "d ").appendHours()
        .appendSuffix("h ", "h ").appendMinutes()
        .appendSuffix("m ", "m ").appendSeconds()
        .appendSuffix("s ", "s ").toFormatter();

// Print the period using the previously created period formatter
System.out.println(pf.print(period).trim());

Perhaps it's not what you are looking for, but I hope it helps.

Regards.

  • You didn't write any of the code above. It appears you don't want to add any code either? – Peter Lawrey Sep 25 '12 at 09:02
  • @PeterLawrey ideally not :-). In any case, the code above does not actually fulfill the requirement, so I would have to write it anyway. For example days should only be appended if there are no months/years. – Fletch Sep 25 '12 at 09:37
  • @Fletch so if you have 50 days, it would appear as `1 m, 480 h` ? – Peter Lawrey Sep 25 '12 at 09:40
  • @PeterLawrey it would appear like the date of when you asked the question on StackOverflow, but without "ago". So in your example, it would appear as "2 months" (due to rounding). – Fletch Sep 25 '12 at 09:55
  • So you want `prettyPrintDate.replace("ago", "")` ? – Peter Lawrey Sep 25 '12 at 09:58
  • @PeterLawrey Yep, but internationalised – Fletch Sep 25 '12 at 10:12
  • 2
    I understand you want to avoid writing code, when it's possible I try to avoid it too because I think the most maintainable code is the code you don't write so I try to take as much advantage as possible of existing libraries. Nevertheless, about my experience my advice is that sometimes doesn't exist specifically what you are looking for and is not worth to spend more time trying to find something that doesn't exist. It's up to you keep trying to find an out of the box solution or write a little bit :) – Alejandro García Seco Sep 25 '12 at 10:12