在Grails中设置默认joda持续时间/时段格式

在Grails中设置默认joda持续时间/时段格式,grails,jodatime,Grails,Jodatime,与Grails一起使用时,可以像Grails那样设置默认格式。我想和你做同样的事情,所以我不需要像 durationFormatter.print(exampleObject.myDuration) 每次我要显示持续时间或句点时。我想要调用Duration/Period对象的toString()方法,并以所需格式打印它 我尝试在我的配置文件中实现,就像他们那样,但没有效果。以下是到目前为止我在配置文件中的内容: jodatime { format.org.joda.time.Local

与Grails一起使用时,可以像Grails那样设置默认格式。我想和你做同样的事情,所以我不需要像

durationFormatter.print(exampleObject.myDuration)
每次我要显示持续时间或句点时。我想要调用
Duration
/
Period
对象的
toString()
方法,并以所需格式打印它

我尝试在我的配置文件中实现,就像他们那样,但没有效果。以下是到目前为止我在配置文件中的内容:

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();
    }
}
阶级

ISOPeriodFormat

cStandard  
这是默认的期间格式化程序。此字段为
私有静态
,没有设置器。但您可以使用java反射API更改此值

  final Field f = ISOPeriodFormat.class.getDeclaredField("cStandard");
  f.setAccessible(true);
  f.set(null, yourPeriodFormatter);

我在使用默认格式化程序时遇到了问题,但后来发现它只适用于Joda Time grails插件提供的
标记。看见