在Grails中设置默认的joda持续时间/句点格式

时间:2012-09-06 18:22:30

标签: grails jodatime

joda time与Grails一起使用时,您可以设置默认的LocalDate格式here。我想对DurationPeriod执行相同的操作,因此我不必输入类似

的内容
durationFormatter.print(exampleObject.myDuration)

每次我想显示持续时间或期限。我希望调用toString() / Duration对象的Period方法,并以所需格式打印。

我尝试在配置文件中实现PeriodFormatter,就像它们here一样,但无济于事。以下是我目前在配置文件中的内容:

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

1 个答案:

答案 0 :(得分:0)

上课

ISOPeriodFormat

字段

cStandard  

这是默认的期间格式化程序。此字段为private static且尚未设置。但您可以使用java-reflection API

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