3

When using joda time with Grails you can set the default LocalDate format as they have done here. I want to do the same thing with Duration and Period so I don't have to type something like

durationFormatter.print(exampleObject.myDuration)

every time I want to display a duration or period. I want a call to the toString() method of a Duration / Period object and have it print in the desired format.

I have tried to implement PeriodFormatter in my config file much like they did here, but to no avail. Here is what I have so far in my config file:

jodatime {
    format.org.joda.time.LocalDate = "yyyy-MM-dd"
    format.org.joda.time.PeriodFormatter = { new format.org.joda.time.PeriodFormatterBuilder()
        .appendMonths()
        .appendSuffix( " month", " months" )
        .appendWeeks()
        .appendSuffix( " week", " weeks" )
        .appendDays()
        .appendSuffix( " day", " days" )
        .appendHours()
        .appendSuffix( " hour", " hours" )
        .toFormatter();
    }
}
Community
  • 1
  • 1
ubiquibacon
  • 10,451
  • 28
  • 109
  • 179
  • I had trouble with the default formatter, but then found out that it only applies to the `` tag provided by the Joda-Time grails plugin. See http://gpc.github.io/grails-joda-time/ref/Tags/format.html. – Charles Wood Jul 25 '13 at 17:45

1 Answers1

0

class

ISOPeriodFormat

field

cStandard  

this is default period formatter. This field is private static and haven't setter. But you can change this value using java-reflection API

  final Field f = ISOPeriodFormat.class.getDeclaredField("cStandard");
  f.setAccessible(true);
  f.set(null, yourPeriodFormatter);
Ilya
  • 29,135
  • 19
  • 110
  • 158